Javascript 为什么是';这';没有采取正确的范围
好吧,我知道Javascript中的Javascript 为什么是';这';没有采取正确的范围,javascript,scope,Javascript,Scope,好吧,我知道Javascript中的this的范围有上千条线程(这让人怀疑该语言是否设计得很好),但我仍然无法解释“this”一词: //works function Cat() { this.theCatName = "Mistigri"; function meow() {alert(this.theCatName + " meow") }; this.meow = meow } } var MyCat = new Cat() MyCat.meow() //works fun
this
的范围有上千条线程(这让人怀疑该语言是否设计得很好),但我仍然无法解释“this”一词:
//works
function Cat() {
this.theCatName = "Mistigri";
function meow() {alert(this.theCatName + " meow") };
this.meow = meow }
}
var MyCat = new Cat()
MyCat.meow()
//works
function Cat() {
this.theCatName = "Mistigri";
function meow() {alert(this.theCatName + " meow") };
this.meow = function() { alert(this.theCatName + " meow") }
}
var MyCat = new Cat()
MyCat.meow()
//works
function Cat() {
this.theCatName = "Mistigri";
function meow() {alert(this.theCatName + " meow") };
Cat.prototype.meow = function() { alert(this.theCatName + " meow") }
}
var MyCat = new Cat()
MyCat.meow()
//doesn't work
function Cat() {
this.theCatName = "Mistigri";
function meow() {alert(this.theCatName + " meow") };
this.meow = function() { meow() } }
}
var MyCat = new Cat()
MyCat.meow()
//doesn't work
function Cat() {
this.theCatName = "Mistigri";
function meow() {alert(this.theCatName + " meow") };
Cat.prototype.meow = function() { meow() } }
}
var MyCat = new Cat()
MyCat.meow()
现在我的理解是,在后两种情况下,Cat.prototype.meow
或this.meow
是碰巧调用meow()的匿名函数,meow()是Cat()
的一个内部函数,但是this
的上下文显然指函数内部的Cat-它发生了什么
下面是一个半标准答案:
但关于“这”的实际上下文,它只能说以下几点:
这(也称为“上下文”)是每个函数和
它的值仅取决于函数的调用方式,而不是
定义的方式/时间/地点。它不受词汇范围的影响,
和其他变量一样
当您调用对象的方法时,仅当您将其作为对象的成员调用时,如果您获取对函数的引用或将其作为常规函数调用时,该方法才有效,上下文不是对象。只有调用函数的方式才能决定上下文,上下文不是继承的,因此如果从方法调用函数,它只是一个常规函数调用 例如:
function Dog() {
function bark() {}
this.bark = bark;
this.barkThrice() {
bark(); // function call; context = window
this.bark(); // method call; context = the object
var b = this.bark;
b(); // function call; context = window
}
}
要将函数作为对象的方法调用,需要使用(或bind
或apply
)设置调用的上下文:
function Cat() {
this.theCatName = "Mistigri";
function meow() { alert(this.theCatName + " meow"); }
this.meow = function() { meow.call(this); };
}
var MyCat = new Cat();
MyCat.meow();
演示:我希望这会有所帮助: 函数内声明的变量在该函数外不可访问。 函数中定义的变量可供其嵌套函数访问
function Cat() {
// public var exist on the Cat instance: accesible from outside the Cat constructor
this.theCatName = "Mistigri";
// public function exist on the Cat instance
// has access to all variables defined in the Cat constructor (_theCateName and _meow)
// and all variables and methods defined on 'this'
// this reference to a Cat Instance
this.meow = function() {
alert(this.theCatName);
_meow();
}
// private var only accessible within the Cat constructor.
var _theCateName = "_Mistigri"
// private function only accessible within the Cat constructor.
function _meow() {
// _meow is defined as closure in the Cat constructor
// In the function _meow 'this' is a reference to the scope from where the Cat function / constructor was applied (the window object in this case)
alert(this.theCatName + " meow " + _theCateName); // outputs: undefined meow _Mistigri
};
}
var MyCat = new Cat()
MyCat.meow()
alert (MyCat.theCatName)// outouts: Mistigri
编辑:我知道如何修复它,但我不知道“这”的上下文中实际发生了什么。meow()是Cat()的一个函数,由Cat()的另一个函数调用,这两个函数的上下文中都有这个。catname?那么,“this”在找不到密码时试图引用什么?@user3467349:函数
meow
不是对象的成员,它是Cat
函数中的一个函数。Javascript没有类作用域,其中所有内容都是类的一部分。