LoginSignup
0
0

More than 3 years have passed since last update.

Javascript クラスの書き方 進化版

Last updated at Posted at 2020-07-09

かなりいいと思います。もうこれで決定ですね。
あえて何も説明を書かないが、わかる人がわかると思います。

'use strict'

let Cat = (function() {
    let _share = 'only one';
    let privates = new WeakMap();
    let $ = function(o) {
        return privates.get(o);
    };
    function Cat(name) {
        privates.set(this, {});
        $(this).name = name;
    }
    Cat.prototype = {
        //self:function(){return privates.get(this);},
        init:function(){},
        load:function(){},
        say:function(){console.log('my name is', $(this).name)},
        getShare:function(){console.log(_share);},
        setShare:function(newValue){_share = newValue}
    };
    return Cat;
})();

let cat1 = new Cat('cat1');
let cat2 = new Cat('cat2');

console.log(cat1);
console.log(cat1 === cat2);
console.log(cat1.init === cat2.init);
console.log(cat1.load === cat2.load);
console.log(cat1.name);
cat1.say();
cat2.say();
cat1.getShare();
cat2.getShare();
cat1.setShare('share changed by cat1');
cat1.getShare();
cat2.getShare();

Image 1.png

Q:継承はとうなるの?
A:継承?Javascript 継承させる?

0
0
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
0
0