最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript setting variables with a constructor - Stack Overflow

programmeradmin1浏览0评论

I was trying to set a variable in a constructor function that can be called by nested function expression. not too sure on how to do this

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);

I was trying to set a variable in a constructor function that can be called by nested function expression. not too sure on how to do this

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();

This should be returning the variable set by the constructor. Maybe I'm confused with another language Thanks for any help in advance.

Share Improve this question edited Sep 18, 2018 at 19:07 Omid Nikrah 2,4803 gold badges16 silver badges31 bronze badges asked Sep 18, 2018 at 18:51 JonoJamesJonoJames 1,22310 silver badges23 bronze badges 3
  • Are you able to use es6? This may be helpful for you developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – chevybow Commented Sep 18, 2018 at 18:54
  • @chevybow Nope . I have to keep the structure as is without going over to class based declaration – JonoJames Commented Sep 18, 2018 at 18:56
  • What exactly do you think new test returns? Try renaming your variables. After that, just step through the code and see where it goes wrong. – Jonathan Commented Sep 18, 2018 at 19:11
Add a ment  | 

2 Answers 2

Reset to default 7

this returns the variable: console.log(t.a);

Right, so the property is on the t instance.

But your test.getvariableA function doesn't know about t at all! It does try to access test.a when you are invoking test.getvariableA().

You may be looking to put the method on the prototype object of your class, not the constructor itself. That way it will be inherited by all instances (like t), and you can call it on t to get t.a:

var test = function() {
  // var a; - this is not used anywhere, drop it

  function test(a, b, c) {
    this.a = a;
    this.b = b;
    this.c = c;

  }
  test.prototype.getVariableA = function() {
//    ^^^^^^^^^^
    console.log(this.a);
  };
  return test;
}();

var t = new test("pizza", "pasta", "steak");
t.getVariableA(); /*
^ */
console.log(t.a);

The issue here is that you are defining the getvariableA outside of the constructor and attaching it to the function test instead. Therefore the getvariableA is a "public" method and does not point to the t instance you created (or any other instance you would create using the new keyword).

In other words this inside of the test.getvariableA points to the function constructor itself, not any specific instance of this constructor (t in your example).

When yo attach a method to the function outside of the constructor you can access it without creating new instances. If you console.log(test.getvariableA) you can see you have access to this method without creating new instances whereas console.log(test.a) shows undefined as a is assigned to each new instance of the class.

Hope this clarifies it at least a bit, sorry if it's not clear.

发布评论

评论列表(0)

  1. 暂无评论