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

Javascript: Passing constructor as reference in a variable and calling this constructor - Stack Overflow

programmeradmin1浏览0评论

I need to store the reference to a constructor of a class in a variable and construct the object later. This is my minimal code example:

function A() {
  var _foo = "A";
}

function Wrapper( constructorFunc  ) {
  var _constructorFunc = constructorFunc;

  this.constructorFunc = function() {
    return _constructorFunc;
  }
}

var wrapper = new Wrapper( A.constructor );
var cFunc = wrapper.constructorFunc();
var obj = new cFunc(); /* obj should be an A now */

I hope it is clear what I would like to do. The Firebug console gives the error TypeError: cFunc is not a constructor. What is the correct way?

Moreover I must be able to "pare" constructors, i.e. I need to know if two references point to the same constructor. (In C++ this would be possible, because one pares the function's address.) As an example:

function A() {
  var _foo = "A";
}

function B() {
  var _bar = "B";
}

function Wrapper( constructorFunc  ) {
  var _constructorFunc = constructorFunc;

  this.constructorFunc = function() {
    return _constructorFunc;
  }
}

var wrapper1 = new Wrapper( A.constructor );
var wrapper2 = new Wrapper( A.constructor );
var wrapper3 = new Wrapper( B.constructor );

wrapper1.constructorFunc() == wrapper2.constructorFunc() /* should evaluate to true */
wrapper1.constructorFunc() == wrapper3.constructorFunc() /* should evaluate to false */

Is this possible?

I need to store the reference to a constructor of a class in a variable and construct the object later. This is my minimal code example:

function A() {
  var _foo = "A";
}

function Wrapper( constructorFunc  ) {
  var _constructorFunc = constructorFunc;

  this.constructorFunc = function() {
    return _constructorFunc;
  }
}

var wrapper = new Wrapper( A.constructor );
var cFunc = wrapper.constructorFunc();
var obj = new cFunc(); /* obj should be an A now */

I hope it is clear what I would like to do. The Firebug console gives the error TypeError: cFunc is not a constructor. What is the correct way?

Moreover I must be able to "pare" constructors, i.e. I need to know if two references point to the same constructor. (In C++ this would be possible, because one pares the function's address.) As an example:

function A() {
  var _foo = "A";
}

function B() {
  var _bar = "B";
}

function Wrapper( constructorFunc  ) {
  var _constructorFunc = constructorFunc;

  this.constructorFunc = function() {
    return _constructorFunc;
  }
}

var wrapper1 = new Wrapper( A.constructor );
var wrapper2 = new Wrapper( A.constructor );
var wrapper3 = new Wrapper( B.constructor );

wrapper1.constructorFunc() == wrapper2.constructorFunc() /* should evaluate to true */
wrapper1.constructorFunc() == wrapper3.constructorFunc() /* should evaluate to false */

Is this possible?

Share Improve this question asked Nov 2, 2014 at 12:23 user2690527user2690527 1,8812 gold badges23 silver badges42 bronze badges 3
  • Note: you don't need the _constructorFunc variable because the constructorFunc function parameter will still be accessible within the same closure. – nnnnnn Commented Nov 2, 2014 at 12:37
  • As stated before; constructor is a property of prototype that you get without having to do anything. With inheritance you usually break this property. The following answer may be helpful: stackoverflow./questions/16063394/… – HMR Commented Nov 2, 2014 at 15:24
  • @nnnnnn, @HMR: I really love the munity here, because one usually gets a good answer within little time. See the answer by dystroy below. It is concise and straight to the point. But I wonder why people start to ment on insignificant flaws of the code, if is cleary stated that the code shall serve as a minimal example. As an example see the discussion below the other answer given by plalx. I believe it should be obvious that in my real code I neither have a class A, B nor attributes that are called _foo or _bar. So any discussion if something make "sense" is a waste. – user2690527 Commented Nov 3, 2014 at 15:10
Add a ment  | 

2 Answers 2

Reset to default 5

The constructor is A, so you must change

var wrapper = new Wrapper( A.constructor );

to

var wrapper = new Wrapper( A );

You can test the result with

console.log(obj instanceof A);

For your second question : there's nothing preventing you to pare constructors (or any function) : two functions are equal when they're the same.

In JavaScript, any function can be a constructor, but function's don't have a constructor property.

Therefore, A.constructor should be changed to just A.

However, functions do have prototypes, and the constructor property of their prototypes refers back to them. Therefore, A.prototype.constructor would work as well, but would be a rather wierd way to refer to A.

If your Wrapper only encapsulates a constructor to return it later it seems that you introduced an unecessary object. Just pass the constructor function around directly.

Finally, you can use the instanceof operator to know if an object is an instance of a specific constructor. The advantage of instanceof over paring constructor functions together is that the inheritance hierarchy will be considered.

发布评论

评论列表(0)

  1. 暂无评论