我有一个只有一个属性的类:
var A = function(b){this.Value=b};
var a = new A('hello world');
我想按如下方式检索属性的值:
var what = a; //instead of using var what =a.Value;
有可能吗?在python中,我知道我可以使用调用来实现它。
class A:
def __init__(self,b):
self.Value=b
def __call__(self):
return self.Value
a=A('hello world')
what=a()
答案 0 :(得分:1)
没有
您可以做的最好的事情是在类原型中定义toString
方法,这样您就可以直接在字符串中使用该对象:
A.prototype.toString = function() {return this.Value;};
var a = new A('hello world');
var str = 'The string is: ' + a;
答案 1 :(得分:1)
是的,你可以。但这是一个对象。如果你想获得它的价值,你必须致电what.Value