开发者

how can I get to the methods in the "parent"?

开发者 https://www.devze.com 2023-03-06 21:11 出处:网络
I have this object. fieldsCreator\'s members hold creator methods for each field. My question is how can I call the creator method inside fieldsCreators as described below:

I have this object.

fieldsCreator's members hold creator methods for each field.

My question is how can I call the creator method inside fieldsCreators as described below:

var obj={
    creator:function(ch) {
        ....
        ....
        ....
    },
    fieldsCreators:{
            INVITER: function () {
                return creator('a'); //HOW DO I CALL creator METHOD?
            },
            CUSTOMER: function () {
                return creator('b'); //HOW DO I CALL creator METHOD?
            }开发者_如何学JAVA
    }
}


You can also use a pattern like this:

var obj = (function() {

    var methods = {
        creator: function(ch) {

        },
        fieldsCreators:{
            INVITER: function () {
                return methods.creator('a');
            },
            CUSTOMER: function () {
                return methods.creator('b');
            }
        }
    };

    return methods;
})();

How is this useful? Let's say you wanted to give all your methods access to a variable, but only visible inside of obj. You couldn't do this if obj was just an object, it needs a function's scope. For example:

var obj = (function() {

    // everything declared with var won't be visible to the rest
    // of the application, which is good encapsulation practice.
    // You only return what you want to be publicly exposed.
    var MYPRIVATEINFO = "cheese";

    var methods = {
        creator: function(ch) {
            // you have access to MYPRIVATEINFO here
            // but the rest of the application doesn't
            // even know about it.    
        },
        fieldsCreators:{
            INVITER: function () {
                return methods.creator('a');
            },
            CUSTOMER: function () {
                return methods.creator('b');
            }
        }
    };

    return methods;
})();

If you don't want to have to name your object, you COULD structure it like this, which is why having a private environment is key:

var obj = (function() {

    var creator = function(ch) {

    };


    return {
        // if you wish, you can still expose the creator method to the public:
        creator: creator,
        fieldsCreators: {
            INVITER: function () {
                return creator('a');
            },
            CUSTOMER: function () {
                return creator('b');
            }
        }
    };
})();


Does obj.creator(...) not work?

You are defining an instance called 'obj' so you should be able to use that name.

EDIT-- This is probably what you want -

var obj={
    creator:function(ch) {
        ....
        ....
        ....
    },
    self : this,
    fieldsCreators:{
            INVITER: function () {
                return self.creator('a'); //HOW DO I CALL creator METHOD?
            },
            CUSTOMER: function () {
                return self.creator('b'); //HOW DO I CALL creator METHOD?
            }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消