-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrototypeObject.html
30 lines (22 loc) · 1.42 KB
/
PrototypeObject.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!DOCTYPE html>
<html>
<body>
<script>
//3. Prototype Object Attribute
function People () {
this.superstar = "Michael Jackson";
}
// Define "athlete" property on the People prototype so that "athlete" is accessible by all objects that use the People () constructor.
People.prototype.athlete = "Tiger Woods";
var famousPerson = new People ();
famousPerson.superstar = "Steve Jobs";
// The search for superstar will first look for the superstar property on the famousPerson object, and since we defined it there, that is the property that will be used. Because we have overwritten the famousPerson’s superstar property with one directly on the famousPerson object, the search will NOT proceed up the prototype chain.
console.log (famousPerson.superstar); // Steve Jobs -- property shadowing
// Note that in ECMAScript 5 you can set a property to read only, and in that case you cannot overwrite it as we just did.
// This will show the property from the famousPerson prototype (People.prototype), since the athlete property was not defined on the famousPerson object itself.
console.log (famousPerson.athlete); // Tiger Woods
// In this example, the search proceeds up the prototype chain and find the toString method on Object.prototype, from which the Fruit object inherited—all objects ultimately inherits from Object.prototype as we have noted before.
console.log (famousPerson.toString()); // [object Object]
</script>
</body>
</html>