Can I set the type of a Javascript object?

后端 未结 5 1457
忘了有多久
忘了有多久 2021-02-02 00:15

I\'m trying to use some of the more advanced OO features of Javascript, following Doug Crawford\'s \"super constructor\" pattern. However, I don\'t know how to set and get types

5条回答
  •  情话喂你
    2021-02-02 00:46

    If you declare Bicycle like this, instanceof will work:

    function Bicycle(tires) {
      this.tires = tires;
      this.toString = function () {
        return 'Bicycle with ' + tires + ' tires.';
      }
    }
    
    var b = new Bicycle(2);
    console.log(b instanceof Bicycle);
    

提交回复
热议问题