JavaScript、MooTools - 类中的变量范围/覆盖全局变量
有人可以解释一下,为什么我能够通过在本地设置全局实例的值来覆盖全局实例的方法值,以及为什么我不能对变量执行类似的操作?
访问变量的唯一方法是使用 window
对象层次结构吗?或者有没有更短的方法?
(function() {
console.log(this);
var someVar = this.someVar = false;
var subClass = new Class({
test: false,
setValue: function(value) {
this.test = value
}
});
var subPub = this.subPub = new subClass();
var MainClass = new Class({
rewriteVar: function() {
console.log("someVar = " + someVar); // returns global value
console.log("subPub.test = " + subPub.test); // returns global value
someVar = true;
console.log("someVar local: " + someVar); // returns new local value
console.log("someVar global: " + window.someVar); // returns old global value
subPub.setValue(true);
console.log("subPub.test local: " + subPub.test); // returns new local value
console.log("subPub.test global: " + window.subPub.test) // returns new global value
}
});
/* var someObj = this.someObj = {};
var someVar = someObj.someMeth = false;
// And why is this possible?
var MainClass = new Class({
rewriteVar: function() {
someObj.someMeth = true;
console.log(window.someObj.someMeth); // returns new global value
}
}); */
window.addEvent("load", function() {
var test = new MainClass();
test.rewriteVar()
})
})()
Can somebody explain me, why I am able to overwrite a method value of a global instance by just setting its value locally and why I am not able to do something similar with variables?
Is the only way to access the variable to use the window
object hierarchy? Or is there maybe a shorter way?
(function() {
console.log(this);
var someVar = this.someVar = false;
var subClass = new Class({
test: false,
setValue: function(value) {
this.test = value
}
});
var subPub = this.subPub = new subClass();
var MainClass = new Class({
rewriteVar: function() {
console.log("someVar = " + someVar); // returns global value
console.log("subPub.test = " + subPub.test); // returns global value
someVar = true;
console.log("someVar local: " + someVar); // returns new local value
console.log("someVar global: " + window.someVar); // returns old global value
subPub.setValue(true);
console.log("subPub.test local: " + subPub.test); // returns new local value
console.log("subPub.test global: " + window.subPub.test) // returns new global value
}
});
/* var someObj = this.someObj = {};
var someVar = someObj.someMeth = false;
// And why is this possible?
var MainClass = new Class({
rewriteVar: function() {
someObj.someMeth = true;
console.log(window.someObj.someMeth); // returns new global value
}
}); */
window.addEvent("load", function() {
var test = new MainClass();
test.rewriteVar()
})
})()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与变量范围有关。
JavaScript 有函数范围。
因此,通过这样做:
您声明一个局部变量 someVar 和一个全局变量(它被提升到 window 对象,即 window.someVar),因为闭包中的 this 引用全局范围,即 window。
因此,当您编写:
您正在用这个新值覆盖局部变量。
如果使用 var 关键字,则在函数定义中声明的变量对于该函数来说是局部的:
This is to do with variable scope.
Javascript has functional scope.
So by doing:
You are declaring a local variable someVar and a global variable (which gets hoisted to the window object ie window.someVar), as this within your closure referes to the global scope ie window.
So when you write:
You are overwriting the local variable with this new value.
Variables declared within a function definition are local to that function if you use the var key word:
(如果我正确理解了这个问题)
它与 Mootools 或类无关,@Felix_Kling 已经给了你答案,但我会用一个简单的例子来说明它:
我不太确定这是否是你要问的= )希望这有帮助
(if i understood correctly the problem)
It has nothing to do with Mootools or the clases, @Felix_Kling already gave you the answer but I will ilustrate it with a simple example:
i'm not really sure if this is what you were asking =) Hope this helps