What it the significance of the Javascript constructor property?

前端 未结 6 1007
一生所求
一生所求 2020-11-22 04:23

Trying to bend by head around Javascript\'s take on OO...and, like many others, running into confusion about the constructor property. In particular, the signif

6条回答
  •  花落未央
    2020-11-22 05:12

    one case to use constructor:

    1. this is one of the common realization of inheritance:

      Function.prototype.extend = function(superClass,override) {
          var f = new Function();
          f.prototype = superClass.prototype;
          var p = this.prototype = new f();
          p.constructor = this;
          this.superclass = superClass.prototype;
          ...
      };
      
    2. this new f() would not call the constructor of superClass,so when you create a subClass,maybe you need call the superClass at first,like this:

      SubClass = function() {
          SubClass.superClass.constructor.call(this);
      };
      

    so the constructor property make sense here.

提交回复
热议问题