具有模块模式的构造函数

时间:2016-07-28 20:41:41

标签: javascript

我尝试使用以下代码的模块模式:

(function(elements) {

    elements.Textbox = function() {
        this.properties = elements.defaultProperties.textboxProperties;
    }

    elements.Textbox.prototype = {
        setSize: function(size) {
            this.properties.size = size;
        },

        getProperties: function() {
            return this.properties;
        },
        getSize: function() {
            return this.properties.size;
        }
    }
}(app.elements || {}));

尝试创建和修改像这样的对象

var textbox1 = new app.elements.Textbox();
var textbox2 = new app.elements.Textbox();
textbox2.setSize({
    width: 300,
    height: 100
});

但是这会导致两个文本框以相同的高度和宽度结束。我是使用带有Constructor的Module模式的新手。如果我拿出文本框,然后执行

var textbox1 = new Textbox();
var textbox2 = new Textbox();
textbox2.setSize({
    width: 300,
    height: 100
});

这很好用。我对此有些误解,所以提前感谢您的帮助和澄清。

编辑:我已经查看了许多SO问题,包括:Javascript: Module Pattern vs Constructor/Prototype pattern? 他们都非常乐于助人。

0 个答案:

没有答案
相关问题