javascript - 当`let y = x`时,`y !== x`是否可能
问题描述
有没有什么情况
let x = y;
console.log(y !== x) //=> true
到目前为止,没有任何案例有效,无论是对象还是符号
?
解决方案
哦,我刚发现。
使用 getter 和 setter。
如果y
是全局 getter 并且每次返回不同的值,这是可能的。
// Defining y.
((i) => {
Object.defineProperty(this, "y", {
get: () => i++
})
})(0)
// y is going to be one more than the previous value it gave,
// but is starts with 1.
// The magical testing...
let x = y;
console.log(x === y); // It returns `false`!
// Even y is not y.
console.log(y === y);
推荐阅读
- html - 具有三种状态的复选框
- python - 如何仅在 tkinter 中更改所选文本的属性
- firebase - Firebase Cloud Functions:`convert` 因 ChildProcessError 而失败
- reactjs - 如何将 react-helmet 与 netlify 预渲染一起使用?
- python - 如何使用 atom-python-run 包在 Atom 中运行 python 脚本?
- git - 旧的提交出现在新的拉取请求中?
- java - 无法在线性布局中移动项目
- jquery - 在更新的路线上显示移动标记 (gmaps.js)
- java - IntelliJ IDEA 添加 JDK 10:“所选目录不是 JDK 的有效主目录”
- php - 仪表板索引页四个错误,但本地主机工作良好