Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I've been following along a blog post on JavaScript inheritance. I wanted to slightly adapt this and try to write something more like I've seen in Backbone (extend method). See I set about it, and run into problems. Then I came across this SO post SO post. It was so much simpler. So after some minor tweaks to that I set about writing a way each extended constructor could have their own extend method (inherited from Base). This is what I have:

var Base = function(name){
 this.name = name;
}
Base.prototype.extend = function(new_constructor){
 //new_constructor.prototype = new this.constructor();
 new_constructor.prototype = Object.create(this.constructor.prototype);
 new_constructor.prototype.constructor = new_constructor;
 
 return new_constructor;
}
var Robot = Base.prototype.extend(function(name, material){
 Base.call(this, name)
 this.material = material
 this.type = 'robot';
});
var T1000 = Robot.prototype.extend(function(options){
 Robot.call(this, options.name, options.material)
 this.guns = options.guns;
 this.type = 'killer robot';
});
robot = new Robot("Boutros", "metal");
t1000 = new T1000({name: "Arnie", material:"flesh/metal", guns:3});
console.log(robot)
console.log(t1000)

Anyway seems to work, but I'm a little new to this. Can anyone suggest any limitations with this approach. Also, I'd like to move the [Constructor].call out of the new constructor function and into the extend function so this is handled automatically, not sure if I can. Would appreciate your feedback.

Btw, here is my jsFiddle

I've been following along a blog post on JavaScript inheritance. I wanted to slightly adapt this and try to write something more like I've seen in Backbone (extend method). See I set about it, and run into problems. Then I came across this SO post. It was so much simpler. So after some minor tweaks to that I set about writing a way each extended constructor could have their own extend method (inherited from Base). This is what I have:

var Base = function(name){
 this.name = name;
}
Base.prototype.extend = function(new_constructor){
 //new_constructor.prototype = new this.constructor();
 new_constructor.prototype = Object.create(this.constructor.prototype);
 new_constructor.prototype.constructor = new_constructor;
 
 return new_constructor;
}
var Robot = Base.prototype.extend(function(name, material){
 Base.call(this, name)
 this.material = material
 this.type = 'robot';
});
var T1000 = Robot.prototype.extend(function(options){
 Robot.call(this, options.name, options.material)
 this.guns = options.guns;
 this.type = 'killer robot';
});
robot = new Robot("Boutros", "metal");
t1000 = new T1000({name: "Arnie", material:"flesh/metal", guns:3});
console.log(robot)
console.log(t1000)

Anyway seems to work, but I'm a little new to this. Can anyone suggest any limitations with this approach. Also, I'd like to move the [Constructor].call out of the new constructor function and into the extend function so this is handled automatically, not sure if I can. Would appreciate your feedback.

Btw, here is my jsFiddle

I've been following along a blog post on JavaScript inheritance. I wanted to slightly adapt this and try to write something more like I've seen in Backbone (extend method). See I set about it, and run into problems. Then I came across this SO post. It was so much simpler. So after some minor tweaks to that I set about writing a way each extended constructor could have their own extend method (inherited from Base). This is what I have:

var Base = function(name){
 this.name = name;
}
Base.prototype.extend = function(new_constructor){
 //new_constructor.prototype = new this.constructor();
 new_constructor.prototype = Object.create(this.constructor.prototype);
 new_constructor.prototype.constructor = new_constructor;
 
 return new_constructor;
}
var Robot = Base.prototype.extend(function(name, material){
 Base.call(this, name)
 this.material = material
 this.type = 'robot';
});
var T1000 = Robot.prototype.extend(function(options){
 Robot.call(this, options.name, options.material)
 this.guns = options.guns;
 this.type = 'killer robot';
});
robot = new Robot("Boutros", "metal");
t1000 = new T1000({name: "Arnie", material:"flesh/metal", guns:3});
console.log(robot)
console.log(t1000)

Anyway seems to work, but I'm a little new to this. Can anyone suggest any limitations with this approach. Also, I'd like to move the [Constructor].call out of the new constructor function and into the extend function so this is handled automatically, not sure if I can. Would appreciate your feedback.

Btw, here is my jsFiddle

Source Link
Martyn
  • 233
  • 2
  • 6

Good way to write extend method of a contructor?

I've been following along a blog post on JavaScript inheritance. I wanted to slightly adapt this and try to write something more like I've seen in Backbone (extend method). See I set about it, and run into problems. Then I came across this SO post. It was so much simpler. So after some minor tweaks to that I set about writing a way each extended constructor could have their own extend method (inherited from Base). This is what I have:

var Base = function(name){
 this.name = name;
}
Base.prototype.extend = function(new_constructor){
 //new_constructor.prototype = new this.constructor();
 new_constructor.prototype = Object.create(this.constructor.prototype);
 new_constructor.prototype.constructor = new_constructor;
 
 return new_constructor;
}
var Robot = Base.prototype.extend(function(name, material){
 Base.call(this, name)
 this.material = material
 this.type = 'robot';
});
var T1000 = Robot.prototype.extend(function(options){
 Robot.call(this, options.name, options.material)
 this.guns = options.guns;
 this.type = 'killer robot';
});
robot = new Robot("Boutros", "metal");
t1000 = new T1000({name: "Arnie", material:"flesh/metal", guns:3});
console.log(robot)
console.log(t1000)

Anyway seems to work, but I'm a little new to this. Can anyone suggest any limitations with this approach. Also, I'd like to move the [Constructor].call out of the new constructor function and into the extend function so this is handled automatically, not sure if I can. Would appreciate your feedback.

Btw, here is my jsFiddle

default

AltStyle によって変換されたページ (->オリジナル) /