For this question, I'm not expecting a solution to solve something but would like to understand things better ..
Some quote from the specifications:
Edition 5.1 (Link)
§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.
Edition 6 - draft (Link)
§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.
If I understood correctly, both of the specs allow the following code to be executed:
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'
Seems it created an object of a type derives from (sorry for the poor terminology .. ) Function
as I tested in FireFox, and so x
is non-invocable:
x(); // x is not a function
I'm thinking about why doesn't it either disallow a constructor to be used as O
or just create a valid constructor.
So I'm wondering what would you expect Object.create to do with a constructor?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…