javascript - 绑定(bind)成员删除后调用

标签 javascript

在此代码段中,删除返回 true。那么为什么删除后f调用成功了呢?

function X() {
    this.f = function() {
        console.log("X::f");
    }
}

x = new X;
var f = x.f.bind(x);
console.log("delete: " + delete x);
f();

最佳答案

delete 只是删除了标识符 x。该对象仍然存在于内存中,因为它绑定(bind)到 f

f 仍然引用具有相同绑定(bind)上下文的相同函数,即使 x 不是。

参见 the MDN page on delete :

Unlike what common belief suggests, the delete operator has nothing to do with directly freeing memory (it only does indirectly via breaking references. See the memory management page for more details).

关于javascript - 绑定(bind)成员删除后调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30734833/

相关文章:

javascript - Bing Maps V8 - Uncaught ReferenceError : Microsoft is not defined

javascript - 内部链接强制滚动到页面顶部

javascript - PreventDefault 右键单击​​图像

javascript - Angular2 HTTP : Uncaught SyntaxError: Unexpected token < polyfills

javascript - 在 AngularJS 中使用 ngSanitize 指令

javascript - 触发文档中其他任何地方的点击,即使使用 iframe?

javascript - Highcharts 下载带有 svg 模式的图表

javascript - 加载文件并解析文档

javascript - 如何将触发器/事件转换为 Promise 或异步/等待?

javascript - jQuery - 输入字段中的多个电子邮件地址