好吧,我知道关于Javascript中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(),这是Cat()的内部函数--但是this的上下文明确地指函数中的cat --它会发生什么呢?
以下是一个半规范的答案:See How to access the correct this / context inside a callback?,但它只想说明'this‘的上下文实际上是什么:
这个(又称“上下文”)是每个函数中的一个特殊关键字,其值仅取决于函数是如何调用的,而不是如何/何时/在何处定义的。与其他变量一样,它不受词法作用域的影响。
发布于 2014-06-22 18:24:27
当您调用一个对象的方法时,它只在您作为对象的一个成员调用它时才能工作,如果您获得对该函数的引用或将它作为一个常规函数调用,则上下文不是对象。决定上下文的仅仅是调用函数的方式,上下文并不是继承的,所以如果从方法调用函数,那么它只是一个常规函数调用。
示例:
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
}
}要将函数作为对象的方法调用,需要使用 method (或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();演示:http://jsfiddle.net/k6W9K/
发布于 2014-06-22 19:14:38
我希望这会有所帮助:在函数中声明的变量在该函数之外是不可访问的。函数中定义的变量可由其嵌套函数访问。
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: Mistigrihttps://stackoverflow.com/questions/24354122
复制相似问题