prototype - JavaScript unable to access same object method after object.create(baseObject) -


i new javascript. have written code incorporating suggested answers. code block working in once scenario , not working in other scenario.

<script langugage="javascript">  var baseobject = {     name:"sunrise",     age:39,     printproperties:function(){         console.log("base class-> name:age:"+this.name+":"+this.age);     } } baseobject.printproperties(); console.log(baseobject);   /* code block works fine */  var derivedobject2=object.create(baseobject);     derivedobject2.education="m.c.a"     derivedobject2.printproperties=function(){           console.log("derived -> name:age:education:"+this.name+":"+this.age+":"+this.education);     }  derivedobject2.printproperties();  console.log(derivedobject2);   /* derivedobject.__proto__ = baseobject; derivedobject.printproperties(); // works fine */  /* code block not work  */  var derivedobject=object.create(baseobject,{       education:{value:"mca"},       //education:"mca",       printproperties:function(){           console.log("derived -> name:age:education:"+this.name+":"+this.age+":"+this.education);         return this;       } });  derivedobject.printproperties(); // getting error here,  console.log(derivedobject);   </script> 

here error:

error:  uncaught typeerror: derivedobject.printproperties not function 

use object.create()

var baseobject = {     name:"sunrise",     age:39,     printproperties:function(){         console.log("name:age:"+this.name+":"+this.age);     } } 

then

var derivedobject=object.create(baseobject);     derivedobject.education="m.c.a"     derivedobject.printproperties=function(){    console.log("name:age:education:"+this.name+":"+this.age+":"+this.education);         }  derivedobject.printproperties(); 

now derivedobject inherit properties of base object

edit can

var derivedobject=object.create(baseobject,{               education:{value:"mca"},               printprop:function(){} }); 

object.create() abstracts of complexity associated prototypes


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -