JavaScript中'this'关键字行为的基本原理是什么? [英] What is the rationale for the behavior of the 'this' keyword in JavaScript?
问题描述
我从语言设计的角度来问这个问题。所以我试图找出
-
行为的理由是什么
? -
这个
的行为在多大程度上是错误的,或者可以改进?
澄清为什么我对这个
感到不安,考虑这个例子:
var a = {};
a.f = function(){return this; }
var f = a.f;
// f()!= af()
注意对象是多么容易 f()
属于遗失:与分开
,此
成为浏览器的全局对象(窗口
)。现在考虑:
var newA = function(){
var self = {};
self.f = function(){return self; }
返回自我;
}
var a = newA();
var f = a.f;
// f()== a.f()!
根本不使用这个
,我们是无论方法在何处或如何使用,都能够建立和维护对象上下文。我不禁想到,凭借闭包提供的力量,这个
变得多余,甚至可能有点危险......
我没有针对这个
的某些仇杀,或者想要开始争论;我只是想更好地理解它。我很欣赏这个很有用,但要认识到它也可能令人困惑 ......当然会让初学者感到困惑,也许对专家来说也会让人感到困惑。
然而,它在该语言的其他核心方面似乎是公平的避免游戏(即Crockford和与
)时,它仍然是一种使用频繁,看似备受尊重的语言部分。 新
)。我错过了什么,这使得这个
必不可少?
你似乎期望这种行为与某些OO语言中的行为一样,它总是指方法所属的对象。
但是在JavaScript中,函数可以附加到多个对象,或者根本不附加任何对象。在您的示例中,您已经编写了一个旨在用于一个特定对象的上下文中的函数...但是没有什么能阻止我使用该函数并将其附加到任何其他对象。这只是语言的本质 - 函数是一流的,对象成员资格是可选的。
因此, this 指的是调用函数的上下文。现在,这是一个任意对象(通过。
, .apply
或指定)。 call()
)或全局对象。在该语言的未来版本中,它将引用定义函数的上下文:全局函数的全局对象,内部函数的外部 this
;您可以将其视为设计缺陷的修正,因为实际上能够使用它来引用全局对象并不是特别有用。
I am asking this from a language design point of view. So I am trying to find out
- What is the rationale for the behavior of
this
? - To what degree the behavior of
this
was a mistake, or could be improved upon?
To clarify why I'm uneasy about this
, consider this example:
var a = {};
a.f = function(){ return this; }
var f = a.f;
// f() != a.f()
Note how easily the object to which f()
belong is lost: separated from a
, this
becomes the global object (window
for browsers).
Now consider:
var newA = function(){
var self = {};
self.f = function(){ return self; }
return self;
}
var a = newA();
var f = a.f;
// f() == a.f() !
Without using this
at all, we are able to establish and maintain the object context regardless of where or how the method is used. I can't help but think that, with the power that closures provide, this
becomes superfluous, and perhaps even a little dangerous...
I'm not on some vendetta against this
, or looking to start an argument; I'm merely trying to better understand it. I do appreciate that 'this' can be useful, but recognize that it can be confusing as well... Certainly confusing to beginners, and perhaps to experts as well in sufficiently obscure cases.
And yet, it remains a heavily-used and seemingly well-respected part of the language, in a time when other core aspects of the language seem fair game for shunning (i.e., Crockford and with
or new
). What am I missing then, that makes this
indispensable?
You seem to be expecting this to behave as it does in certain OO languages, where it always refers to the object a method belongs to.
But in JavaScript, a function can be attached to multiple objects, or no object at all. In your example, you've written a function intended to be used in the context of one specific object... But nothing prevents me from taking that function and attaching it to any other object. That's just the nature of the language - functions are first-class, object membership is optional.
Therefore, this refers to the context in which a function is called. Right now, that's either an arbitrary object (specified via .
, .apply
, or .call()
) or the global object. In future versions of the language, it will refer to the context in which the function was defined: the global object for global functions, the outer this
for inner functions; you can view this as a correction of a design flaw, as in practice being able to refer to the global object using this was not particularly useful.
这篇关于JavaScript中'this'关键字行为的基本原理是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!