我正在努力更好地理解JavaScript中that和this的用法。我正在遵循Douglas Crockford的教程,链接在此:http://javascript.crockford.com/private.html 但是有几个问题让我感到困惑。下面是我的一个例子,我想知道我是否正确使用了它们:
function ObjectC()
{
//...
}
function ObjectA(givenB)
{
ObjectC.call(this); //is the use of this correct here or do we need that?
var aa = givenB;
var that = this;
function myA ()
{
that.getA(); //is the use of that correct or do we need this?
}
this.getA = function() //is the use of this correct?
{
console.log("ObjectA");
};
}
function ObjectB()
{
var that = this;
var bb = new ObjectA(that); //is the use of that correct or do we need this?
this.getB = function()
{
return bb;
};
that.getB(); //is the use of that correct or do we need this?
}
请注意,这只是一个例子。
that
不是关键字,而是普通的变量名。 - Bergithis
的值,因为 getB 函数中的this
与 ObjectB 不同。 - guy777