search
LoginSignup
0

More than 5 years have passed since last update.

Organization

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

問題

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

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

//プロパティ削除はできない
delete obj.y;     //=> false
Object.keys(obj); //=> ["x", "y"]

//プロパティ値変更は可能
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