Created
May 10, 2011 05:32
-
-
Save mapserver2007/963947 to your computer and use it in GitHub Desktop.
シンプルな継承パターン1
This file contains 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.prototype.mix = function() { | |
var c = Object.clone(this), prop, pprop, p; | |
for (var i = 0, len = arguments.length; i < len; i++) { | |
for (prop in arguments[i]) { | |
c[prop] = arguments[i][prop]; | |
} | |
} | |
return c; | |
}; | |
var Android = { | |
init: function() { | |
console.log("i am android phone user"); | |
return this; | |
}, | |
getPhoneName: function() { | |
return "android"; | |
} | |
}; | |
var Iphone = { | |
init: function() { | |
console.log("i am iphone user"); | |
return this; | |
}, | |
getPhoneName: function() { | |
return "iphone"; | |
} | |
}; | |
var Feature = { | |
getPhoneType: function() { | |
return "old"; | |
} | |
}; | |
// デメリットはmixメソッドがObjectに勝手についてしまう | |
var o1 = Iphone.mix(Android).init(); | |
//console.log(o1); | |
// 多重継承風(1) | |
var o2 = Iphone.mix(Android).mix(Feature); | |
//console.log(o2); | |
// 多重継承風(2) | |
var o3 = Iphone.mix(Android, Feature); | |
//console.log(o3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment