I have several javascript objects like this:
var object = {
name: "object name",
description: "object description",
properties: [
{ name: "first", value: "1" },
{ name: "second", value: "2" },
{ name: "third", value: "3" }
]
};
Now I wanted to change these objects to smarter objects (adding some methods etc).
At first I made a constructor function like this:
SmartObject = function( object ){
this.name = object.name;
this.description = object.description;
this.properties = object.properties;
};
SmartObject.prototype.getName = function(){
return this.name;
};
SmartObject.prototype.getDescription = function(){
return this.description;
};
SmartObject.prototype.getProperies = function(){
return this.properties;
};
And then I used this constructor to change my object to a SmartObject
instance like this:
var smartObject = new SmartObject( object );
This seems the proper Object Oriented javascript code to do this, but this feels so overly complicated since all I actually want to do is add some methods and now I copy all properties from my object
to my SmartObject
in the constructor function.
In this example there are only 3 properties and some simple methods, but in my project there are several dozens of (nested) properties and much more complex methods.
So then I tried this:
object.__proto__ = SmartObject.prototype;
And this seems to result in exactly the same result and seems much easier (check this fiddle for this example).
Is this a proper and acceptable way to add the prototype to my object? Or is this breaking object oriented patterns and considered bad practice and should I continue doing like I did (using the constructor).
Or is there maybe another more acceptable way to add the methods to my existing object without pulling it through the constructor function?
Note. I tried to find such example on StackOverflow, but when I search I always end up in examples extending the prototype of existing javascript classes. If there is such question feel free to mark this as a duplicate and I will close my question again.
Using a constructorIn JavaScript, all functions have a property named prototype . When you call a function as a constructor, this property is set as the prototype of the newly constructed object (by convention, in the property named __proto__ ).
constructor. The constructor property returns a reference to the Object constructor function that created the instance object. Note that the value of this property is a reference to the function itself, not a string containing the function's name.
JavaScript Constructor Examples In JavaScript, multiple objects can be created in a constructor: //Constructor function User() { this.name = 'Bob'; } var user1 = new User(); var user2 = new User(); In the above example, two objects are created using the same constructor.
As I previously mentioned, changing an object's prototype is going to have a severe impact on your code's performance. (tbh, I've never taken the time to measure the impact). This MDN page explains.
However, if you're issue is about boilerplate, you could easily create a generic factory for your objects, such as:
function genericFactory(proto, source) {
return Object.keys(source).reduce(function(target, key) {
target[key] = source[key];
return target;
}, Object.create(proto));
}
And now you can use it by passing your SmartObject.prototype
and object
as arguments like this:
var smartObject = genericFactory(SmartObject.prototype, object);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With