Java Reference
In-Depth Information
a = {};
<< {}
b = { test: [1,2] };
<< { test: [1,2] }
c = { nest: { d: "nested" } };
<< { nest: { d: "nested" } }
a.mixin(b,c);
<< {"nest": {"d": "nested"}, "test": [1, 2]}
b.test.push(3);
<< 3
a.test
<< [1, 2]
b.test;
<< [1, 2, 3]
It works as expected―the changes only affect the object they are acted on.
The mixin() method is a particularly powerful way of dealing with objects, and has a
number of uses.
Using Mixins to Add Properties
One use for the mixin() method is to add a large number of properties to an object all
at once. For example, we can instantiate a new Superhuman object and then add all its
individual properties in one go, instead of one at a time as we did earlier, while avoiding
having to use the more verbose property descriptor notation:
wonderwoman = Object.create(Superhuman);
<< {"arms": 2, "change": function () {
return this.realName + " goes into a phone box and comes
out as "
↵ + this.name;
Search WWH ::




Custom Search