i just tried the following way of having a function assigned for a object. It works on chrome(43.0) but does not work on firefox(36.0.4)
code:
var obj = new Object();
obj.name = "Andrew";
obj.age = 20;
obj.print = function(){
console.log( this.name );
console.log( this.age );
}
obj.print(); // printing undefined in Firefox
I know of other ways of adding a function to an object such as Object.getPrototypeOf()
and Object.defineProperty()
but those are for json objects i suppose. The code sample above uses the object constructor, i want to know how to add a method to an object created with the object constructor. If its just not possible to have methods in an object created with the object constructor, let me know of that too. I know how to use JSON and get the methods within it or use call and apply, this is just for finding out if there is a way to add a method for objects using the new Object()
constructor.
Thank you.