参考文献:Supercharged JavaScript Graphics: with HTML5 canvas, jQuery, and More より。
いろんなやり方があるが、これが一番自然な方法なんじゃないかと筆者は言っている。
js
<script type="text/javascript">
var pet = function(name, legs) {
var that = {
name: name,
getDetails: function(){
return that.name + ' has ' + legs + ' legs';
}
};
return that;
};
var cat = function(name) {
var that = pet(name, 4);
that.action = function(){
return 'free as a bird';
};
return that;
};
</script>
js
var neko = cat('tama');
こうして neko を作った後で、neko の name を変えることはできるが、legs は変えられない、という訳である。