为什么不能使用const关键字分配JavaScript对象的常量属性? [英] Why is it impossible to assign constant properties of JavaScript objects using the const keyword?

查看:113
本文介绍了为什么不能使用const关键字分配JavaScript对象的常量属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我问是否有可能:如何使用const关键字创建Java常量作为对象的属性?

First, I had asked is it possible: How to create Javascript constants as properties of objects using const keyword?

现在,我要问:为什么?对我来说,答案似乎是正因为如此,但这样做是很有用的:

Now, I gotta ask: why? The answer to me seems to be 'just because', but it would be so useful to do something like this:

var App = {};  // want to be able to extend
const App.goldenRatio= 1.6180339887  // throws Exception

为什么在激活对象上设置的常量可以工作,而在其他对象上设置的常量却不能工作?

Why can constants set on an activation object work but not when set on they are set on any other?

如果可能的话,会造成什么样的破坏?

What sort of damage can be done if this were possible?

const 的目的是什么,如果不是防止更改公共API的话?

What is the purpose of const, if not to prevent a public API from being altered?

推荐答案

要回答您要问的问题,并说不要使用 const 。口译员不做任何事情。它所做的一切都是在误导开发人员以为他/她可以认为值永远不会改变,这很可能就像 const 关键字不存在一样。

Going to answer the question you meant to ask and say never use const. The interpreter doesn't do anything with it. All it does is mislead the developer into thinking s/he can assume that the value never changes, which is about as likely as if the const keyword weren't present.

这篇关于为什么不能使用const关键字分配JavaScript对象的常量属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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