x is not a function … what would you expect Object.create to do with a constructor

前端 未结 2 1141
后悔当初
后悔当初 2020-12-19 07:13

For this question, I\'m not expecting a solution to solve something but would like to understand things better ..

Some quote from the specifications:

2条回答
  •  半阙折子戏
    2020-12-19 07:46

    So I'm wondering what would you expect Object.create to do with a constructor?

    I would expect it to follow the spec of course…

    I'm thinking about why doesn't it either disallow a constructor to be used as O

    Why should it? Every constructor function is an object (§8.6).

    … or just create a valid constructor.

    The spec says it should create a plain object (as by new Object), whose [[prototype]] is set to O. Plain objects are no functions, they don't have a [[call]] or [[construct]] property. It also will have [[class]] Object, not Function.

    x.prototype.constructor===F // true
    x instanceof Function // true
    typeof x // 'object'
    

    Seems it created an object of a type derives from (sorry for the poor terminology .. ) Function

    From F, actually. It does inherit the .prototype property from F (that's why your first test is true), and through F it also inherits from Function.prototype which makes it instanceof Function. Yet, it doesn't have a [[call]] property (it's not callable) so typeof does not yield "function", but just "object".

提交回复
热议问题