search
LoginSignup
0

More than 5 years have passed since last update.

Organization

[クイズ]javascriptと仲良くなるための一歩 第20話「関数で不変オブジェクト化~Object.preventExtensions~」

問題

var obj = {x:2, y:3};
Object.preventExtensions(obj);

obj.z = 4;
Object.keys(obj); //=>?

delete obj.y;
Object.keys(obj); //=>?

obj.x = 20;
obj.x; //=>?

: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 = {x:2, y:3};
Object.preventExtensions(obj);

//プロパティ追加はできない
obj.z = 4;
Object.keys(obj); //=>["x", "y"]

//プロパティ削除は可能
delete obj.y;
Object.keys(obj); //=>["x"]

//プロパティ値変更は可能
obj.x = 20;
obj.x; //=> 20

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0