代码之家  ›  专栏  ›  技术社区  ›  prettyInPink

在其他方法中的方法中创建的访问变量

  •  4
  • prettyInPink  · 技术社区  · 10 年前

    我一次又一次地为同一个话题疯狂,可变范围。 为此创建一个全局变量不起作用,我尝试在一个函数中返回值以使用另一个函数,但总是在控制台中返回“undefined”错误。 这是代码的简单标记:

    domElement.plugin({
        method1: function() {
            // create variable
            var myvar = 1;
            // other things going on here in this method
        },
        method2: function() {
            // use variable
            console.log(myvar);
        }
    });
    

    这是一个jquery插件,我试图访问在method2内部method1中创建的var myvar的值(并且是变量)。 我在论坛上发现了一些东西,但似乎无法使其中任何一个发挥作用,因此任何帮助都是非常感谢的。

    4 回复  |  直到 10 年前
        1
  •  3
  •   Rory McCrossan Hsm Sharique Hasan    10 年前

    您需要使两种方法都可以访问变量范围:

    domElement.plugin({
        myvar: null, 
        method1: function() {
            this.myvar = 1;
        },
        method2: function() {
            console.log(this.myvar); // = null or 1 depending on when you call this function
        }
    });
    

    或者您可以将它从一个方法传递到另一个方法:

    domElement.plugin({
        method1: function() {
            var myvar = 1;
            this.method2(myvar);
        },
        method2: function(v) {
            console.log(v); // = 1
        }
    });
    

    我个人会用第一个例子。

        2
  •  3
  •   millerbr    10 年前

    你不能,它将不再在范围内。您需要在该范围之外定义变量,并在函数内部赋值。

    domElement.plugin({
        myvar:null,
        method1: function() {
            // create variable
            myvar = 1;
            // other things going on here in this method
        },
        method2: function() {
            // use variable
            console.log(myvar);
        }
    });
    

    在另一条评论中提到,您可以在一个方法中定义变量,然后将其传递给另一个方法,但这只在您计划只使用第一个方法中的第二个方法时有效。该变量仍然无法从方法外部访问。

        3
  •  2
  •   Julien Leray    10 年前

    两种最简单的方法:

    全球范围:

    var myvar;
    domElement.plugin({
        method1: function() {
            // create variable
            myvar = 1;
            // other things going on here in this method
        },
        method2: function() {
            // use variable
            console.log(myvar);
        }
    });
    

    不要过多地使用全局范围,它有点混乱;更好地使用对象:

    对象属性:

    domElement.plugin({
        myVar: 1,
        method1: function() {
            // create variable
            this.myVar = 1;
            // other things going on here in this method
        },
        method2: function() {
            // use variable
            console.log(this.myVar);
        }
    });
    

    我鼓励你使用第二种方法。

        4
  •  1
  •   Billy Moon    10 年前

    这可能看起来有点冗长,但您可以通过使用立即调用的函数表达式为您的方法创建一个共享范围。。。

    // create shared scope for methods
    var methods = (function() {
        // create variable in shared context
        var myvar;
        // define method1
        function method1() {
            // assign value to variable
            myvar = 1;
            // other things going on here in this method
        }
        // define method2
        function method2() {
            // use variable
            console.log(myvar);
        }
        // return methods
        return {
            method1: method1,
            method2: method2
        }
    // execute the scope function immediately...
    }());
    
    // assign methods to plugin
    domElement.plugin({
        method1: methods.method1,
        method2: methods.method2
    });
    

    这是因为javascript是函数范围的,而不是块范围的,并且在两个方法都可以访问的父范围中声明变量意味着它们都将对同一个变量进行操作。此外,变量对于范围的上下文仍然是私有的,因此不能从外部访问和修改。这种模式有助于管理任意模块,这些模块可以相互共存而不会发生冲突。