JavaScript

[クイズ]javascriptと仲良くなるための一歩 第28話「nullでcreate1」

More than 1 year has passed since last update.

問題

var obj = {}
Object.getPrototypeOf(obj); //=> Object {__defineGetter__: function, ~省略~}
'toString' in obj;          //=> true

var obj = Object.create(null);
Object.getPrototypeOf(obj); //=> ?
'toString' in obj;          //=> ?

:mouse:
:cow:
:tiger:
:rabbit:
:dragon_face:
:snake:
:horse:
:sheep:
:monkey_face:
:bird:
:dog:
:boar:
:mouse:
:cow:
:tiger:
:rabbit:
:dragon_face:
:snake:
:horse:
:sheep:
:monkey_face:
:bird:
:dog:
:boar:

答え

var obj = {}
Object.getPrototypeOf(obj); //=> Object {__defineGetter__: function, ~省略~}
'toString' in obj;          //=> true

var obj = Object.create(null);
Object.getPrototypeOf(obj); //=> null
'toString' in obj;          //=> false

creteで{}作りたいなら、

var obj = Object.create(Object.prototype);
Object.getPrototypeOf(obj); //=> Object {__defineGetter__: function, ~省略~}
'toString' in obj;          //=> true