why do we use `Boy.prototype = new Human;` to simulate inheritance?

前端 未结 2 1118
北荒
北荒 2021-01-13 20:54

i don\'t get why everyone is using Boy.prototype = new Human; to simulate inheritance. Look, what we want is the function\'s of A right? we can do that without

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-13 21:17

    • It sets up the prototype chain correctly, meaning that instanceof and isPrototypeOf() will work
    • It's less verbose

    There are simple but ugly workarounds to prevent a constructor function from performing its usual initialization when you're just using it to create a prototype object. For example, you could either check the arguments:

    function Boy(name) {
        if (arguments.length == 1) {
            this.name = name;
            // Do other initialization
        }
    }
    

    ... or move the initialization into a separate method that has to be called explicitly:

    function Boy(name) {}
    
    Boy.prototype.init = function(name) {
        this.name = name;
        // Do other initialization
    }
    

    This has the obvious downside of requiring you to remember to call init() whenever you create a new Boy.

    In ECMAScript 5 environments (current versions of most browsers, for example), a better option may be ECMAScript 5's Object.create(), which allows you to create an object which inherits directly from another object and sets up the prototype chain for you. This can be emulated (but only approximately: see Object.defineProperty in ES5?) in non-ES5 environments:

    if (!Object.create) {
        Object.create = function(o) {
            function F() {}
            F.prototype = o;
            return new F();
        }; 
    }
    

提交回复
热议问题