How to extend existing constructor function in javascript? - Stack Overflow

Let's suppose that I have the following object function:function A(options){...}Then I want to cr

Let's suppose that I have the following object function:

function A(options){
 ...
}

Then I want to create a new function (B) that inherits A's prototype. These are the conditions I'm looking for:

  • B's prototype, if modified, should not modify A's
  • when calling to B function as a constructor, A's constructor should be called with the corresponding options.

B should look like this:

function B(aOptions, bOptions){ ... }

var b = new B({}, {})

Let's suppose that I have the following object function:

function A(options){
 ...
}

Then I want to create a new function (B) that inherits A's prototype. These are the conditions I'm looking for:

  • B's prototype, if modified, should not modify A's
  • when calling to B function as a constructor, A's constructor should be called with the corresponding options.

B should look like this:

function B(aOptions, bOptions){ ... }

var b = new B({}, {})
Share Improve this question edited Oct 10, 2014 at 16:21 Dan D. 74.7k15 gold badges110 silver badges127 bronze badges asked Oct 10, 2014 at 16:17 Pato LocoPato Loco 1,2441 gold badge14 silver badges30 bronze badges 3
  • Javascript doesn't have a classical inheritance mechanism. – amphetamachine Commented Oct 10, 2014 at 16:19
  • Answers to many JS questions can be found on MDN: developer.mozilla/en-US/docs/Web/JavaScript/… – Felix Kling Commented Oct 10, 2014 at 16:53
  • There would be no need for 2 options. You can pass one and have the functions use or mutate whatever applies to that specific function. More on prototype and passing arguments to a chain of functions here: stackoverflow./questions/16063394/… – HMR Commented Oct 11, 2014 at 0:27
Add a ment  | 

1 Answer 1

Reset to default 7

Just call A constructor with this

function B(aOptions, bOptions) {
  A.call(this, aOptions);

  // do stuff with bOptions here...
}

Now to setup the prototype

B.prototype = Object.create(A.prototype, {
  constructor: {
    value: B
  }
});

Now B will have the prototype methods from A.

Any new methods added to B's prototype will not be available to A's prototype


There's a couple other tweaks that can make your life easier too.

function A(options) {

  // make `new` optional
  if (!(this instanceof A)) {
    return new A(options);
  }

  // do stuff with options here...
}

And do the same for B

function B(aOptions, bOptions) {

  // make `new` optional
  if (!(this instanceof B)) {
    return new B(aOptions, bOptions);
  }

  // call parent constructor
  A.call(this, aOptions);

  // do stuff with bOptions here...
}

Now you can call A(options) or new A(options) to get the same result.

Same with B, B(aOptions, bOptions) or new B(aOptions, bOptions) will get the same result.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744232075a4564300.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信