我可以在JavaScript中這樣定義一個類來創建一個靜態字段:如何在JavaScript類
var appender = function (elements, func) {
this.Prop = something;
staticProp = something_else;
};
我說得對不對?那麼,我該如何在這個類中創建一個靜態字段?我怎樣才能在課堂上訪問該字段?我的意思是我想要一個在該類的所有實例之間共享的字段。
var ap1 = new appender();
var ap2 = new appender();
ap1.Prop = something1;
ap2.Prop = something2;
var t = ap1.Prop == ap2.Prop; // true
ap1.staticProp = something_static;
var s = ap2.staticProp = something_static; // I want to this returns true. how can I?
你可以看看這篇文章: [http://stackoverflow.com/questions/1535631/static-variables在-javascript](http://stackoverflow.com/questions/1535 ...) 'appender.staticProp = x;'將解決您的問題。 –
[JavaScript中的靜態變量](http://stackoverflow.com/questions/1535631/static-variables-in-javascript) –