JS Functions and Prorotype: 1
by Aditya Jain
HTML
1. <button onclick="alert(John.shoutYourName())">John.shoutYourName()</button>
<br>
2. <button onclick="alert(Donn.shoutYourName())">Donn.shoutYourName()</button>
<br>
3. <button onclick="alert(John.shhhYourName())">John.shhhYourName()</button>
<br>
4. <button onclick="alert(Donn.shhhYourName())">Donn.shhhYourName()</button>
<br>
5. <button onclick="changeJohnsFunction()">Change John.shoutYourName</button>
<br>
6. <button onclick="changePrototypeFunction()">Change Person.prototype.shhhYourName</button>
JavaScript
Person = function(name, age){
this.name = name;
this.age = age;
this.shoutYourName = function(){
return 'SHOUTING '+ this.name;
};
};
John = new Person('John',25);
Donn = new Person('Donn',25);
console.log(John.shoutYourName()) // SHOUTING John
console.log(Donn.shoutYourName()) // SHOUTING Donn
Person.prototype.shhhYourName = function(){
return 'shhh ' + this.name;
};
//changing defintion
changeJohnsFunction = function(){
John.shoutYourName = function(){
return 'SHOUTING ' + this.name + '!!!!!';
};
alert('Changed...\nTry clicking button 1 and 2: Behaviour is changed');
};
changePrototypeFunction = function(){
Person.prototype.shhhYourName = function(){
return 'shhh ' + this.name + '!!!!!';
};
alert('Changed...\nTry clicking button 3 and 4: Behaviour is similar');
}