23
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScript の継承とカプセル化

Posted at

参考文献: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 は変えられない、という訳である。

23
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?