JavaScript、MooTools - 类中的变量范围/覆盖全局变量

发布于 2024-12-05 13:44:02 字数 1524 浏览 1 评论 0原文

有人可以解释一下,为什么我能够通过在本地设置全局实例的值来覆盖全局实例的方法值,以及为什么我不能对变量执行类似的操作?

访问变量的唯一方法是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守望孤独 2024-12-12 13:44:02

这与变量范围有关。
JavaScript 有函数范围。

因此,通过这样做:

var someVar = this.someVar = false;

您声明一个局部变量 someVar 和一个全局变量(它被提升到 window 对象,即 window.someVar),因为闭包中的 this 引用全局范围,即 window。

因此,当您编写:

someVar = true;

您正在用这个新值覆盖局部变量。

如果使用 var 关键字,则在函数定义中声明的变量对于该函数来说是局部的:

(function () {
   var name = 'Mark';
})();
// Out here you cannot access name
console.log(name);

This is to do with variable scope.
Javascript has functional scope.

So by doing:

var someVar = this.someVar = false;

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:

someVar = true;

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:

(function () {
   var name = 'Mark';
})();
// Out here you cannot access name
console.log(name);
三人与歌 2024-12-12 13:44:02

(如果我正确理解了这个问题)

它与 Mootools 或类无关,@Felix_Kling 已经给了你答案,但我会用一个简单的例子来说明它:

var aObj = bObj = {}; //since bObj is an 'object', aObj will store the objects reference 
aObj.foo = "bar";
console.log(aObj.foo);
console.log(bObj.foo);
// output:
//     "bar"
//     "bar"


var a = b = 1; //since 'b' is primitive, 'a' will not store a reference of 'b', it will only copy it's value
a = 0;
console.log(a);
console.log(b);
// output:
//     0
//     1

我不太确定这是否是你要问的= )希望这有帮助

(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:

var aObj = bObj = {}; //since bObj is an 'object', aObj will store the objects reference 
aObj.foo = "bar";
console.log(aObj.foo);
console.log(bObj.foo);
// output:
//     "bar"
//     "bar"


var a = b = 1; //since 'b' is primitive, 'a' will not store a reference of 'b', it will only copy it's value
a = 0;
console.log(a);
console.log(b);
// output:
//     0
//     1

i'm not really sure if this is what you were asking =) Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文