JavaScript

[クイズ]javascriptと仲良くなるための一歩 第16話「プロトタイプ継承 delete」

More than 1 year has passed since last update.

問題

第一問

var map = {x:1}        //=> undefined
map                    //=> Object {x: 1}
map.toString           //=> function toString() { [native code] }
map['toString'] = 1    //=> 1
map.toString           //=> ?
delete map["toString"] //=> true
map.toString           //=> ?

第二問

var map = {x:1}        //=> undefined
map                    //=> Object {x: 1}
map.toString()         //=> "[object Object]"
map['toString'] = 1
map.toString()         //=> ?
delete map["toString"] //=> true
map.toString()         //=> ?

: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 map = {x:1}        //=> undefined
map                    //=> Object {x: 1}
map.toString           //=> function toString() { [native code] }
map['toString'] = 1    //=> 1
map.toString           //=> 1
delete map["toString"] //=> true
map.toString           //=> function toString() { [native code] }

第二問

var map = {x:1}        //=> undefined
map                    //=> Object {x: 1}
map.toString()         //=> "[object Object]"
map['toString'] = 1
map.toString()         //=> Uncaught TypeError: map.toString is not a function
                       //   at <anonymous>:1:5
delete map["toString"] //=> true
map.toString()         //=> "[object Object]"