为什么'这个'决议在JavaScript中如此特别? [英] Why 'this' resolution is so special in JavaScript?
问题描述
// 1:马车计数器
// ---- -------------
//问题:'this'可以被破坏
var Counter1 = function(){
this.count = 0;
this.increment = function(){
function increment_helper(){
++ this.count; //这是指窗口(全局),而不是Counter1被
调用的对象
increment_helper();
}
this.reset = function(){this.count = 0; }
this.get_count = function(){return this.count; }
}
var counter1 = new Counter1();
counter1.increment();
document.writeln(< p> 1:+ counter1.get_count()+< / p>);
可以看出, this
在 increment_helper
中引用全局作用域(或者如果我已经明白了,则指向
lockquote
$ / $ $
$ b
而不是
<1>
<我会列出更好的(仍然不完美,但没关系)解决方案,然后会问主要问题。所以,解决方案(恕我直言)可以这样排序:
// 2:更好的计数器
// -----------------
//解析:'that'不能被破坏
var Counter2 = function(){
var that = this;
that.count = 0;
that.increment = function(){
function increment_helper(){
++ that.count; //引用该对象的权利Counter1被调用
}
increment_helper();
}
that.reset = function(){that.count = 0; }
that.get_count = function(){return that.count; }
}
var counter2 = new Counter2();
counter2.increment();
document.writeln(< p> 2:+ counter2.get_count()+< / p>);
所以,主要问题是:为什么这个
在JavaScript中独一无二?为什么它只是这个
和用于什么目的?
非常感谢!
更新:下面是@ Jordan的评论(他解释了相对变量之间的差异,比如这个
和范围变量和常规范围变量一样),重新描述我的问题:
答:相对变量?
B:为什么这个
是JavaScript中唯一的相对变量?关于为什么相对/范围概念可以(专门)应用于每个变量的任何理论基础? 解决方案
因此,作为一个愚蠢的例子:
var o = {
name:My name is o,
foo:function(){alert(this.name); }
};
var p = {
name:我的名字是p,
};
p.foo = o.foo;
p.foo(); //我的名字是p
在其他OO C语言中无法完成,但它可以在JavaScript中。因此这个
必须是相对的。是的,它会导致问题,但语言变得令人难以置信与它表达。
B。
参数
也是相对的。 Warning: Buggy JavaScript code first!
// 1: buggy counter
// -----------------
// problem: 'this' can be "broken"
var Counter1 = function() {
this.count = 0;
this.increment = function() {
function increment_helper(){
++this.count; // this refers to window (global), not to the object that Counter1 was called with
}
increment_helper();
}
this.reset = function() { this.count = 0; }
this.get_count = function() { return this.count; }
}
var counter1 = new Counter1();
counter1.increment();
document.writeln("<p> 1: " + counter1.get_count() + "</p>");
As it can be seen, the this
in increment_helper
rather refers to the global scope (or to window
scope to be precise IF I've got it right) instead of referring to the this of the enclosing closure. So that, the output will be:
0
instead of
1
I'll list the better (still not perfect, but okay) solution and then will ask the main question. So, the solution (IMHO) can be sorted out like this:
// 2: better counter
// -----------------
// solved: 'that' can't be "broken"
var Counter2 = function() {
var that = this;
that.count = 0;
that.increment = function() {
function increment_helper(){
++that.count; // that refers right to the object the Counter1 was called with
}
increment_helper();
}
that.reset = function() { that.count = 0; }
that.get_count = function() { return that.count; }
}
var counter2 = new Counter2();
counter2.increment();
document.writeln("<p> 2: " + counter2.get_count() + "</p>");
So, the main question is: why this
is special like noone else in JavaScript? Why it's only this
and for what purpose?
Thanks a lot!
Update: Following @Jordan's comment below (where he explained the difference between relative variables like this
and scoped variables like regular ones), rephrasing my questions:
A: what is the general rationale behind relative variables?
B: why this
is the only relative variable in JavaScript? Any rationale on why the relative/scoped concept can be (exclusively) applied to every variable?
A. In JavaScript, unlike in other C-like OO languages, functions are first class objects. You can copy functions around, pass them into other functions, return them from functions, create new ones. Functions are not like methods in Java or C# that are permanently affixed to the class declaration. This means that this
has to be relative to the function itself and how it was called. It doesn't make sense otherwise.
So as a silly example:
var o = {
name: "My name is o",
foo: function () { alert(this.name); }
};
var p = {
name: "My name is p",
};
p.foo = o.foo;
p.foo(); // "My name is p"
That can't be done in other OO C-type languages, but it can in JavaScript. Hence this
has to be relative. Yes, it causes problems, but the language becomes incredibly expressive with it.
B. arguments
is also relative.
这篇关于为什么'这个'决议在JavaScript中如此特别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!