2014-10-31 136 views
0
function someClass(){ 
} 
someClass.prototype.CONST = 'Some Constant.'; 

console.log('with Obj : '+(new someClass).CONST); 
console.log('without Obj : '+someClass.CONST); 

第一个给出了正确的答案,第二个回报不确定, 现在,有没有什么办法来访问CONST,而无需创建对象实例,访问,而无需创建对象实例的原型属性

我正在寻找类似的东西访问Java类

回答

4

叶氏的静态属性,你可以访问它:

console.log(someClass.prototype.CONST); 
0

您可以访问通过对象的原型成员都典型成员,而不仅仅是变量:

// returns a value 
someObject.prototype.someMember 

// calls the function someFunct() 
someObject.prototype.someFunct() 

如果你需要从一个对象调用一个方法,用另一个对象为“调用实例”你可以使用.call.apply

// calls functMember using instanceObject as the instance 
someObject.prototype.functMember.call(instanceObject, arg1, arg2, ...); 

// calls functMember using isntanceObject as the instance, using an array as arguments 
someObject.prototype.functMember.call(instanceObject, [arg1, arg2, ...]);