javascript - 浏览器 API 返回的对象是 const 吗?

标签 javascript cross-browser immutability

例如考虑 Position我可以通过 API 调用获取对象。

如果我多次调用 API,在编辑/更新对象的属性值后,API 每次都会返回相同的(单个)Position 对象吗?或者它每次都会实例化并返回一个新对象?

即如果我存储 Position 对象,存储的/旧的值是否会被下一个 API 调用覆盖?为了避免这种可能性,必须克隆/复制它吗?

此行为是否在某处定义(针对所有 API 或针对每个 API)?

我猜 API(以及所有 API)会返回新对象,但我想知道我是否可以依赖它。

最佳答案

API 规范应该告诉您会发生什么。例如getCurrentPosition (强调我的):

When called, it must immediately return and then asynchronously attempt to obtain the current location of the device. If the attempt is successful, the successCallback must be invoked (i.e. the handleEvent operation must be called on the callback object) with a new Position object, reflecting the current location of the device.

关于javascript - 浏览器 API 返回的对象是 const 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38438243/

相关文章:

javascript - 当到达页面的开头或结尾时,按键得到 “remembered”

html - 仍然渲染 png 图像背景黑色

python - __setattr__ 方法确保类中的对象不可变

python - Python 中的元组真的是不可变的吗?

.net - 调用 ToString 后,StringBuilder 是否变得不可变?

javascript - jQuery 从特定 div 内的所有输入、文本区域和选择元素中删除类

javascript - JavaScript 中的函数与方法

javascript - 如何刷新 promise 链中的数据?

javascript - Angular2 数据在 IE11 中未绑定(bind)

javascript - 检测打印(不是打印预览)事件