Tag: 范围

JavaScript中variables的范围是什么?

什么是JavaScript的variables的范围? 他们有内部相同的范围,而不是外部function? 还是它甚至重要? 另外,如果全局定义variables存储在哪里?

请解释在循环中使用JavaScript闭包

我已经阅读了一些关于循环内部闭包和闭包的解释。 我很难理解这个概念。 我有这样的代码:是否有办法尽可能地减少代码,这样可以使闭包的概念更清晰。 我很难理解i在两个括号内的部分。 谢谢 function addLinks () { for (var i=0, link; i<5; i++) { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function (num) { return function () { alert(num); }; }(i); document.body.appendChild(link); } } window.onload = addLinks;

如果你总是喜欢xrange()超过范围()?

为什么或者为什么不?

var self =这个?

使用实例方法作为事件处理程序的回调函数将其范围从“我的实例”更改为“无论何时只是称为回调” 。 所以我的代码看起来像这样 function MyObject() { this.doSomething = function() { … } var self = this $('#foobar').bind('click', function(){ self.doSomethng() // this.doSomething() would not work here }) } 它的工作原理,但这是最好的办法吗? 我看起来很奇怪

在JavaScript原型函数中保留对“this”的引用

我只是开始使用原型JavaScript,我无法弄清楚如何在范围更改时从原型函数内部保留对主对象的引用。 让我来说明我的意思(我在这里使用jQuery): MyClass = function() { this.element = $('#element'); this.myValue = 'something'; // some more code } MyClass.prototype.myfunc = function() { // at this point, "this" refers to the instance of MyClass this.element.click(function() { // at this point, "this" refers to the DOM element // but what if I want to access the original "this.myValue"? […]