x is not a function … what would you expect Object.create to do with a constructor
对于这个问题,我不希望有解决方案来解决某些问题,但希望更好地理解。
规范中的一些引述:
-
版本5.1(链接)
§15.2.3.5 Object.create ( O [, Properties] )
The create function creates a new object with a specified prototype. When the create function is called, the following steps are taken:
- If Type(O) is not Object or Null throw a TypeError exception.
- Let obj be the result of creating a new object as if by the expression new Object() where Object is the standard built-in constructor with that name
- Set the [[Prototype]] internal property of obj to O.
- If the argument Properties is present and not undefined, add own properties to obj as if by calling the standard built-in function Object.defineProperties with arguments obj and Properties.
- Return obj.
-
第6版-草稿(链接)
§19.1.3.2 Object.create ( O [, Properties] )
The create function creates a new object with a specified prototype. When the create function is called, the following steps are taken:
- If Type(O) is not Object or Null throw a TypeError exception.
- Let obj be the result of the abstract operation ObjectCreate with argument O.
- If the argument Properties is present and not undefined, then
a. Return the result of the abstract operation ObjectDefineProperties(obj, Properties).- Return obj.
如果我理解正确,那么两个规范都允许执行以下代码:
1 2 3 4 5 6 7 8 9 | function F() { } var x=Object.create(F); // a minimal test alert(x.prototype.constructor===F); // true alert(x instanceof Function) // true alert(typeof x) // 'object' |
就像我在FireFox中测试的那样,它似乎创建了某种类型的对象(对不起的术语..)
1 | x(); // x is not a function |
我在考虑为什么不禁止将构造函数用作
所以我想知道您希望Object.create对构造函数做什么?
不幸的是,这行不通。您所拥有的是在其原型链中包含
仅通过函数声明或函数表达式创建的对象将以" Function"作为其[[Class]],并具有一个[[Call]]方法,这使它可以被调用。它们是根据ECMAScript 5规范的13.2节中详述的步骤创建的。
如您在报价中所见,
我不确定在ES6中是否会更改其中任何一个,但我想不会。
So I'm wondering what would you expect Object.create to do with a constructor?
我希望它会遵循规范……
I'm thinking about why doesn't it either disallow a constructor to be used as O
为什么要这样每个构造函数都是一个对象(第8.6节)。
… or just create a valid constructor.
规范说应该创建一个普通对象(如
1
2
3 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
实际上是从