另一个令人困惑的地方是直接在 Setup() 中使用 this。当您使用 new 调用函数时,正如您所做的那样,this 不会绑定到全局对象,而是绑定到 Setup 的新实例> 对象。
对于上面的示例,这意味着调用 new o.doSomething() 将发出“未定义”警报,因为为调用创建的新对象没有“someVal”成员。
If you have a function that uses this, you must make sure that the calling context is correct. i.e. use this.helperFunc(), not just helperFunc() (but you'll also need to set things up so that this.helperFunc is defined). The referent of this inside helperFunc in your example is not the same as it is in Create().
You can think of it as though functions are not defined as members of objects, but called as members of objects.
There are three things that this might resolve to, depending on context.
A newly created object, if the function call was preceded by the new keyword.
The Object to the left of the dot when the function was called.
The Global Object (window), if neither of the above are provided.
If a function is called without an object, as in the case of your call to helperFunc in this.Create, this will be bound to the global object (window, when used in a browser)
Given something like this:
var o = {someVal:"hello"};
o.doSomething = function (){alert(this.someVal)};
Calling o.doSomething() will obviously alert "hello".
Given:
var o2 = {someVal:"world"};
o2.someFunc = o.doSomething;
Calling o2.someFunc() will alert "world", not "hello", as you would expect if it were a pointer to the doSomething member of o.
And given:
var someFunc = o.doSomething
someVal = "sailor"
Calling someFunc() will alert "sailor".
Another point of confusion is the use of this directly within Setup(). When you call a function using new, as you have done, this is not bound to the global object, but to a new instance of the Setup object.
For my example above, this means that calling new o.doSomething() will alert "undefined", as the new object that has been created for the call does not have a "someVal" member.
发布评论
评论(4)
引用 Xhalent 的精彩文章(做得非常好,写得很清楚)由他:
所以在你的情况下:
可能会达到预期的效果。
To cite Xhalent's wonderful article (really well done and clearly wirtten) mentioned by him:
So in your case:
might achieve what's desired.
我已经完成了一系列关于基本 javascript 基础知识的文章 - 这里介绍了对象和原型:
Javascript 对象实例化和原型
我在这里深入研究闭包:Javascript 闭包
希望它们有所帮助。干杯。
I've done a series on basic javascript fundamentals - objects and prototypes are covered here:
Javascript Object Instantation and Prototypes
I delve into Closures here:Javascript Closures
Hope they help. Cheers.
如果您有一个使用
this
的函数,则必须确保调用上下文正确。即使用this.helperFunc()
,而不仅仅是helperFunc()
(但您还需要进行一些设置,以便定义 this.helperFunc)。您的示例中 helperFunc 内的this
的指称与Create()
中的指称不同。您可以将其视为函数不是作为对象的成员定义,而是作为对象的成员调用。
根据上下文,
this
可能会解析三件事。new
关键字,则为新创建的对象。window
)(如果以上两者均未提供)。如果在没有对象的情况下调用函数,就像在
this.Create
中调用helperFunc
的情况一样,this
将绑定到全局对象(window
,当在浏览器中使用时)给定这样的东西:
调用
o.doSomething()
显然会警告“hello”。给出:
调用
o2.someFunc()
将警告“world”,而不是“hello”,正如您所期望的,如果它是指向的
doSomething
成员的指针Ø 。并给出:
调用
someFunc()
将提醒“sailor”。另一个令人困惑的地方是直接在
Setup()
中使用this
。当您使用new
调用函数时,正如您所做的那样,this
不会绑定到全局对象,而是绑定到Setup
的新实例> 对象。对于上面的示例,这意味着调用 new o.doSomething() 将发出“未定义”警报,因为为调用创建的新对象没有“someVal”成员。
If you have a function that uses
this
, you must make sure that the calling context is correct. i.e. usethis.helperFunc()
, not justhelperFunc()
(but you'll also need to set things up so that this.helperFunc is defined). The referent ofthis
inside helperFunc in your example is not the same as it is inCreate()
.You can think of it as though functions are not defined as members of objects, but called as members of objects.
There are three things that
this
might resolve to, depending on context.new
keyword.window
), if neither of the above are provided.If a function is called without an object, as in the case of your call to
helperFunc
inthis.Create
,this
will be bound to the global object (window
, when used in a browser)Given something like this:
Calling
o.doSomething()
will obviously alert "hello".Given:
Calling
o2.someFunc()
will alert "world", not "hello", as you would expect if it were a pointer to thedoSomething
member ofo
.And given:
Calling
someFunc()
will alert "sailor".Another point of confusion is the use of
this
directly withinSetup()
. When you call a function usingnew
, as you have done,this
is not bound to the global object, but to a new instance of theSetup
object.For my example above, this means that calling
new o.doSomething()
will alert "undefined", as the new object that has been created for the call does not have a "someVal" member.此页面上有 Mike Koss 撰写的关于 JS 中的 OOP 的精彩教程。
第二组括号立即调用您在第一组括号中声明的函数。
您可以声明它并单独执行它(允许执行多次):
或者在一行中执行两者:
You have a good tutorial by Mike Koss on OOP in JS on this page.
The second set of parenth immediatly invokes the function you have declared in the first set of parenth.
You could declare it and execute it separatly (allows to execute multiple times):
Or do both in one line: