最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

immutability - How do I make a JavaScript variable completely immutable? - Stack Overflow

programmeradmin0浏览0评论

I've heard similar questions, but not the answer that I wanted; I do not count const because: 1). it doesn't actually make it immutable, it only makes the reference immutable 2). it messes with the scope, and I want it to work outside the block, too 3). not all browsers support it yet

 {
     const hello = ["hello", "world"];
     hello.push("!!!");
     console.log(hello);//outputs "hello", "world", "!!!"
 }
 //and it doesn't, and shouldn't, work here
     console.log(hello);

I've heard similar questions, but not the answer that I wanted; I do not count const because: 1). it doesn't actually make it immutable, it only makes the reference immutable 2). it messes with the scope, and I want it to work outside the block, too 3). not all browsers support it yet

 {
     const hello = ["hello", "world"];
     hello.push("!!!");
     console.log(hello);//outputs "hello", "world", "!!!"
 }
 //and it doesn't, and shouldn't, work here
     console.log(hello);
Share Improve this question edited Oct 13, 2019 at 17:47 Martijn Pieters 1.1m321 gold badges4.2k silver badges3.4k bronze badges asked Jul 23, 2019 at 20:14 Sapphire_BrickSapphire_Brick 1,68316 silver badges28 bronze badges 2
  • 3 const hello = Object.freeze(["hello", "world"]); – Pointy Commented Jul 23, 2019 at 20:15
  • 2 developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – connexo Commented Jul 23, 2019 at 20:15
Add a ment  | 

3 Answers 3

Reset to default 4

Just use Object.freeze

const immutableArray = Object.freeze([1,2,4])

You can use Object.freeze for this (obviously only on objects).

const hello = Object.freeze(["hello", "world"]);

// hello.push("!!!");
// will throw "TypeError: can't define array index property past the end of an array with non-writable length"

// hello.length = 0;
// will fail silently

// hello.reverse();
// will throw "TypeError: 0 is read-only"

// hello[0] = "peter";
// will fail silently

From MDN:

The Object.freeze() method freezes an object. A frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed. In addition, freezing an object also prevents its prototype from being changed. freeze() returns the same object that was passed in.

However, there is no keyword to define a pletely immutable variable without using Object.freeze or Object.seal on the variable's value.

For a less restrictive approach Javascript also has Object.seal().

The way to do it without const is to use Object.defineProperty, and like I wanted, it behaves like var in terms of scope:

{
    Object.defineProperty(typeof global === "object" ? global : window, "PI", {
        value:        Object.seal(3.141593),
        enumerable:   true,
        writable:     false,
        configurable: false
    });
}
console.log(PI); // 3.141593

The only problem is that it that it doesn't throw an error outside of strict mode.

发布评论

评论列表(0)

  1. 暂无评论