javascript - Getting undefined for methods defined as properties using new Object() -
i tried following way of having function assigned object. works on chrome(43.0) 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 function object such object.getprototypeof()
, object.defineproperty()
json objects suppose. code sample above uses object constructor, want know how add method object created object constructor. if not possible have methods in object created object constructor, let me know of too. know how use json , methods within or use call , apply, finding out if there way add method objects using new object()
constructor.
thank you.
in firefox function prints out "andrew" , "20" you'd expect.
it print out undefined
thats because function has no return value.
it's nothing worry about, firefox letting know, chrome isnt because wont cause issues.
if worries you, have function return true
obj.print = function(){ console.log( this.name ); console.log( this.age ); return true; }
it show true
in firefox instead of undefined
also make sure have log
tunred on in dev tools log level, if log
ruened off, you'll see undefined
messages.
Comments
Post a Comment