我应该在ECMAScript 2015中的const上使用Object.freeze吗? [英] Should I use Object.freeze on a const in ECMAScript 2015?

查看:171
本文介绍了我应该在ECMAScript 2015中的const上使用Object.freeze吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管在ECMAScript 2015中不能将 const 分配给这个事实,但如果对象未被冻结 Object,则可以对其进行修改。冻结



由于 const 应该是不可变的,是否有意义添加 Object.freeze 以冻结对象?

大多数用例在声明后不需要修改内容。我想到的一个例外是将一个分配给一个 const (例如 const MyClass =类MyClass {} ),在这种情况下,属性需要更改(至少大多数用途)。



我不是在说ECMAScript 2015是否应该改变?我想知道我是否应该在日常代码中使用 Object.freeze const



示例



  const firstNames = ['John','Daisy']; 

firstNames = [] //默认失败
firstNames // => ['John','Daisy']

firstNames [0] ='替换! //没有错误,它的工作原理
firstNames // => ['替换!','雏菊']



使用 Object.freeze



  const firstNames = Object.freeze(['John','Daisy']); 

firstNames = [] //抛出错误
firstNames // => ['John','Daisy']

firstNames [0] ='替换! //没有错误,它的工作原理
firstNames // => ['替代!','雏菊']


解决方案


我应该在日常代码中使用Object.freeze作为const吗?


我认为这是一个很好的做法。它确实赋予您与 const 相同的优点 - 失败而不是执行(在严格模式下)。这也是一种更具声明性的代码风格,清楚地表明了该对象不应该被突变的意图。



然而,iirc冻结对象仍然比正常情况慢一些那些。如果你有真正的性能关键代码,你必须知道这一点。



b



$ b

然而,您不应该依赖于访问冻结对象时抛出的异常。很可能你不得不将代码转移到不支持冻结的ES3。



1)人们每天使用的越多,引擎越早优化这一点 - 它已经过期了,如下: - )


Despite the fact that a const cannot be assigned to in ECMAScript 2015, the object can be modified if it isn't frozen with Object.freeze.

Since a const is supposed to be immutable, would it make sense to add Object.freeze to freeze the object?
Most use cases would not require the contents being modifiable after being declared. One exception I thought of would be assigning a class to a const (e.g. const MyClass = class MyClass {}), in which case the properties would need to be changeable (at least for most uses).

I'm not talking about whether ECMAScript 2015 should be changed; I was wondering about whether I should use Object.freeze for consts in everyday code.

Example

const firstNames = ['John', 'Daisy'];

firstNames = []                 // Fails silently
firstNames                      // => ['John', 'Daisy']

firstNames[0] = 'Replaced!';    // No error and it works!
firstNames                      // => ['Replaced!', 'Daisy']

Example with Object.freeze

const firstNames = Object.freeze(['John', 'Daisy']);

firstNames = []                 // Throws an error
firstNames                      // => ['John', 'Daisy']

firstNames[0] = 'Replaced!';    // No error and it works!
firstNames                      // => ['Replaced!', 'Daisy']

解决方案

Should I use Object.freeze for consts in everyday code?

Yes, I think that would be a good practise. It does give you the same advantages as const - failing on mistakes instead of carrying on (in strict mode, of course). It's also a more declarative style of code, clearly stating the intention that the object is not supposed to be mutated.

Yet, iirc frozen objects are still a bit slower than normal ones. If you have really performance-critical code, you'll have to know this. It shouldn't stop you from using Object.freeze in general, though1.

You shouldn't however rely on the exceptions thrown when accessing frozen objects. It's likely that you have to transpile your code to ES3 where freezing is not supported.

1) And the more people use it every day, the sooner engines will optimise this - it's overdue imo :-)

这篇关于我应该在ECMAScript 2015中的const上使用Object.freeze吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆