I am currently transforming from Java to Javascript, and it's a bit hard for me to figure out how to extend objects the way I want it to do.
I've seen several people on the internet use a method called extend on object. The code will look like this:
var Person = {
name : 'Blank',
age : 22
}
var Robot = Person.extend({
name : 'Robo',
age : 4
)}
var robot = new Robot();
alert(robot.name); //Should return 'Robo'
Does anyone know how to make this work? I've heard that you need to write
Object.prototype.extend = function(...);
But I don't know how to make this system work. If it is not possible, please show me another alternative that extends an object.
Then:
Update 01/2017:
Please, Ignore my answer of 2015 since Javascript is now supports
extends
keyword since ES6 (Ecmasctipt6 )- ES6 :
- ES7 :
In ES6, there is
Object.assign
for copying property values. Use{}
as first param if you don't want to modify the target object (the first param passed).var resultObj = Object.assign({},Obj1,Obj2);
For more details see link,
MDN - Object.assign()
In case if you need is a Polyfill for ES5, the link offers it too. :)
World without the "new" keyword.
And simpler syntax with Object.create().
I'm in the camp that believes Javascript should try to live without "new". It is a classless language, it doesn't need constructors. You simply create Objects and then extend or morph them. Granted, there are pitfalls, but this is so much more powerful and simple:
Extending the base prototype
One more level deeper
Further reading
** Update 3 Oct 18. Adopt ES6 syntax and use of
const
andlet
. Added example to show how to make properties immutable.** Update 22 Jan 17. Included ES6 Object.assign().
As you can see the assignment requires multiple statements. With ES6, you can use the #assign method to shorten the assignments. (For polyfill to use on older browsers, see MDN on ES6.)
You can also use Object.create()'s second argument a.k.a propertiesObject, which I find to be a little too lengthy. The only reason to use it over #assign is if you need more control over the values i.e writability/configurability etc... Notice how
Robot
is strictly to be all made of metal.And all prototypes of
Robot
cannot be made of something else.There are gotchas to this pattern that are likely to trip "classical-trained" programmers. Nonetheless, I find this pattern so much more readable.
Prototyping is a nice way, but prototype is quite dangerous sometimes and can lead to bugs. I prefer to encapsulate this into a base object, like Ember.js does to it's Ember.Object.extend and Ember.Object.reopen. That is much more secure to use.
I created a gist with how you would setup something similar to what Ember.Object uses.
Here's the link: https://gist.github.com/WebCloud/cbfe2d848c80d4b9e9bd
If you haven't yet figured out a way, use the associative property of JavaScript objects to add an extend function to the
Object.prototype
as shown below.You can then use this function as shown below.
And another year later, I can tell you there is another nice answer.
If you don't like the way prototyping works in order to extend on objects/classes, take alook at this: https://github.com/haroldiedema/joii
Quick example code of possibilities (and many more):