【发布时间】:2019-02-22 20:44:09
【问题描述】:
我试图在可以由嵌套函数表达式调用的构造函数中设置一个变量。不太清楚如何做到这一点
var test = function() {
var a;
function test(a, b, c) {
this.a = a;
this.b = b;
this.c = c;
}
test.getvariableA = function() {
//not returning a variable that is supposed to be set by the constructor
console.log(this.a);
};
return test;
}();
var t = new test("pizza", "pasta", "steak");
//does not return the variable
test.getvariableA();
//this returns the variable
console.log(t.a);
test.getvariableA();
这应该返回构造函数设置的变量。也许我对另一种语言感到困惑 感谢您提前提供任何帮助。
【问题讨论】:
-
你会用es6吗?这可能对你有帮助developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
-
@chevybow 不。我必须保持结构不变,而不需要进行基于类的声明
-
您认为
new test究竟返回了什么?尝试重命名变量。之后,只需单步执行代码,看看哪里出错了。
标签: javascript variables nested-function