2014-01-29 224 views
-2

在JavaScript中,有兩種模式,我想衡量使用一個和另一個的好處。是什麼和回訪功能,例如返回一個對象之間的區別:返回函數與返回對象

var returnFunction = function(name,age){ 
    var n = name; 
    var a = name; 
    return function(){ 
    anotherFunc : function(){}, 
    oneMoreFunc : function(){} 
    } 
} 

我返回一個包含兩個函數的函數,並獲得私有變量的名字和年齡。我明白我可以調用returnfunction,並且我知道我可以像構造函數一樣使用它。我想知道,什麼是這種風格的VS好處:

var returnObject = function(name,age){ 
    var n = name; 
    var a = age; 
    return { 
    anotherFunc:function(){}, 
    oneMoreFunc:function(){}, 
    }; 
} 
  • 是否有性能損失?
  • 這只是一個風格問題嗎?
  • 對這個或那個有任何好處,還是我只是在推翻這個?

編輯:

至於選項A,我引用從Javascript這個特殊的語法:好的部分

Function.prototype.method = function(name, func) { 
    this.prototype[name] = func; 
    return this; 
}; 

String.method('deentityify', function() { 

    // The entity table. It maps entity names to 
    // characters. 

    var entity = { 
     quot: '"', 
     lt: '<', 
     gt: '>' 
    }; 

    // Return the deentityify method. 

    return function() { 

     // This is the deentityify method. It calls the string 
     // replace method, looking for substrings that start 
     // with '&' and end with ';'. If the characters in 
     // between are in the entity table, then replace the 
     // entity with the character from the table. It uses 
     // a regular expression (Chapter 7). 

     return this.replace(/&([^&;]+);/g, 
      function(a, b) { 
       var r = entity[b]; 
       return typeof r === 'string' ? r : a; 
      } 
     ); 
    }; 
}()); 

選項A是爲了複製這句法一個人爲的例子。

+0

'功能'總是會比調用'對象'吃更多的資源。 –

+5

第一個不行。它返回一個函數,它除了定義和放棄另外兩個函數外什麼也不做。 –

+0

@NicholasHazel如果函數是在一個對象中呢? – Julio

回答

1

變體A不起作用。這是一個語法錯誤。

那麼你真的比較:

var returnFunction = function(name,age){ 
    var n = name; 
    var a = name; 

    // return a function that returns 
    return function(){ 

     // logic to construct the object 
     var obj = { 
      anotherFunc : function(){}, 
      oneMoreFunc : function(){} 
     } 

     // return the object 
     return obj; 
    } 
} 

// vs. 
var returnObject = function(name,age){ 
    var n = name; 
    var a = age; 

    // return the object directly 
    return { 
     anotherFunc:function(){}, 
     oneMoreFunc:function(){}, 
    }; 
} 

這要看是什麼物體的樣子。

在大多數情況下,你會選擇B選項。只需返回一個簡單的對象。 我不知道比V8其他任何東西,但在V8它看起來像這樣:

- >新範圍 - >分配一些瓦爾 - >創建一個函數 - >編譯該函數 代碼 - >返回功能,關閉範圍 - >運行功能 - >新範圍 - >創建對象 - >返回對象,關閉範圍

- >新範圍 - > assi gn一些變量 - >創建對象 - >返回對象,關閉範圍

很明顯,第一步有更多的步驟,但速度差異是微不足道的。

但是,在某些情況下,返回具有多個嵌套屬性和必須初始化的本地函數的複雜對象是不切實際的。在這種情況下,生成對象並將其按需返還將更有用。選項A.

但是,不必選擇一個更好的,如果你打算做一個返回對象的干預措施,它只是更好,使之成爲類:

var returnObjectClass = function(name,age){ 
    this.name = name; 
    this.class = class; 
    this.anotherFunc = function(){}; 
    this.oneMoreFunc = function(){}; 
    return this; 
} 

你可以在這裏閱讀更多:http://www.phpied.com/3-ways-to-define-a-javascript-class/

+0

JavaScript中沒有類。如果* returnObjectClass *被打算作爲構造函數被調用,則它的名字以大寫字母開頭。函數只是用* new *調用時的構造函數,默認情況下它們返回* this *,所以不需要返回語句,並且約定不包含返回語句。 'this.anotherFunc:function(){};'should perahps be'this.anotherFunc = function(){};',但方法應該在構造函數的原型上,否則它只是一個對象。 – RobG

+0

你說的都是對的。我現在正在糾正。 –