Я новичок в javascript. Я написал некоторый код, включив предлагаемые ответы. Теперь блок кода работает один раз и не работает в другом сценарии.
<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);
/* This 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
*/
/* This code block does 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>
Вот моя ошибка:
Error: Uncaught TypeError: derivedObject.printProperties is not a function