2009-01-12 76 views
11

這是我剛剛發佈的問題的後續行動。 我想知道在使用MyClass.prototype來定義方法時,如何在JavaScript類中處理成員變量。面向對象的javascript中成員變量的最佳方法?

如果你定義所有的構造函數的方法:

function MyClass(){ 
this.myMethod = function(){} 
} 

你可以非常漂亮聲明成員變量,並從你的方法裏面訪問它們:

function MyClass(){ 
var myVar = "hello"; 
this.myMethod = function(){ 
    alert(myVar); 
} 
} 

當使用對象。原型技術,你失去了這種精緻,必須這樣做;

function MyClass(){} 
MyClass.prototype.myVar = "hello"; 
MyClass.prototype.myMethod = function(){alert(this.hello)}; 

我對每次訪問成員變量時都必須寫「this」並不是瘋狂。我想使用Object.prototype方法來實現內存和靈活性的原因,但它似乎是一種笨拙的語法。這是你們通常工作的方式嗎?

感謝,

-Morgan

+0

關於私人變量e會員,您可能有興趣使用Google Closure編譯器。每個類都可以在單獨的文件中定義,然後私有變量將由編譯器強制執行。 https://developers.google.com/closure/compiler/docs/js-for-compiler#tag-private – 2014-04-11 13:10:22

回答

14

你應該在你所厭惡的使用this指針來訪問成員變量。在構造函數中

分配成員變量,你可以用原型方法訪問它們:

function Cat(){ 
    this.legs = 4; 
    this.temperament = 'Apathetic'; 
    this.sound = 'Meow'; 
} 

Cat.prototype.speak = function(){alert(this.sound)} 

var cat = new Cat(); 
cat.speak(); 

是那些物體的屬性是公開的,但是,作爲圭多會說,我們這裏都是成年人。畢竟,Javascript是一種純文本,鬆散類型的解釋型語言。在這種環境中,「私人」變量的好處最多也就是搖搖欲墜。

我說的只是明確而明確地告訴你應該如何訪問你的對象,並且違規者會自行承擔風險。

3

您應該使用原型存儲方法,因爲當你用100種方法找到自己,他們不在身邊實例之間的複製,而它們使用相同的原型。 我使用這些方針的東西:

var myClass = function(){}; 
myClass.prototype = { 
    method1: function(){} 
    ,method2: function(){} 
}; 
+0

謝謝sktrdie,這是我改用原型的主要原因。我想知道什麼是管理成員變量的最佳方式。我是否需要辭職自己去輸入「這個」呢? – morgancodes 2009-01-12 17:37:37

+0

re:「this」:是的,這是你必須習慣的那些東西之一。 – 2009-01-12 17:52:52

-2

您將使用

function name() 
{ 
    var m_var = null; 
    var privateMethod = function() { } 
    this.PublicMethod = function() { } 
} 

包括我自己找了很多人。我傾向於不寫大量的JS類,這個語法很容易編寫,並且允許私有方法/變量。

編輯

所以,你告訴我,使用XML SOAP包只是爲了調試一步到位更容易是正常的(儘管XML廢物的空間和帶寬,以及時間解析一堆),但加入了每個javascript對象實例的幾個字節是不正確的,即使它允許我們從一開始就使用語言中應該存在的一些fundemental OO概念?

嗯......

(我的XML註釋閱讀註釋:REST URIs and operations on an object that can be commented on, tagged, rated, etc

+0

-1。他想要使用原型函數來獲得內存的好處,有時候這些好處可能很大。 – Triptych 2009-01-12 17:40:30

+0

謝謝尼爾森, 這就是我一直在做的,但最近得知這個.PublicMethod將被複制一次,每個實例。如果您只有幾個實例,則不是問題,但如果您有很多實例,它可能會變得很重要。 – morgancodes 2009-01-12 17:41:05

14

對象的可見性屬性,根據不同你如何聲明它們

function Cat(name) { 

    //private variable unique to each instance of Cat 
    var privateName = 'Cat_'+Math.floor(Math.random() * 100); 

    //public variable unique to each instance of Cat 
    this.givenName = name; 

    //this method has access to private variables 
    this.sayPrivateName = function() { 
     alert(privateName); 
    } 
} 

//this variable is shared by all cats 
Cat.prototype.generalName = 'tiddles'; 

//this method is shared by all cats and has no access to private vars 
Cat.prototype.sayname = function(type) { 
    alert(this[type+'Name'] || 'private!'); 
} 

var vic = new Cat('Victor'); 
var ellers = new Cat('Elmore'); 

vic.sayname('general'); //tiddles 
vic.sayname('given');  //Victor 
vic.sayname('private'); //private - no access 
vic.sayPrivateName();  //cat will say its name 

ellers.sayname('general'); //tiddles 
ellers.sayname('given');  //Elmore 
ellers.sayname('private'); //private - no access 
ellers.sayPrivateName();  //cat will say its name 
1

A(沒有的話)對「私人」變量小此言分配方法的原型時:

它,你是真的不能使用構造函數在其變量上創建閉包,但您當然可以用匿名函數包圍原型方法,並獲取對象實例之間共享的私有變量:

function Foo() {} 

(function() { 
    var sharedPrivateVar; 
    Foo.prototype.methodWithAccessToSharedPrivateVar = function() {}; 
})(); 

依照一些另外的擺弄,你可以實現自己的保護機制,如變量至極只能讀,通過不寫:

function Foo() { 
    this.registerInstance({ bar : 'baz' }); 
    this.registerInstance = undefined; 
} 

(function() { 
    var store = {}, guid = 0; 

    Foo.prototype.registerInstance = function(protectedProperties) { 
     this.__guid = ++guid; 
     store[this.__guid] = protectedProperties; 
    }; 

    Foo.prototype.getProtectedProperty = function(name) { 
     return store[this.__guid][name]; 
    }; 

})(); 

這種做法不會從粗放函數對象和封閉創作吃虧,但少量增加查找次數。

編輯:你也應該提供的功能

Foo.prototype.unregisterInstance = function() { 
    delete store[this.__guid]; 
}; 

否則,這是引入內存泄漏的好方法...

EDIT2:你也可以得到各地需要具有以下模式的registerInstance()函數:

Foo = (function() { 
    var store = {}, guid = 0; 

    function Foo() { 
     this.__guid = ++guid; 
     store[guid] = { bar : 'baz' }; 
    } 

    Foo.prototype.getBar = function() { 
     var privates = store[this.__guid]; 
     return privates.bar; 
    }; 

    Foo.prototype.destroy = function() { 
     delete store[this.__guid]; 
    }; 

    return Foo; 
})(); 
相關問題