Changing the type of an object in JavaScript

I have an array of an existing object defined using JSON. Objects obviously belong to the type of object. How to associate them with a custom object type to give them specific functions?

+7
javascript javascript-objects
source share
5 answers

The method that will work in all browsers is to either increase each element in the array using the properties and methods you want, or pass the object to the constructor, and create a new object based on the old properties and methods of the object.

Or if you are not interested in IE:

var obj = { name : "Jeremy" }; function CustomType() { this.name = this.name || "someValue"; this.greeting = "Hi"; } CustomType.prototype.sayHi = function() { alert(this.greeting + ", " + this.name); }; obj.__proto__ = CustomType.prototype; obj.constructor.call(obj); obj.sayHi(); 
+11
source share

I do not think that there is a way to change the type of an object after it is created. Fortunately, you probably don't really need to change the class of your objects — you are likely to be satisfied with providing objects with new methods and other functions. (The only real difference is what happens if you change the class and most people do not change the classes while the code is running.)

I will serve as an example for you. First, I will create a simple object to represent the JSON objects that you have:

 var myobj = new Object() myobj.name = "Fred" 

Then I will create a class that you would like to assign myobj :

 function Speaker() { this.speak = function() { window.alert("Hello, " + this.name); } } 

This new Speaker class has some useful features: it can use the speak() method to output useful information:

 var s = new Speaker() s.name = "Sally" s.speak() 

The execution that gave me the message " Hello, Sally ." Unfortunately, you do not need a NEW object (for example, s ) with this functionality, you want an existing object ( myobj ) to have it. Here's how you do it:

 myobj.speak = s.speak 

Now that I am doing this:

 myobj.speak() 

I see the message " Hello, Fred ".

To summarize: create an object that does what you want. Copy all methods (and any helper variables) into the new object. Except for some unusual uses of inheritance, this will cause your new object to behave as it wishes.

+4
source share

If you want to use a specific method on them, you can use apply / call.

Or you can copy methods for your custom object.

 function Object1() { this.aValue = "10"; } function CustomObject() { this.customValue = "6"; } function convertToCustom(obj) { var custom = new CustomObject(); for( var key in obj ) { custom[key] = obj[key]; } return custom; } var obj = new Object1(); var custom = convertToCustom(obj); console.log(custom.customValue); // <- 6 console.log(custom.aValue); // <- 10 
+2
source share

If you don't need a constructor, you can do it like:

 Object.assign(new CustomType, {}); 
+1
source share

As a complement to Jeremy's answer, you can use Object.create instead of directly playing with proto . You will also need an “advanced” function.

So, given Jeremy’s example, you might have something like this:

 var obj = { name : "Jeremy" }; function CustomType() { } CustomType.prototype.init = function(){ this.name = this.name || "someValue"; this.greeting = "Hi"; return this; //let make it fluent } CustomType.prototype.sayHi = function() { console.log(this.greeting + ", " + this.name); }; function extend(obj, props) { for(prop in props) { if(props.hasOwnProperty(prop)) { obj[prop] = props[prop]; } } return obj; //let make it fluent } obj = extend(Object.create(CustomType.prototype), obj).init(); obj.sayHi(); 
0
source share

All Articles