2009-01-12 74 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; 
})(); 
相关问题