//第一种
var Person1 = {
say:function(){
console.log("Person1.say");
}
}
new Person1.say();
Person1.say();
//第二种
var Person2 = function(){}
Person2.say = function(){console.log("Person2.say")}
Person2.say();
new Person2.say();
//第三种
var Person3 = function(name){
return {
say : function(){
console.log("Person3.say"+name);
}
}
}
var person3_1 = new Person3("胡林军");
var person3_2 = new Person3("胡林");
person3_2.say();
Person3("胡").say();
//第四种
var Person4 = function(){
this.say = function(){
console.log("Person4.say");
}
}
new Person4().say();
//第五种
var Person5 = function(){}
Person5.prototype={
say:function(){
console.log("Person5.say");
//return this;//链式写法
}
}
new Person5().say();
//第六种
//高级进阶
var Book1 = (function(){
var booknum = 0;
return function(name){
this.name = name;
this.checkBook=function(){console.log("Book1"+name)}
}
})();
var book1_1 = new Book1("book1");
book1_1.checkBook();
var book1_2 = new Book1("book2");
book1_2.checkBook();
//第七种** 特别有绑定原型出现污染
var Book2 = (function(){
var booknum = 0;
var _Book2 = function(name,_booknum){
this.name = name;
booknum = _booknum;
this.checkBook=function(){console.log("Book2"+name)}
}
_Book2.prototype.checkBook2 = function(){console.log("Book2"+booknum)}
return _Book2;
})();
var book2_1 = new Book2("book1",1);
var book2_2 = new Book2("book2",2);
book2_1.checkBook();
book2_2.checkBook();
book2_1.checkBook2();
book2_2.checkBook2();