'Freezing' Arrays in Javascript?

前端 未结 3 957
轻奢々
轻奢々 2021-01-01 08:24

Since the ECMA-262 specifications Javascript has gained the Object.freeze() method, which allows for objects, whose properties can not be changed, added or removed.

3条回答
  •  抹茶落季
    2021-01-01 09:20

    Yes, it is applicable to arrays too.

    const arr = [1,2,3,4];
    Object.freeze(arr);
    Object.isFrozen(arr)// true
    arr.push(5) // you will get a type error
    arr.pop() // you will get a type error
    

提交回复
热议问题