var arr = []; arr.a =1;
複製代碼
var person = function(name){
this.name = name
};
person.prototype.getName=function(){//經過person.prototype設置函數對象屬性
return this.name;
}
var crazy= new person(‘crazyLee’);
crazy.getName(); //crazyLee//crazy繼承上屬性
複製代碼
在函數內建立一個對象,給對象賦予屬性及方法再將對象返回設計模式
function Person() {
var People = new Object();
People.name = 'CrazyLee';
People.age = '25';
People.sex = function(){
return 'boy';
};
return People;
}
var a = Person();
console.log(a.name);//CrazyLee
console.log(a.sex());//boy
複製代碼
無需在函數內部從新建立對象,而是用this指代數組
function Person() {
this.name = 'CrazyLee';
this.age = '25';
this.sex = function(){
return 'boy'
};
}
var a = new Person();
console.log(a.name);//CrazyLee
console.log(a.sex());//boy
複製代碼
函數中不對屬性進行定義,利用prototype屬性對屬性進行定義,能夠讓全部對象實例共享它所包含的屬性及方法。bash
function Parent() {
Parent.prototype.name = 'carzy';
Parent.prototype.age = '24';
Parent.prototype.sex = function() {
 var s="女";
    console.log(s);
}
}
var x =new Parent();
console.log(x.name); //crazy
console.log(x.sex()); //女
複製代碼
原型模式+構造函數模式。這種模式中,構造函數模式用於定義實例屬性,而原型模式用於定義方法和共享屬性函數
function Parent(){
this.name="CrazyLee";
this.age=24;
};
Parent.prototype.sayname=function(){
return this.name;
};
var x =new Parent();
console.log(x.sayname()); //Crazy  
複製代碼
將全部信息封裝在了構造函數中,而經過構造函數中初始化原型,這個能夠經過判斷該方法是否有效而選擇是否須要初始化原型。ui
function Parent(){
this.name="CrazyLee";
this.age=24;
if(typeof Parent._sayname=="undefined"){
Parent.prototype.sayname=function(){
return this.name;
}
Parent._sayname=true;
}
};
var x =new Parent();
console.log(x.sayname());
複製代碼