2012-10-10 135 views
2

這是一個新手查詢。我經歷了幾次類似的帖子,但他們沒有足夠的幫助我。這篇文章有2個查詢,但是我們放在一起,因爲它們的根似乎是一樣的。「this」裏面的javascript函數

var Server = module.exports.Server = function Server(listener) { 
    if (!(this instanceof Server)) return new Server(listener); 
    //other code 
} 

module.exports.createServer = function(listener) { 
    return new Server(listener); 
}; 

我無法理解使用if (!(this instanceof Server)) ;時能不指向服務器位置:

我碰到下面的代碼片段來了?

我試圖把一個簡單的測試方法:

var createTest = function(){ 
    console.log(this.toString()); 
    return new Test(); 
}; 

var Test = function Test(){ 
    console.log(this instanceof Test); 
    console.log(this.toString()); 
    if (!(this instanceof Test)) 
    { 
     return new Test(); 
    } 
} 

var tester = createTest(); 

,輸出:

[object global] 
true 
[object Object] 

這進一步混淆了我在爲什麼this.toString打印[對象的對象] - 它不應該是[對象測試]?

謝謝!

+0

我不知道,但也許那是因爲你在常量是ructor,因此該類尚未被認爲是Test對象?只是一個猜測。 –

回答

5

這是一個習慣使new可選。例如:

function TraditionalTest() { 
} 
function IdiomaticTest() { 
    if(!(this instanceof IdiomaticTest)) return new IdiomaticTest(); 
} 

console.log(new TraditionalTest()); // [object Object] 
console.log(TraditionalTest());  // undefined 
console.log(new IdiomaticTest()); // [object Object] 
console.log(IdiomaticTest());  // [object Object] 

至於爲什麼它[object Object]而非[object Test],我不知道爲什麼它的定義方式,但that's the way it's defined

toString方法被調用時,採取以下步驟:

  1. 如果this的值爲undefined,返回「[object Undefined]」。
  2. 如果this的值爲null,則返回「[object Null]」。
  3. 設O是調用ToObject傳遞this值作爲參數的結果。
  4. 類別的[[Class]]內部屬性的值。
  5. 返回連接三個字符串「[object」,和「]」的結果的字符串值。

[等級]被引用在規範幾次,但它永遠只能代表內建類型:

  • Arguments
  • Array
  • Boolean
  • Date
  • Error
  • Function
  • JSON
  • Math
  • Number
  • Object
  • RegExp
  • String
+0

你能解釋一下我的查詢的第二部分嗎? – WinOrWin

+0

@WinOrWin:當然。我編輯了我的答案。 – icktoofay

+0

+1 ES5在'[[]]'中使用一個單詞(比如'[[Prototype]]'),這意味着它是一個規範機制。實現必須像支持它一樣行事,但實際上並不需要。因此'[[Class]]'只是定義對象類的一種方式,而不指定它們實際上有一個內部的,不可訪問的* Class *屬性。他們必須像他們一樣行事。參見[ES5§8.6.2對象內部屬性和方法](http://ecma-international.org/ecma-262/5.1/#sec-8.6.2) – RobG

1

該測試可確保實例化一個新的服務器被用於這兩種用途的創建:

new module.exports.Server() 

和:

module.exports.Server() 

在第二個,this指針將是全局對象,因此不會是Test的一個實例。在這種情況下,代碼意識到構造函數只是直接調用而沒有new,所以沒有創建新對象。由於這不是構造函數的預期用途,因此通過調用一個並返回結果來強制使用new

+0

第二部分,console.log(this instanceof Test);那麼返回true? – WinOrWin

+1

你用'return new Test();'調用它,它創建一個新的'Test'對象,然後用'this'設置該新對象來調用構造函數。所以'console.log(this instanceof Test);'在構造函數中顯示這確實發生了。 – jfriend00