js优化-prototype原型的使用

时间:2022-08-24 22:16:41

var builder = function (id) {
this.init($(id));
this.forms = [];

/*
this.init = function ()//写在内部,每个实例都会分配一个function,如果又存在大量的DOM存取,操作开销就大了.
{
var $row = $("<div class='row'>").appendTo($(id).addClass("container-fluid"));
}
this.addControl = function (ctrl) {

}
this.xxxxxx = function(){ }
*/

}

builder.prototype.addControl = function (ctrl) {//原型,每个实例都共享一个原型。
this.$content.append(ctrl.view);
var self = this;
ctrl.on('click', function () {
var view = this.editor.render(this.model);
self.$editor.html('').append(view);
});
this.forms.push(ctrl);
}

builder.prototype.getData = function () {
return this.forms;
}

builder.prototype.init = function ($dom) {
var $row = $("<div class='row'>").appendTo($dom.addClass("container-fluid"));
var $toolBox = $("<div> class='col-md-3'").appendTo($row);
this.$content = $("<div class='col-md-6'>").appendTo($row);
this.$editor = $("<div class='col-md-3'>").appendTo($row);
};

return builder;