如何访问是在函数中定义这样一个变量:如何在函数内访问变量
var functionVar = function(){
this.var = 1;
}
console.log(functionVar().var); //MH - obviously this doesn't work, but I'm looking for the command that will log that variable
如何访问是在函数中定义这样一个变量:如何在函数内访问变量
var functionVar = function(){
this.var = 1;
}
console.log(functionVar().var); //MH - obviously this doesn't work, but I'm looking for the command that will log that variable
您可以访问这个样子,
var functionVar = function(){
this.var = 1;
}
var o = new functionVar();
alert(o.var)
这是构造对象上的属性。不是一个变量。我们确定这个函数是用来作为构造函数吗? – 2012-08-09 14:45:38
是的,它不是一个变量。我的英语不好。 – 2012-08-09 14:47:12
一个新会做的伎俩。
var functionVar = function(){
this.var = 1;
}
console.log(new functionVar().var);
虽然不知道你试图通过使用此代码实现什么。
“functionVar.var”不起作用?为什么添加括号给变量? – Ricola3D 2012-08-09 14:42:42
这不是一个“变量” - 它是一个对象属性。 – Pointy 2012-08-09 14:43:09
真的,这不是一个实例。 – Ricola3D 2012-08-09 14:45:18