将范围变量添加到构造函数 [英] Adding scope variable to a constructor

查看:92
本文介绍了将范围变量添加到构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在javascript上创建类似体系结构的类,但遇到了麻烦. 这是代码:

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);
    }
};

我试图将priv变量添加到cons.prototype的返回函数对象的作用域和对象作用域上,但我做不到;

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;

以下是make对象的用法:

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:好吧,我使用外部变量只是为了演示我想做的事情.我知道javascript函数结构的私有变量语法.但是我需要一种解决方案来更改(添加私有变量)将要由新"(我忘记了模式名称)实例化模式使用的函数的范围.

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:请原谅我的英语...

PS: Please forgive my english...

推荐答案

如果您追求的是类结构,则应该遵循以下模式:

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

function MyClass(arg){};

重要提示:每当将内部闭包的var名称设置为与包含外部闭包的var名称相同时,您就不再可以访问具有相同名称的原始外部闭包vars.

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.

另一方面,如果需要访问外部闭包var,则无需将内部闭包var设置为相同的名称.只需使用var,就好像它们在内部闭包中一样.

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;
    }
};

// 如果您需要使用名称cons:将var或private变量添加到功能对象中,请按照下面的说明进行操作,就像其他功能对象一样添加它.

// 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();

这篇关于将范围变量添加到构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆