1.//立即执行函数写法
var module1 = (function(){
var _count = 0;
var m1 = function(){
console.log('m1'); //方法
};
var m2 = function(){
console.log('m2');
};
var name ="leon";//属性
return {
name: name,
m1 : m1,
m2 : m2,
};
})();
//放大模式 (继承)
var module1 = (function (mod){
mod.m3 = function () {
console.log('m3');
};
mod.mode = 666; //属性
return mod;
})(module1);
var a1 = module1;
a1.m1();
a1.m2();
a1.m3();
alert(a1.name);
alert(a1.mode);
2.函数写法
function MyClass(name,age){
this.name = name;
this.age = age;
}
MyClass.prototype = {
toString:function(){
alert("string");
},
sayHellow:function(){
alert("hello");
}
};
var cls1 = new MyClass("liming",10);
alert(cls1.name);
cls1.sayHellow();
1.js中利用prototype给类添加方法
https://blog.csdn.net/github_26672553/article/details/51280131
2.Javascript 定义类(class)的三种方法
https://blog.csdn.net/muzhengjun/article/details/79409806
转载于:https://blog.51cto.com/13968096/2357937