push the value 1 into its a property (an array)
directly modify its a property to be the array [1,2,3]
delete its a property entirely
After each step I will print out the state of each instance, at which point some drastic differences in the two programs will become apparent.
First, here’s a simple example of class inheritance and instantiation in Ruby:
And here’s what running that Ruby script outputs:
I create two instances, baz and qux. When I modify properties on baz, properties on qux are not changed. This makes sense.
Here are the differences highlighted.
So, this raises a few questions:
Subsequently, why does baz.a = [1,2,3]; not modify qux.a?
And why is baz.a still 1 after delete baz.a;?
Well, baz and qux do not have their own property a. Instead, they share a from their prototype.
In the next step, I directly define a property a on baz which equals [1,2,3].
Unlike a class, which is like a blueprint for an instance, an object’s prototype is itself another object.
A prototype, being just an object, can be accessed and modified at runtime.