Created
May 14, 2014 16:07
-
-
Save shoyan/55a315fdee3b6310e7fa to your computer and use it in GitHub Desktop.
Object.createを使ったMixInのサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Object.create(): the New Way to Create Objects in JavaScript | |
* http://www.htmlgoodies.com/beyond/javascript/object.create-the-new-way-to-create-objects-in-javascript.html | |
*/ | |
var Car2 = Object.create(null); //this is an empty object, like {} | |
Car2.prototype = { | |
getInfo: function() { | |
return 'A ' + this.color + ' ' + this.desc + '.'; | |
} | |
}; | |
var car2 = Object.create(Car2.prototype, { | |
//value properties | |
color: { writable: true, configurable:true, value: 'red' }, | |
//concrete desc value | |
rawDesc: { writable: false, configurable:true, value: 'Porsche boxter' }, | |
// data properties (assigned using getters and setters) | |
desc: { | |
configurable:true, | |
get: function () { return this.rawDesc.toUpperCase(); }, | |
set: function (value) { this.rawDesc = value.toLowerCase(); } | |
} | |
}); | |
car2.color = 'blue'; | |
alert(car2.getInfo()); //displays 'A RED PORSCHE BOXTER.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment