小编Jon*_*mes的帖子

JavaScript使用构造函数设置变量

我试图在构造函数中设置一个可以由嵌套函数表达式调用的变量.不太确定如何做到这一点

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);
Run Code Online (Sandbox Code Playgroud)

test.getvariableA();

这应该返回构造函数设置的变量.也许我对另一种语言感到困惑,谢谢你提前帮忙.

javascript variables nested-function

0
推荐指数
1
解决办法
464
查看次数

标签 统计

javascript ×1

nested-function ×1

variables ×1