The main thing I would like you all is to understand the following. Let’s walk t
ID: 3584892 • Letter: T
Question
The main thing I would like you all is to understand the following. Let’s walk through this code and see what’s happening.
First, you define a constructor function that takes a single argument, personName. Within the constructor, two properties, name and info, and one method, showInfo, are define.
You crate two objects, instantiating each with a different name property. Having created these two person objects, you then decide to add a further method, sayHello, to the person object definition. You do so using the prototype keyword.
After you finish the following coding, save it as newmethod.htmland Click on the four buttons visible on the pages shows that the initially defined showInfo method is still intact, but the new sayHello method operates too, and is available for both of the existing instances of the object type.
<html>
<head>
<title>object oriented programming</title>
<script>
function person(personName)
this.name =personName;
this.info = 'I am called' + this.name;
this.showinfo = function() {
alert(this.info);
}
}
var person1 =new person('Adam');
var person2 =new person ('Eve');
person.prototype.sayHello =funtion() {
alert(this.name + "says hello");
}
</script>
</head>
<body>
<input type ="button" value ="show info on Adam" on click ="Person1.showInfo()"/>
<input type ="button" value ="show info on Eve" on click ="Person2.showInfo()"/>
<input type ="button" value ="say Hello Adam" on click ="Person1.sayHello()"/>
<input type ="button" value ="say Hello Eve" on click ="Person2.sayHello()"/>
</body>
</html>
Explanation / Answer
Here is your html code:
HTML Code:
<html>
<head>
<title>object oriented programming</title>
<script>
function person(personName){
this.name =personName;
this.info = 'I am called ' + this.name;
this.showInfo = function() {
window.alert(this.info);
}
}
var person1 =new person('Adam');
var person2 =new person ('Eve');
person.prototype.sayHello =function() { //you wrote funtion it function
window.alert(this.name + " says hello");
}
</script>
</head>
<body>
<input type ="button" value ="show info on Adam"/><!-- Its onclick not on click and its person1 not Person1-->
<input type ="button" value ="show info on Eve"/>
<input type ="button" value ="say Hello Adam"/>
<input type ="button" value ="say Hello Eve"/>
</body>
</html>
Your code if fine it just had some typo errors. Use above error free code
I hope this solves your problem
Comment if you have any problem in above answer
And please give it a thumbs up if it solved your problem :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.