亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

學(xué)習(xí)javascript面向?qū)ο?掌握創(chuàng)建對象的9種方式

 更新時(shí)間:2016年01月04日 09:56:21   投稿:lijiao  
這篇文章主要為大家介紹了創(chuàng)建對象的9種方式,幫助大家更好地學(xué)習(xí)javascript面向?qū)ο?,感興趣的小伙伴們可以參考一下

本文為大家分享了javascript創(chuàng)建對象的9種方式,供大家參考,具體內(nèi)容如下

【1】使用Object構(gòu)造函數(shù)
[缺點(diǎn)]使用同一個(gè)接口創(chuàng)建很多對象,會產(chǎn)生大量重復(fù)代碼

var person = new Object();
  person.name = "Nicholas";
  person.age = 29;
  person.job = "Software Engineer";
  person.sayName = function(){
    alert(this.name);
  }

【2】使用對象字面量
[缺點(diǎn)]使用同一個(gè)接口創(chuàng)建很多對象,會產(chǎn)生大量重復(fù)代碼

var person = {
  name: "Nicholas",
  age : 29,
  job: "Software Engineer",
  sayName: function(){
    alert(this.name);
  }
};

【3】工廠模式:抽象了創(chuàng)建具體對象的過程,考慮到ECMAScript中無法創(chuàng)建類,開發(fā)人員就發(fā)明了一種函數(shù),用函數(shù)來封裝以特定接口創(chuàng)建對象的細(xì)節(jié)
  [缺點(diǎn)]解決了創(chuàng)建多個(gè)相似對象的問題,但沒有解決對象識別的問題

function createPerson(name,age,job){
  var o = new Object();
  o.name = name;
  o.age = age;
  o.job = job;
  o.sayname = function(){
    alert(this.name);
  }
  return o;
}
var person1 = createPerson('Nicholas',29,'software Engineer');
var person2 = createPerson('greg',27,'doctor');

【4】構(gòu)造函數(shù)模式:沒有顯式地創(chuàng)建對象,直接將屬性和方法賦給了this對象,沒有return語句
  [缺點(diǎn)]每個(gè)方法都要在每個(gè)實(shí)例上重新創(chuàng)建一遍

function Person(name,age,job){
  this.name = name;
  this.age = age;
  this.jog = job;
  this.sayName = function(){
    alert(this.name);
  };
  //與聲明函數(shù)在邏輯上是等價(jià)的
  //this.sayName = new Function('alert(this.name)');

}
var person1 = new Person("Nicholas",29,"software Engineer");
var person2 = new Person("Greg",27,"doctor");

【4.1】構(gòu)造函數(shù)拓展模式:把函數(shù)定義轉(zhuǎn)移到構(gòu)造函數(shù)外部
[缺點(diǎn)1]在全局作用域中定義的函數(shù)實(shí)際上只能被某個(gè)對象調(diào)用,這讓全局作用域有點(diǎn)名不副實(shí)
[缺點(diǎn)2]若對象需要定義很多方法,就要定義很多全局函數(shù),這個(gè)自定義引用類型就沒有封裝性可言

function Person(name,age,job){
  this.name = name;
  this.age = age;
  this.job = job;
  this.sayName = sayName;
}
function sayName(){
  alert(this.name);
}
var person = new Person('小火柴','20','student')
person.sayName();
console.log(Person);

【5】原型模式:我們創(chuàng)建的每個(gè)函數(shù)都有一個(gè)prototype(原型)屬性,這個(gè)屬性是一個(gè)指針,指向一個(gè)對象,而這個(gè)對象的用途是包含可以由特定類型的所有實(shí)例共享的屬性和方法。如果按照字面意思來理解,prototype就是通過調(diào)用構(gòu)造函數(shù)而創(chuàng)建的對象實(shí)例的原型對象

function Person(){
  Person.prototype.name = "Nicholas";
  Person.prototype.age = 29;
  Person.prototype.job = "software Engineer";
  Person.prototype.sayName = function(){
    alert(this.name);
  }
}
var person1 = new Person();
person1.sayName();//"Nicholas"
var person2 = new Person();
person2.sayName();//"Nicholas"
alert(person1.sayName == person2.sayName);//true

【5.1】更簡單的原型模式:為了減少不必要的輸入,也為了從視覺上更好地封裝原型的功能,用一個(gè)包含所有屬性和方法的對象字面量來重寫整個(gè)原型對象。
[缺點(diǎn)]以這種方式重設(shè)constructor屬性會導(dǎo)致它的[[Enumerable]]特性被設(shè)置為true,默認(rèn)情況下原生的constructor屬性是不可枚舉的

function Person(){};
Person.prototype = {
  constructor : Person,
  name: "Nicholas",
  age: 29,
  job: "software Engineer",
  sayName : function(){
    alert(this.name);
  }
};

【5.2】解決enumerable問題的原型模式

function Person(){};
Person.prototype = {
  name: "Nicholas",
  age: 29,
  job: "software Engineer",
  sayName : function(){
    alert(this.name);
  }
};
Object.defineProperty(Person.prototype,"constructor",{
  enumerable : false,
  value : Person
});

[原型模式缺點(diǎn)1]重寫原型對象切斷了現(xiàn)有原型與已存在對象實(shí)例之間的聯(lián)系,它們引用的仍是最初的原型。

function Person(){}
var friend = new Person();
Person.prototype = {
  constructor: Person,
  name: "Nicholas",
  age: 29,
  job: "Software Engineer",
  sayName: function(){
    alert(this.name);
  }
};
friend.sayName();//error

[原型模式缺點(diǎn)2]引用類型屬性的共享性問題突出

function Person(){}
Person.prototype = {
  constructor: Person,
  name: "Nicholas",
  age: 29,
  job: "Software Engineer",
  friend : ["shelby","Court"],
  sayName: function(){
    alert(this.name);
  }
};
var person1 = new Person();
var person2 = new Person();
person1.friends.push("Van");
alert(person1.friends);//["shelby","Court","Van"];
alert(person2.friends);//["shelby","Court","Van"];
alert(person1.friends === person2.friends);//true

【6】組合模式:組合使用構(gòu)造函數(shù)模式和原型模式是創(chuàng)建自定義類型的最常見方式。構(gòu)造函數(shù)模式用于定義實(shí)例屬性,而原型模式用于定義方法和共享的屬性。這種混成模式還支持向構(gòu)造函數(shù)傳遞參數(shù),是用來定義引用類型的一種默認(rèn)模式

function Person(name,age,job){
  this.name = name;
  this.age = age;
  this.job = job;
  this.friends = ["shelby","Court"];
}
Person.prototype = {
  constructor: Person,
  sayName : function(){
    alert(this.name);
  }  
}
var person1 = new Person("Nicholas",29,"Software Engineer");
var person2 = new Person("Greg",27,"Doctor");
person1.friends.push("Van");
alert(person1.friends);// ["shelby","Court","Van"];
alert(person1.friends);// ["shelby","Court"];
alert(person1.friends === person2.friends);//false
alert(person1.sayName === person2.sayName);//true

【7】動態(tài)原型模式:把所有信息都封裝在構(gòu)造函數(shù)中,通過在構(gòu)造函數(shù)中初始化原型(僅在必要情況下),又保持了同時(shí)使用構(gòu)造函數(shù)和原型的優(yōu)點(diǎn)。換句話說,可以通過檢查某個(gè)存在的方法是否有效,來決定是否要初始化原型。
  [注意]使用動態(tài)原型模式時(shí),不能使用對象字面量重寫原型。如果在已經(jīng)創(chuàng)建了實(shí)例的情況下重寫原型,那么就會切斷現(xiàn)有實(shí)例與新實(shí)例之間的聯(lián)系

function Person(name,age,job){
  //屬性
  this.name = name;
  this.age = age;
  this.job = job;
  //方法
  if(typeof this.sayName != "function"){
    Person.prototype.sayName = function(){
      alert(this.name);
    };
  }
}
var friend = new Person("Nicholas",29,"Software Engineer");
friend.sayName();

【8】寄生構(gòu)造函數(shù)模式:創(chuàng)建一個(gè)函數(shù),該函數(shù)的作用僅僅是封裝創(chuàng)建對象的代碼,然后再返回新創(chuàng)建的對象

function Person(name,age,job){
  var o = new Object();
  o.name = name;
  o.age = age;
  o.job = job;
  o.sayName = function(){
    alert(this.name);
  };
  return o;
}
var friend = new Person("Nicholas",29,"Software Engineer");
friend.sayName();//"Nicholas"

【寄生構(gòu)造函數(shù)模式應(yīng)用】創(chuàng)建一個(gè)具有額外方法的特殊數(shù)組。由于不能直接修改Array構(gòu)造函數(shù),因此可以使用這個(gè)模式

function SpecialArray(){
  //創(chuàng)建數(shù)組
  var values = new Array();
  //添加值
  values.push.apply(values,arguments);
  //添加方法
  values.toPipedString = function(){
    return this.join('|');
  };
  //返回?cái)?shù)組
  return values;
}
var colors = new SpecialArray("red","blue","green");
alert(colors.toPipedString());//"red|blue|green"  

【9】穩(wěn)妥構(gòu)造函數(shù)模式:所謂穩(wěn)妥對象指沒有公共屬性,而且其方法也不引用this的對象。穩(wěn)妥對象最適合在一些安全環(huán)境中(這些環(huán)境會禁止使用this和new)或者在防止數(shù)據(jù)被其他應(yīng)用程序改動時(shí)使用。

function Person(name,age,job){
  //創(chuàng)建要返回的對象
  var o = new Object();
  //可以在這里定義私有變量和函數(shù)
  //添加方法
  o.sayName = function(){
    alert(name);
  };
  //返回對象
  return o;
}
//在穩(wěn)妥模式創(chuàng)建的對象中,除了使用sayName()方法之外,沒有其他方法訪問name的值
var friend = Person("Nicholas",29,"Software Engineer");
friend.sayName();//"Nicholas"

以上就是javascript創(chuàng)建對象的九種方式,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評論