(function() {
if (!window.project) {
window.project = {};
}
window.project = {
init: function() {
alert("test");
},
show: function() {
alert("test2");
}
};
})();
调用:project.init();
自己的修改
(function() {
if (!window.project) {
window.project = {};
}
window.project = {
init: function() {
alert("test");
},
show: function() {
alert("test2");
}
};
})(jQuery);
elfinder插件就是这么封装的。地址:
https://github.com/Studio-42/elFinder
/////////////////////////////////////////////////////////////////////////////
function Range() {}
Range.prototype = {
init: function() {
alert("XXX");
},
show: function() {
alert("YYY");
}
};
function RangeChildren() {}
RangeChildren.prototype = Range.prototype;
RangeChildren.prototype.add = function() {
alert("ADD");
};
调用:
var rc = new RangeChildren();
rc.add();
rc.show();
//工厂方式:封装函数
function test(name) {
var obj = new Object();
obj.name = name;
obj.sayName = function () {
alert(this.name);
};
//抛出
return obj;
}
var p1 = test('小明');
p1.sayName();
var p2 = test('小王');
p2.sayName()
可参考其它的封装方法