Adding scope variable to a constructor

2019-06-14 09:02发布

I'm trying to create a class like architecture on javascript but stuck on a point. Here is the code:

var make = function(args) {
    var priv = args.priv,
        cons = args.cons,
        pub = args.pub;
    return function(consParams) {

        var priv = priv,
            cons = args.cons;

        cons.prototype.constructor = cons;
        cons.prototype = $.extend({}, pub);

        if (!$.isFunction(cons)) {

            throw new Hata(100001);

        }
        return new cons(consParams);
    }
};

I'm trying to add the priv variable on the returned function objects's scope and object scope of the cons.prototype but I could not make it;

Here is the usage of the make object:

var myClass = make({
    cons: function() {
        alert(this.acik);
        alert(priv.gizli);
    },
    pub: {
        acik: 'acik'
    },
    priv: {
        gizli: 'gizli'
    }
})
myObj = myClass();

PS: Well I have used the outer vars for just to demonstrate what I want to do. I know the private variable syntax of javascript function structure. But I need a solutution for changing (adding private vars) the scope of a function which is going to be used by a "new" (i forgot the pattern name) instantiation pattern.

PS: Please forgive my english...

2条回答
祖国的老花朵
2楼-- · 2019-06-14 09:18

If your trying to replicate a class like structure the common approach in Javascript is as follows:

//Constructor
function MyClass(myParam){
  this.myParam = myParam
}

MyClass.prototype.myMethod = function(){
  alert(this.myParam);  //retreives the "class" instance variable myParam
}

//Instantiation
var firstMyClass = new MyClass(7);
firstMyClass.myMethod(); //alerts 7

Note that within the functions you add to the prototype this will be a reference to the MyClass instance. If you would like to make private variables you can checkout this link from Javascript Expert Douglas Crockford: http://javascript.crockford.com/private.html

查看更多
别忘想泡老子
3楼-- · 2019-06-14 09:27

If you're after the class structure you really should follow the pattern of:

function MyClass(arg){};

Important Note: When ever you set your var names of the inner closure to the same name as it's containing outer closure you no longer have access to the original outer closures vars with the same name.

On the other hand, if you need to access the outer closures vars there is no need to set the inner closures vars with the same name. Just work with the vars as if they're with-in the inner closure.

var make = function(args) {
    var priv = args.priv,
        cons = args.cons,
        pub = args.pub;
    return function(consParams) {

        var someThing = priv,
            sElse     = args.cons;
    }
};

// If you need to add a var or private variable to a function object with the name cons: as you've got it below just add it like any other function object.

var myClass = make({
    cons: function() {
         var myPrivateVariable = 'private';
        alert(this.acik);
        alert(priv.gizli);
    },
    pub: {
        acik: 'acik'
    },
    priv: {
        gizli: 'gizli'
    }
})
myObj = myClass();
查看更多
登录 后发表回答