Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binded member call after delete

Tags:

javascript

In this snippet delete returns true. So why f is being successfully called after delete?

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

x = new X;
var f = x.f.bind(x);
console.log("delete: " + delete x);
f();
like image 960
mirt Avatar asked Jun 07 '26 19:06

mirt


1 Answers

delete just removed the identifier x. The object still exists in memory, because it is bound to f.

f still refers to the same function with the same bound context, even though x doesn't.

See 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).

like image 76
Scimonster Avatar answered Jun 10 '26 07:06

Scimonster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!