A big advantage of using objects is the ability to re-use already-written code in a new context. JavaScript supports extending and inheriting objects via the
prototype
keywordExample:
function Person(personName){
this.name = personName;
this.info = "This person is called " + this.name;
this.showInfo = function(){
console.log(this.info);
};
}
Person.prototype.sayHello = function(){
console.log(this.name + " says Hello");
};
var name = prompt("Who are you?");
person1 = new Person(name);
person1.showInfo();
person1.sayHello();
Output:
This person is called kkondal
kkondal says Hello
No comments:
Post a Comment