Java Reference
In-Depth Information
}, "legs": 2, "walk": function () {
console.log("Walking"); }}
Instead of assigning each property, one at a time: = "Wonder Woman";
<< "Wonder Woman"
wonderwoman.realName = "Diana Prince";
<< "Diana Prince"
We can just mix in an object literal and add both properties at once:
wonderwoman.mixin({ name: "Wonder Woman", realName: "Diana
<< {"arms": 2, "change": function () {
return this.realName + " goes into a phone box and comes
out as "
}, "legs": 2, "name": "Wonder Woman", "realName": "Diana
"walk": function () { console.log("Walking"); }}
Using Mixins to Create a copy() Method
Another use of the mixin() method is to create a copy() method that is used to make
an exact, deep copy of an object. This is added to the Object prototype object so that
it's inherited by all objects. It uses the Object.create() method to create a new ob-
ject that inherits from the same prototype as the object calling the method. The mixin()
method is then used to add all the properties and methods of the object calling the method
to the new object, which is then returned:
Object.defineProperty(Object.prototype, 'copy', {
enumerable: false,
writable: false,
configurable: false,
Search WWH ::

Custom Search