JavaScript編程設計模式之構造器模式執行個體分析,設計模式執行個體分析

來源:互聯網
上載者:User

JavaScript編程設計模式之構造器模式執行個體分析,設計模式執行個體分析

本文執行個體講述了JavaScript編程設計模式之構造器模式。分享給大家供大家參考,具體如下:

經典的OOP語言中,構造器(也叫建構函式)是一個用於初始化對象的特殊方法。在JS中,因為一切皆對象,物件建構器經常被提起。

物件建構器用於建立制定類型(Class)的對象,可以接受參數用於初始化對象的屬性和方法。

對象建立

在JS中,有三個常用的方法用於建立對象:

//1, 推薦使用var newObject = {};//2,var newObject = Object.create( null );//3, 不推薦var newObject = new Object();

但是,這也只是建立了三個Null 物件, 並沒有任何屬性和方法。我們可以通過以下四種方法,為對象設立屬性和方法。

// ECMAScript 3 相容的方式// 1. 常規對象定義方式//設定屬性newObject.someKey = "Hello World";//擷取屬性var key = newObject.someKey;// 2. 方括弧方式// 設定屬性newObject["someKey"] = "Hello World";//擷取屬性var key = newObject["someKey"];// 僅僅用於ECMAScript 5// 3. Object.defineProperty// 設定屬性Object.defineProperty( newObject, "someKey", { value: "for more control of the property's behavior",  writable: true,  enumerable: true,  configurable: true});//可以通過下面的函數簡化屬性設定var defineProp = function ( obj, key, value ){  config.value = value;  Object.defineProperty( obj, key, config );};// 使用方法var person = Object.create( null );defineProp( person, "car", "Delorean" );defineProp( person, "dateOfBirth", "1981" );defineProp( person, "hasBeard", false );// 4. Object.defineProperties//設定屬性Object.defineProperties(newObject,{ "someKey": { value: "Hello World", writable: true }, "anotherKey": { value: "Foo bar", writable: false }});// 3和4的擷取屬性方法同1,2.

基本的構造器

我們知道, JS中沒有Class的概念,但它也支援用構造器建立對象。

通過使用【new】關鍵字,我們可以使一個函數的舉止類似於構造器,從而建立自己的對象執行個體。

一個基礎的構造器形式如下:

function Car( model, year, miles ) {  //這裡,this指向建立立的對象自己  this.model = model;  this.year = year;  this.miles = miles;  this.toString = function () {    return this.model + " has done " + this.miles + " miles";  };}//用法// 建立兩個car執行個體var civic = new Car( "Honda Civic", 2009, 20000 );var mondeo = new Car( "Ford Mondeo", 2010, 5000 );// 輸出結果console.log( civic.toString() );console.log( mondeo.toString() );

這就是簡單的構造器模式, 它有兩個主要問題,

第一,它很難繼承;第二,toString()被每一個對象執行個體定義一遍,作為函數,它應該被每一個Car類型的執行個體共用。

使用原型的構造器

JS中有一個很好的特性:原型【Prototype】,

利用它,建立對象時,所有構造器原型中的屬性都可以被對象執行個體獲得。

這樣多個對象執行個體就可以共用同一個原型。

我們改善前面的Car例子如下:

function Car( model, year, miles ) {  this.model = model;  this.year = year;  this.miles = miles;}Car.prototype.toString = function () {  return this.model + " has done " + this.miles + " miles";};// 用法var civic = new Car( "Honda Civic", 2009, 20000 );var mondeo = new Car( "Ford Mondeo", 2010, 5000 );//輸出console.log( civic.toString() );console.log( mondeo.toString() );

在上面的例子中,toString()方法被多個Car對象執行個體共用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.