Skip to content

Instantly share code, notes, and snippets.

@shoyan
Created May 14, 2014 16:07
Show Gist options
  • Save shoyan/55a315fdee3b6310e7fa to your computer and use it in GitHub Desktop.
Save shoyan/55a315fdee3b6310e7fa to your computer and use it in GitHub Desktop.
Object.createを使ったMixInのサンプル
/*
* 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