JavaScript - 就是这个 [英] JavaScript - this of this
问题描述
String.prototype.foo = {};
String.prototype.foo.bar = function() {
//How can you reference the "grandparent" string?
console.log(this.parent.parent); //obviously, doesn't exist
}
如同,你好,护士!。foo.bar()
会记录你好,护士!。
As in, "Hello, Nurse!".foo.bar()
would log "Hello, Nurse!".
如果它有所作为是否可以控制foo?
Will it make a difference if there's control over foo?
编辑:在那里,定义了foo。
There, foo is defined.
编辑2:很好,而不是 this.this.this
, this.parent.parent
。当然父母不存在,但希望现在语义不会妨碍。
Fine, instead of this.this.this
, this.parent.parent
. Of course parent doesn't exist, but hopefully now the semantics won't get in the way.
编辑3:没有具体案例。提供的细节几乎都是我得到的:有一个对象foo,原型的一部分。 foo.bar
是 foo
的方法,并且应该访问其祖父母。而已。没有其他的。这就是我所拥有的所有信息。
There isn't a specific case. The provided details are pretty much all I got: There's an object foo, part of a prototype. foo.bar
is a method of foo
, and is supposed to access its grandparent. That's it. Nothing else. That's all the information I have.
Edit4:已解决。根据提供的答案(以及Douglas Crockford的一些二手帮助):
Resolved. Based on the answer provided (and some second-hand help from Douglas Crockford):
String.prototype.foo = function() {
var that = this;
return {
bar : function() {
console.log(that.valueOf());
}
}
}
//Called:
"Hello, Nurse!".foo().bar();
推荐答案
这样做的唯一方法就是转向 foo()
成一个函数。可以把它想象为初始化特定字符串的 foo
命名空间:
The only way this can be done, is to turn foo()
into a function. Think of it as initialising the foo
namespace for a particular string:
String.prototype.foo = function () {
var str = String(this);
var o = Object(this)
o.bar = function () {
console.log(str);
};
return o;
};
然后你可以使用:
"foobar".foo().bar(); // logs "foobar"
或者如果我们重命名 foo
和 bar
更令人兴奋的事情:
Or if we rename foo
and bar
into something more exciting:
"Hello!".console().log(); // logs "Hello!"
为什么这么复杂?
使用特定的上下文调用每个函数,该函数是单个对象。是否使用 ab()
或 abcd()
调用它无关紧要 - 它立即被赋予对象函数调用的左作为其上下文。因此 ab()
的上下文将是 a
,以及 abcd()的上下文
是 c
。关键字 this
引用上下文。因为 c
只是一个对象(不是正在运行的函数),它没有上下文,并且它没有这个概念
,所以 this.this
毫无意义。
Why is it so complicated?
Each function is called with a particular context that is a single object. Whether it is called with a.b()
or a.b.c.d()
doesn't matter - it is given the object immediately to the left of the function call as its context. So the context for a.b()
would be a
, and the context for a.b.c.d()
is c
. The keyword this
references the context. Because c
is just an object (not a running function) it has no context, and it has no concept of this
, so this.this
makes no sense.
因此,这是不可能的一般地访问所谓的父母。胡安的回答给出了一个很好的概念解释原因。但是,如果你想要实现的是原型函数中的命名空间,那么可以通过从 foo
返回一个增强对象来实现这一点。
Therefore, it is not possible to generically access the so-called "parent". Juan's answer gives a good conceptual explanation why. However, if what you want to achieve is namespacing in prototype functions, then you can do this by returning an augmented object from foo
.
注意我还必须将此
转换为上面的对象
。这是因为您无法将属性附加到原始值(如字符串)。 var str =foo; str.bar = 1
将起作用,但只是因为JS自动将foo
转换为对象。但是,由于 str
引用了原语,而不是自动创建的对象,因此该对象立即被丢弃,我们将丢失 bar
。
Notice I also had to convert this
into an Object
above. This is because you can't attach properties to primitive values like strings. var str = "foo"; str.bar = 1
will work, but only because JS automatically converts "foo"
into an object. However, since str
references the primitive, not the automatically created object, the object is then immediately discarded, and we lose bar
.
这篇关于JavaScript - 就是这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!