TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

JavaScript: what is "this"?

96 点作者 juvenn大约 15 年前

9 条评论

WilliamLP大约 15 年前
If anyone wonders why every JavaScript framework under the sun rolls its own object system, and they're all incompatible, and almost nobody uses native JavaScript alone anymore, this is one of the reasons.
评论 #1208750 未加载
评论 #1208467 未加载
评论 #1208693 未加载
评论 #1208560 未加载
tumult大约 15 年前
<i>It's not entirely our fault, the language was designed to work like one thing (scheme-like), but look like another (c-like).</i><p>The baseless assertion rears its head again. People keep repeating this, and it's not true. JavaScript is not Scheme. It's not any more Scheme-like than Java, C++, Python, Perl, or many other languages. Lua and Ruby are far more Scheme-like than JavaScript.<p>I've commented before on people repeating this statement without ever examining it. I don't want to dredge up the arguments against it again ( <a href="http://news.ycombinator.com/item?id=1171202" rel="nofollow">http://news.ycombinator.com/item?id=1171202</a> ), mostly I just want to whine at it because it's so annoying. Why do you have to make JavaScript seem like something it's not? Are you trying to seem cooler by equating the language you work in to Scheme? What's wrong with JavaScript just being what it is?
评论 #1208312 未加载
评论 #1208333 未加载
评论 #1208449 未加载
评论 #1208391 未加载
评论 #1208768 未加载
rriepe大约 15 年前
I had a lot of trouble with this as a newbie to Javascript. Glad to see it posted, even if it's months and months late for me.
sesqu大约 15 年前
<i>The var statement declares a variable as local to the current scope and the entire current scope, not just from the var statement onward. These local variables shadow any existing variables from outer scopes.</i><p>Err, I tested this on Chrome's Inspector console, and I don't see such behaviour. The article says <i>name</i> should be undefined at the time of the comparison in the below code, but it isn't (and I frankly can't see why anyone would want it to be). Is this a feature of my execution environment, or have I just completely misunderstood what the author means?<p><pre><code> (function(name){var cmp=name=="tim"; var name; return cmp})("tim") // returns true</code></pre>
评论 #1210302 未加载
评论 #1210352 未加载
icode大约 15 年前
Honestly, I think this is all too complicated. In Javascript, when you look at a line like this:<p><pre><code> a=1 </code></pre> you have to guess in which scope it changes or creates the variable a. I prefer the way PHP is dealing with scope. You simply know that "a=1" is only affecting the current scope.
评论 #1208438 未加载
btipling大约 15 年前
This kind of a bind is available in dojo via dojo.hitch and in google closure via goog.bind (make sure to require goog.base)<p>There's a $.hitch plugin in JQuery, it doesn't come with it by default.
endlessvoid94大约 15 年前
<a href="http://justin.harmonize.fm/index.php/2009/09/an-introduction-to-javascripts-this/" rel="nofollow">http://justin.harmonize.fm/index.php/2009/09/an-introduction...</a>
erlanger大约 15 年前
I feel it's worth noting that JavaScript developers refer to "this" as the <i>execution context.</i>
nailer大约 15 年前
It's this.
评论 #1208856 未加载