0

I am trying to access the static properties of a class without using an instance of the class to do so. I tried adapting the method in this post, but to no avail. All I get is test.getInstanceId is not a function

Based on how I am creating the class (below), how can I do this? Here is a fiddle.

test = (function() {
  var currentInstance;

  function test() {
    this.id = 0;
    currentInstance = this;
    // this won 't work
    this.getInstanceId = function() {
      return currentInstance.id;
    }
  }


  test.prototype.setId = function(id) {
    this.id = id;
  }

  return test;
})();


var myTest = new test();
myTest.setId(1);
console.log(myTest.id)
console.log(test.getInstanceId());
Community
  • 1
  • 1
mseifert
  • 5,390
  • 9
  • 38
  • 100
  • 1
    Also, wouldn't it be `console.log(myTest.getInstanceId())` instead? https://jsfiddle.net/gt0wd8hp/10/ – Baruch Dec 28 '16 at 05:58
  • @Baruch `new test()` will create an instance of the `class` `test`. Defining classes like this allows me to have static variables which I can access from an instance of the class. `myTest` is an instance of `test`, and in order to be able to use it, I would need to have kept a global reference to it, which I am hoping I don't need to do. – mseifert Dec 28 '16 at 06:02
  • Yea, I deleted that comment once I realized it made no sense. – Baruch Dec 28 '16 at 06:03
  • 2
    The purpose of using a closure that way is to create "private" properties. If you want them public, then make them public, e.g. `test.currentInstance = ...`. By convention, constructor names start with a capital letter, so *Test* rather than *test*. – RobG Dec 28 '16 at 06:25
  • @RobG - Thanks for your help and insight. I've posted an answer with working code for anyone who follows, but if you'd like to post an answer, I'll accept it. – mseifert Dec 28 '16 at 16:59

2 Answers2

0

As my comment suggests, you're using test.getInstanceId() instead of myTest.getInstanceId()

var test = (function() {
  var currentInstance;
  /* This won't work
  function getInstanceId(){
      return currentInstance.id;
  }
  */

  function test() {
    this.id = 0;
    currentInstance = this;
    // this won 't work
    this.getInstanceId = function() {
      return currentInstance.id;
    }
  }


  test.prototype.setId = function(id) {
    this.id = id;
  }

  return test;
})();


var myTest = new test();
myTest.setId(1);
console.log(myTest.id)
console.log(myTest.getInstanceId());

Fid: https://jsfiddle.net/gt0wd8hp/10/

Baruch
  • 2,381
  • 1
  • 17
  • 29
  • myTest is an instance of test, and in order to be able to use it, I would need to have kept a global reference to it. Any way to get at the static without the instance myTest? – mseifert Dec 28 '16 at 06:04
  • Don't think so. You could probably do something like this? http://stackoverflow.com/a/1535687/554021 – Baruch Dec 28 '16 at 06:07
  • Yes, that was the same link I posted in the question.I am needing to keep my class definition in its current format. If I can't find an outer way to access the static, I'll have to keep a reference to an instance of the class. – mseifert Dec 28 '16 at 06:14
0

Thanks to RobG, the code below works. It makes the variable public by setting it using test.currentInstance = .... Here is the working fiddle.

When inspecting the object test, the now public var currentInstance seems to "live" outside the test function prototype, which I didn't realize was possible.

I have not corrected the naming convention which he has pointed out - it should be Test instead of test.

test = (function() {
  test.currentInstance = undefined;

  function test() {
    this.id = 0;
    test.currentInstance = this;
  }


  test.prototype.setId = function(id) {
    this.id = id;
  }

  return test;
})();



var myTest = new test();
myTest.setId(1);
console.log(myTest.id)
console.log(test.currentInstance.id);
mseifert
  • 5,390
  • 9
  • 38
  • 100