在JavaScript方法之间传递信息

时间:2015-07-24 01:08:49

标签: javascript prototype

我有一个对象,里面有两组不同的对象:

var myObj;
(function (myObj) {
    var myFunction = (function () {
        function myFunction(){
           this.myValue = "something";
        }
        myFunction.getValue = function () {
           var _this = this;

           return _this.myValue;
        }
        return myFunction;
    })();
    myObj.myFunction = myFunction;

    var myFunction2 = (function () {
        function myFunction2() {
        }
        myFunction2.prototype.something = function () {
            var a = myFunction.getValue();
        }
        return myFunction2;
    })();
    myObj.myFunction2 = myFunction2;        
})(myObj || (myObj = {}));

每次运行myFunction2.something()时,都会a分配undefined

如何将myFunction的值转换为myFunction2.something()

3 个答案:

答案 0 :(得分:2)

代码中有很多范围和设计模式。在另一个内部有两个以相同方式命名的函数会根据您调用它们的位置为该名称创建不同的范围。这很快就会失去控制。就像这部分一样:

var myFunction = (function () {  // this guy is named myFunction
    function myFunction(){ // this guy is also named myFunction
       this.myValue = "something"; // 'this' here most certainly refers to 'window', not myFunction. Unless you do a 'new myFunction()' a la prototype
    }
    myFunction.getValue = function () {
       var _this = this;// same here

       return _this.myValue;
    }
    return myFunction;// here you are returning a function, not an object with methods
})();

此外,我注意到你正在使用原型和其他带闭包的逻辑处理,当其他人(或者你在几个月内)需要重构这段代码时,这也有点令人困惑。

   myFunction.getValue = function () {
       var _this = this;

       return _this.myValue;
    }
    return myFunction;

你可以去所有的原型或所有的闭包。我更喜欢闭包,所以这就是我要做的。

var myObj;
(function (myObj) {
    // in this closure, we create a scope and return only the public methods
    var myFunction = (function () {
        // define properties here
        var myValue = 'default value';

        // define your methods, here we return
        // myValue declared on the parent scope
        function getValue() {
           return myValue;
        }

        // this acts as a constructor, it autoexecutes
        (function init(){
           myValue = "something";
        })();

        // return all the functions in this scope
        // you want to expose
        return {
            getValue: getValue
        };
    })();
    // we then put myFunction into myObj
    // at this point myFunction is an object with a method called getValue()
    myObj.myFunction = myFunction;

    var myFunction2 = (function () {
        function something() {
            // once this guy is called, we get the value from the other 'class'
            var a = myFunction.getValue();
            alert(a);// voila!
        }

        (function myFunction2() {
            // do your init stuff here
        })();
        return {
            something: something
        };
    })();
    myObj.myFunction2 = myFunction2;        
})(myObj || (myObj = {}));

// at this point myObj is an object with 2 'classes', each 'class' has its own methods
// we can proceed and call the desired method
myObj.myFunction2.something();

演示:http://jsfiddle.net/bzw9kse7/

答案 1 :(得分:0)

编辑抱歉,这一切都错了。它返回this,因为getValue是myFunction对象的静态方法,而不是实例方法。这意味着this与myFunction构造函数不同。

此外,您甚至没有将myValue附加到 .... var myFunction = (function () { function myFunction(){ this.myValue = "something"; } myFunction.prototype.getValue = function () { return this.myValue; } // this creates the instance of the myFunction class. return new myFunction(); })(); myObj.myFunction = myFunction; ... // now you should be able to see the right result var a = myFunction.getValue(); 任何地方......

编辑2 我将getMyValue添加到myFunction原型中。

试试这个:

var a = myFunction.getValue();

这是我见过的最疯狂的代码之一,但答案是:

undefinedfunction myFunction(){ var myValue = "something"; } ,因为myFunction的第二个定义返回undefined,如下所示:

var myFunction = (function () {

        // this definition replaces the one above
        function myFunction(){
           var myValue = "something";
        }
        myFunction.getValue = function () {
           var _this = this;

           return _this.myValue;
        }

        // which makes this return to not be taking in account when you are 
        // calling myFunction()
        return myFunction;
    })();

你对myFunction的第二个定义实际上取代了第一个。请参阅以下评论:

$categories = get_terms('portfolio_category', 'parent=80');

答案 2 :(得分:0)

我已经介入了你的代码,只是发现了一些你做错的小事。

你似乎稍微错误地定义了myFunction,因为它不会将myValue作为成员变量,只是一个很快超出范围的临时变量。 (因为它变得未定义,它被分配给a,然后被取消分配)。

所以,替换这些行:

var myFunction = (function () {
    function myFunction(){
        var myValue = "something";
    }
    myFunction.getValue = function () {
        var _this = this;
        return _this.myValue;
    }
    return myFunction;
})();

有了这个:

var myFunction = (function () {
    var myFunction = {};
    myFunction.myValue = "something";
    myFunction.getValue = function () {
       var _this = this;

       return _this.myValue;
    }
    return myFunction;
})();