Java Reference
In-Depth Information
print("Object.getPrototypeOf(point) === Object.prototype is " +
(Object.getPrototypeOf(point) === Object.prototype));
print("Object.getPrototypeOf(colorPoint) === point is " +
(Object.getPrototypeOf(coloredPoint) === point));
print("Object.getPrototypeOf(colorPoint) === Object.prototype is " +
(Object.getPrototypeOf(coloredPoint) === Object.prototype));
After setting the prototype for coloredPoint...
(10, 20)
(10, 20, red)
After setting the x and y properties for point...
(100, 200)
(100, 200, red)
[object Object]
[object Object]
Object.getPrototypeOf(point) === Object.prototype is true
Object.getPrototypeOf(colorPoint) === point is true
Object.getPrototypeOf(colorPoint) === Object.prototype is false
You can observe the following points in the code:
point
object with three properties named
x
,
y
,
•
It defines a
and
print
coloredPoint,
with two
properties named
color
and
print
. At this point, the two objects
are unrelated.
•
It defines another object, called
point
object as the prototype of the
coloredPoint
object using the
Object.setPrototypeOf()
function, so now
the
coloredPoint
object inherits from the
point
object. At this
point, the
coloredPoint
object inherits all properties of the
point
object, including the properties
x
and
y
. Notice that only
one copy of x and
y
exists and they are shared by both
point
and
coloredPoint
objects.
•
It sets the
print()
method on both objects. The output confirms
that both objects called their versions of the
print()
method.
This is a case of property overriding. The
coloredPoint
object
overrides the
print()
method of the
point
object. This works the
same as in Java.
•
It calls the
x
and
y
properties on the
point
object and calls the
print()
method of both objects again. The
output confirms that the values of x and y were changed for both
point
and
coloredPoint
objects.
•
It changes the values for the
Search WWH ::
Custom Search