如何在javascript中创建构造函数创建构造函数?

时间:2015-05-18 13:48:24

标签: javascript constructor

有时在JavaScript中我需要很多伪类的构造函数和对象,因为我非常喜欢客观,所以我做了类似的事情:

var anyClass = (function (settings) {
    var staticPrivate = {}, staticPublic = function () {
        var public = this, private = {};
        (function constructor (here, the, args) {
            this.hta = [here, the, args, arguments];
        }).apply(this, arguments);
        arguments = undefined;delete arguments;
        private.stuff = function () {}
        Object.defineProperties(public, {
            "e.g. length": {
                get: function () {
                    return private.length;
                }, 
                set: function (newValue) {
                    return;
                }, 
                enumerable: false
            }
        });
    };
    Object.defineProperties(staticPublic, {
        "staticFinalHiddenString": {
            get: function () {
                return "YEAH, I'm static and final and hidden...";
            }, 
            set: function (newValue) {
                return "You cannot set me.. :P";
            }, 
            enumerable: false
        }
    });
    staticPrivate.some = function (init) {
        if (settings.some == "settings") init();
    }
    window.requestAnimationFrame(function () {
        staticPrivate.some(function (I) {
            run(on, first, render);
        });
    });
    return staticPublic;
})({
    some: "settings", 
    here: null
});

每次都这样,所以现在我想要一个为我创建新类的构造函数。我想这个:

new Class({
    constructor: function (here) {
        is(my + constructor);
    }, 
    properties: {
        name: {
            getter: function () {}, 
            setter: function (newValue) {}, 
            hidden: false, 
            static: false, 
            final: false
        }, 
        version: {
            getter: function () {
                return 0.3;
            }, 
            setter: function (newValue) {}, 
            hidden: true, 
            static: true, 
            final: true
        }
    }
});

但我的问题是我不知道如何使用构造函数类创建原型/构造函数.prototype.prototype不起作用。

我刚尝试过:

var Class = (function () {
    var Class = (function () {
        var constructor = function () {
            return (function (information) {
                this.prototype = {};
                var properties = {};
                for(var key in information.properties) {
                    properties[key] = {
                        get: information.properties[key].getter, 
                        set: information.properties[key].setter, 
                        enumerable: !information.properties[key].hidden || true
                    };
                };
                Object.defineProperties(this.prototype, properties);
                return this;
            }).apply(this, arguments);
        };
        return constructor;
    })();
    return Class;
})();

这对我不起作用:C

我希望你能帮助我。感谢...

1 个答案:

答案 0 :(得分:0)

我理解了我的错误,现在当我有一个构造函数时,我可以返回一些东西。

封闭函数中的var Class = function () {};Class.prototype.apply(this, arguments)有关,这就是为什么我可以在构造函数中返回Class,如果我愿意的话做

var Class = function () {
    var ClassICreate = function () {};
    ...
    return ClassICreat;
}

它不起作用,因为你无法从构造函数返回,因为它是一个对象。

这就是我的工作方式:

var Class = (function () {
    var Class = function () {
        return (function (information) {
            var Class = function () {};
            var properties = {};
            for(var key in information.properties) {
                properties[key] = {
                    get: information.properties[key].getter, 
                    set: information.properties[key].setter, 
                    enumerable: !information.properties[key].hidden || true
                };
            };
            Object.defineProperties(Class.prototype, properties);
            return Class;
        }).apply(this, arguments);
    };
    return Class;
})();

在我找到答案之后,我觉得这么容易,感谢您的评论,他们帮助我找到了正确的答案......