Node.js 類的創(chuàng)建

2018-01-12 13:38 更新

返回對象的函數(shù)是創(chuàng)建類似對象的好方法。

例1

以下代碼顯示了如何使用此模式創(chuàng)建對象。

function Message() {//from www.o2fo.com
   var message = "hello";
   function setMessage(newMessage) {
      if (!newMessage) 
         throw new Error("cannot set empty message");
      message = newMessage;
   }
   function getMessage() {
      return message;
   }
   function printMessage() { 
      console.log(message); 
   }
   return {
      setMessage: setMessage,
      getMessage: getMessage,
      printMessage: printMessage
   }; 
} 
// Pattern in use 
var hi1 = Message(); 
hi1.printMessage(); // hello 

var hi2 = Message(); 
hi2.setMessage("hi"); 
hi2.printMessage(); // hi 

hi1.printMessage(); // hello 

了解這一點

Javascript的 this對象的行為不同取決于我們?nèi)绾畏Q呼它。this 對象指的是調(diào)用上下文。調(diào)用上下文是用于調(diào)用函數(shù)的前綴。

var myData = { 
   myValue: 123, 
   myFunction: function () {
      console.log("inside this.myValue is:", this.myValue); 
   } 
} 
console.log("myData.myValue is: ", myData.myValue); // myData.myValue is: 123 
myData.myFunction(); // inside this.myValue is: 123 

默認調(diào)用上下文是Node.js全局變量。


function myData() {
    console.log("is this called from globals? : ", this === global); // true 
} 
myData(); 

我們可以附加一個函數(shù)到任何對象并改變調(diào)用上下文。

var myData = { 
    myValue: 123 /*www.o2fo.com*/
}; 

function myFunction() { 
    if (this === global) 
        console.log("called from global"); 
    if (this === myData) 
        console.log("called from myData"); 
} 

// global context 
myFunction(); // called from global 

// from myData 
myData.myFunction = myFunction; 
myData.myFunction(); // called from myData 

如果你使用JavaScript運算符new  調(diào)用函數(shù),它創(chuàng)建一個新的JavaScript對象,并且這個在函數(shù)內(nèi)引用這個新創(chuàng)建的對象。

function myData() {//www.o2fo.com
   this.myData = 123;
   console.log("Is this global?: ", this == global); 
} 

// without the new operator 
myData(); // Is this global?: true 
console.log(global.myData); // 123 

// with the new operator 
var newFoo = new myData(); // Is this global?: false 
console.log(newFoo.myData); // 123 

在上面的代碼中,我們在函數(shù)中修改了this.myData,并將newFoo.myData設置為該值。

理解原型

JavaScript中的每個對象都有一個指向另一個對象的內(nèi)部鏈接,稱為原型。當讀取對象上的屬性時,myData.myValue從myData讀取屬性myValue,JavaScript檢查myData上是否存在此屬性。如果沒有,JavaScript檢查屬性是否存在于myData.__ proto__以及__proto__本身。如果在任何級別找到一個值,則返回它。否則,JavaScript返回undefined。

var shape = {}; 
shape.__proto__.myValue= 123; 
console.log(shape.myValue); // 123 

JavaScript中的“__”前綴不應該由用戶代碼使用。在函數(shù)上使用new運算符創(chuàng)建對象時,__proto__設置為函數(shù)的“.prototype”成員。

function shape() { }; 
shape.prototype.myValue = 123; 

var bas = new shape(); 

console.log(bas.__proto__ === shape.prototype); // true 
console.log(bas.myValue); // 123 

注意1

從相同的函數(shù)創(chuàng)建的原型在所有對象之間共享。

function shape() { }; //www.o2fo.com
shape.prototype.myValue = 123; 

var bas = new shape(); 
var myItem = new shape(); 

console.log(bas.myValue); // 123 
console.log(myItem.myValue); // 123 

shape.prototype.myValue = 456; 

console.log(bas.myValue); // 456 
console.log(myItem.myValue); // 456 

上面的代碼生成以下結(jié)果。

原型共享結(jié)果

假設我們有1000個實例創(chuàng)建了某個對象,而原型的所有屬性和函數(shù)都是共享的。因此原型節(jié)省了內(nèi)存。

注意2

原型屬性由對象上的屬性隱藏。

function shape() { }; //from www.o2fo.com
shape.prototype.myValue = 123; 

var bas = new shape(); 
var myItem = new shape(); 

bas.myValue = 456; 
console.log(bas.myValue);

console.log(myItem.myValue); // 123 

this對象是一個完美的讀取/寫入屬性(數(shù)據(jù))的候選,你應該將其用于所有屬性(數(shù)據(jù))。但函數(shù)一般不會在創(chuàng)建后改變。所以函數(shù)是放在 .prototype 上的很好的選擇。函數(shù)性(函數(shù)/方法)在所有實例之間共享,而屬性屬于單個對象。

例2

以下代碼顯示了在JavaScript中編寫類的模式。

function someClass() { /*from www.o2fo.com*/
   // Properties go here 
   this.someProperty = "some initial value"; 
} 
// Member functions go here: 
someClass.prototype.someMemberFunction = function () {
   this.someProperty = "modified value"; 
   console.log("called from prototype");
} 
// Creation 
var instance = new someClass(); 
// Usage 
console.log(instance.someProperty); // some initial value 
instance.someMemberFunction(); 
console.log(instance.someProperty); // modified value 

上面的代碼生成以下結(jié)果。

在成員函數(shù)中,我們可以使用this訪問當前實例,即使在所有實例之間共享相同的函數(shù)體。核心Node.js中的大多數(shù)類都是使用此模式編寫的。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號