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

javascript - How do I wrap a constructor? - Stack Overflow

programmeradmin2浏览0评论

I have this JavaScript:

var Type = function(name) {
    this.name = name;
};

var t = new Type();

Now I want to add this:

var wrap = function(cls) {
    // ... wrap constructor of Type ...
    this.extraField = 1;
};

So I can do:

wrap(Type);
var t = new Type();

assertEquals(1, t.extraField);

[EDIT] I'd like an instance property, not a class (static/shared) property.

The code executed in the wrapper function should work as if I had pasted it into the real constructor.

The type of Type should not change.

I have this JavaScript:

var Type = function(name) {
    this.name = name;
};

var t = new Type();

Now I want to add this:

var wrap = function(cls) {
    // ... wrap constructor of Type ...
    this.extraField = 1;
};

So I can do:

wrap(Type);
var t = new Type();

assertEquals(1, t.extraField);

[EDIT] I'd like an instance property, not a class (static/shared) property.

The code executed in the wrapper function should work as if I had pasted it into the real constructor.

The type of Type should not change.

Share Improve this question edited Apr 11, 2012 at 7:30 Aaron Digulla asked Apr 11, 2012 at 7:14 Aaron DigullaAaron Digulla 329k110 gold badges623 silver badges837 bronze badges 4
  • 1 By what I understand, you want to add an additional property to the constructor? so that further new instances have that property? – Joseph Commented Apr 11, 2012 at 7:20
  • May be you need simply to change prototype of Type inside wrap() function. For example: var wrap = function(cls) { cls.prototype.extraField=1; };? Or may be is best to create new Type2 inherited from Type with additional extraField member? – Andrew D. Commented Apr 11, 2012 at 7:27
  • Can you describe your issue more detail? – seteh Commented Apr 11, 2012 at 7:28
  • I'd like an instance property, not a class (static/shared) property. The code executed in the wrapper function should work as if I had pasted it into the real constructor. – Aaron Digulla Commented Apr 11, 2012 at 7:29
Add a ment  | 

1 Answer 1

Reset to default 8

update: An updated version here

what you were actually looking for was extending Type into another Class. There are a lot of ways to do that in JavaScript. I'm not really a fan of the new and the prototype methods of building "classes" (I prefer the parasitic inheritance style better), but here's what I got:

//your original class
var Type = function(name) {
    this.name = name;
};

//our extend function
var extend = function(cls) {

    //which returns a constructor
    function foo() {

        //that calls the parent constructor with itself as scope
        cls.apply(this, arguments)

        //the additional field
        this.extraField = 1;
    }

    //make the prototype an instance of the old class
    foo.prototype = Object.create(cls.prototype);

    return foo;
};

//so lets extend Type into newType
var newType = extend(Type);

//create an instance of newType and old Type
var t = new Type('bar');
var n = new newType('foo');


console.log(t);
console.log(t instanceof Type);
console.log(n);
console.log(n instanceof newType);
console.log(n instanceof Type);
发布评论

评论列表(0)

  1. 暂无评论