LoginSignup
1
0

More than 5 years have passed since last update.

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

Posted at

問題

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

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

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

//プロパティ値変更はできない
obj.x = 20;
obj.x; //=> 2
1
0
0

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
  3. You can use dark theme
What you can do with signing up
1
0