方法1:
var Aim = function(){ };
Aim.prototype.start = function($a){ alert($a)};
Aim.prototype.stop = function($b){alert($b)};
var my = new Aim();
my.start('start');
my.stop('stop');
方法2:
var Aim = function(){ };
Aim.prototype = {
start:function($a){
alert($a);
},
stop:function($b){
alert($b+'======');
}
};
var my = new Aim();
my.start('start');
my.stop('stop');
方法3:
Function.prototype.method = function($name,$fn){
this.prototype[$name] = $fn;
return this;
}
var Aim = function(){ };
Aim.method('start',function($a){
alert($a)
}).method('stop',
function($b){alert($b)
}
);
var my = new Aim();
my.start('start');
my.stop('stop');
匿名立即执行函数 1. (function(){alert(10*2)})(); 2.(function(a,b){ alert(a*b)})(100,333); 3.var a = (function(a,b){ return a*b;})(10,555);
function person(name,age){
this.name = name;
this.age = age;
}
person.prototype= {
getName:function(){
alert(this.name);
},
getAge:function(){
alert(this.age);
}
}
var p = new person('jhon',222);
p.getAge();
p.getName();
封装
var packaging = function(){
//私有属性和方法
var name = '1111111';
var method1 = function(){
alert('method1');
};
//特权属性和方法
this.title = 'this is title';
this.getName = function(){
alert(name);
}
}
//公有静态属性和方法
packaging.name1= '22222';
packaging.getName1 = function(){
alert(packaging.name1);
}
//公有属性和方法
packaging.prototype = {
Myinit:function(){
this.getName();
}
};
var a = new packaging();
a.getName();
packaging.getName1();
//a.Myinit()