2011-05-26 58 views
2

任何人都可以確認這些來自Pro Javascript Design Patterns第3章的樣本是否有缺陷,如果是這樣,從根本上來說 - 它們比在JavaScript中產生'class'常量的預期目標還要多打字嗎?謝謝。贊成Javascript設計模式勘誤?

var Class = (function() { 

    // Constants (created as private static attributes). 
    var UPPER_BOUND = 100; 

    // Privileged static method. 
    this.getUPPER_BOUND() {//sic 
    return UPPER_BOUND; 
    } 

    ... 

    // Return the constructor. 
    return function(constructorArgument) { 
    ... 
    } 
})(); 

/* Usage. */ 

Class.getUPPER_BOUND(); 

/* Grouping constants together. */ 

var Class = (function() { 

    // Private static attributes. 
    var constants = { 
    UPPER_BOUND: 100, 
    LOWER_BOUND: -100 
    } 

    // Privileged static method. 
    this.getConstant(name) {//sic 
    return constants[name]; 
    } 

    ... 

    // Return the constructor. 
    return function(constructorArgument) { 
    ... 
    } 
})(); 


/* Usage. */ 

Class.getConstant('UPPER_BOUND'); 
+0

Javascript並不經典。學習原型或使用其他語言?閱讀JS:好的部分,而不是你正在閱讀的內容。 – 2011-05-27 04:49:10

+0

@GlennFerrieLive大部分用於記錄,我做過/確實知道原型繼承機制,但是可以看到爲什麼您認爲不然。 – Joffer 2011-11-27 19:29:27

回答

1

的代碼可以平凡固定爲

var Class = { 
    UPPER_BOUND: 100 
}; 

的代碼的其餘部分之上工程化或完全錯誤的,應該被忽略。

如果您只關心被讀取,請將可寫標誌設置爲false(注意默認值爲false)。

var Class = {}; 
Object.defineProperty(Class, "UPPER_BOUND", { 
    value: 100, 
    enumerable: true, 
    configurable: true 
}); 
+0

我的問題在於,UPPER_BOUND是可變的,因此不是常量 – Joffer 2012-01-17 00:07:55

+0

@Joffer它現在是不變的。 – Raynos 2012-01-17 00:33:39

+0

我會離開配置爲false,但是,這是行之有效的 - 謝謝。我猜這本書早於Object.defineProperty – Joffer 2012-01-17 01:02:05

1

檢查了這一點作爲一個很好的選擇:http://www.klauskomenda.com/code/javascript-programming-patterns/

和不可改變的公共屬性,因爲有人建議,使用Object.freeze和John Resig的很好的建議:http://ejohn.org/blog/ecmascript-5-objects-and-properties/

和辦法不要打破你的全球範圍,爲jQuery添加命名空間:Is it possible to create a namespace in jQuery?

+0

良好的鏈接,很好的參考。 – RobG 2011-05-26 02:48:21

+0

是的,總的來說,我還沒有像7年那樣讀過一本書。我知道有一些很好的,但我得到了我需要的一切。 – Milimetric 2011-05-26 12:57:39

+0

感謝您的鏈接。我認爲「揭示模塊」模式對於枚舉和單例模式會很有用。請原諒我,如果我錯了,但我不認爲任何提出的模式可以用來給類公共不可變字段,如果該類的實例化是不受限制的,這是我的模式轉錄的意圖題。你知道任何成功實現這個意圖的模式嗎?我正在考慮將Object.freeze(obj.prototype)與鏈接中自定義對象標題下演示的公式結合使用。 – Joffer 2011-05-27 02:37:21

1

對任何自稱是「專業」的東西要小心。我沒有讀過書,但我採取的代碼如下:

> var Class = (function() { 
> 
> // Constants (created as private static attributes). 

單詞「屬性」是錯誤的,它應該是「屬性」或「變量」,因爲它們是變量,其也可以描述爲本地激活/變量對象的屬性。

> var UPPER_BOUND = 100; 
> 
> // Privileged static method. 
> this.getUPPER_BOUND() {//sic 

該代碼將在全球範圍內,其中this是窗口/全局對象來執行。所以如果有一個全局的* getUPPER_BOUND *函數,它將被調用沒有參數。後面跟着一個花括號({),它在塊不能出現的地方打開一個塊,所以這是一個語法錯誤。

我相信以下的目的:

this.getUPPER_BOUND = function() { 

它創建代碼運行時所assiged在RHS匿名函數全局/ window對象的getUPPER_BOUND財產。

>  return UPPER_BOUND; } 
> 
> ... 
> 
> // Return the constructor. 
> return function(constructorArgument) { 

這是分配給全局變量「Class」的函數。

>  ... 
> } 
> })(); 

修復它可能「工作」,但不優雅。任何在代碼中出現如此明顯錯誤的書都沒有經過精心編寫,並且在發佈之前肯定沒有經過適當的審查。

使用知名的在線資源,並繼續詢問有關您不瞭解或認爲錯誤的任何問題。還有其他討論javascript的論壇可以提供更詳細的技術問題答案。

5

我想,這是錯誤的。如前所述,「this」是指窗口對象,代碼也有語法錯誤。以下代碼應完成所需的目標:

var Class = (function() { 

    // Private static attributes. 

    var constants = { 
     UPPER_BOUND: 100, 
     LOWER_BOUND: -100 
    };    

    var sc = function (constructorArgument) { 

    }; 

    // Privileged static method. 
    sc.getConstant = function (name) { 
     return constants[name]; 
    }; 

    // Return the constructor. 
    return sc; 
})(); 

alert(Class.getConstant('UPPER_BOUND')); 
+0

這是本書希望通過擁有特權靜態方法來展示的內容,但是他們的例子相當不對。 Thanx爲此。 – Zaptree 2012-08-17 03:50:37

0

得到這個工作,但不知道這是否是作者所期望的。

var Class = (function() 
{ 
    // Constants (created as private static attributes). 
    var constants = 
    { 
     UPPER_BOUND: 100, 
     LOWER_BOUND: -100 
    }; 

    // Return the method(s). 
    return { 
     getConstant: function(name) 
     { 
      return constants[name]; 
     } 
    } 
}()); 

console.log(Class.getConstant('UPPER_BOUND')); // shows "100" in console 
+0

您的解決方案避免了getConstant被視爲窗口屬性的問題,但我們仍然需要實例化該類。 – Joffer 2011-11-27 19:31:13

+0

這個怎麼樣?直接返回方法。這樣,我們不必首先啓動Class。 – 2011-12-04 23:35:43

+0

現在的問題是我們不能實例化類 – Joffer 2012-02-01 00:03:03

0

這樣做怎麼樣?

/* Grouping constants together. */ 
var Class = (function() { 
    // Private static attributes. 
    var constants = { 
    UPPER_BOUND: 100, 
    LOWER_BOUND: -100 
    } 

    // Return the constructor. 
    return new function(constructorArgument) { 
    // Privileged static method. 
    this.getConstant = function(name) {//sic 
     return constants[name]; 
    } 
    } 
})(); 

console.log(Class.getConstant("LOWER_BOUND"));