+ 1
Can someone explain me this weird thing...
(function (x){ console.log(arguments[0]); // 1 x += 2 console.log(arguments[0]); // 3 delete arguments[0] console.log(arguments[0]); // undefined return x // expected result would be undefined, but instead it is 3. }(1))
5 Antworten
+ 3
arguments object maintains copy of arguments as an array. So delete arguments[0] delete from arguments object at 0th location. hence no more points to x.
Or
can be said as : delete arguments[0] means address of arguments[0] = null pointer, no more points to x then.
does not make x to null pointer
delete does not affect on local variables.
edit:
yes. delete won't affect on local variables also.
+ 2
Let me know if this helps.
https://code.sololearn.com/WZXo5jdX6MV8/?ref=app
+ 2
Here's better example:
function arg (x){
console.log(arguments[0], x); // 1 1
x += 2;
console.log(arguments[0], x); // 3 3
arguments[0]+=3;
console.log(arguments[0], x); // 6 6
delete x;
console.log(arguments[0], x); // 6 6
delete arguments[0];
console.log(arguments[0], x); // undefined 6
// return x // 6
return arguments[0] // undefined
}
console.log(arg(1))
// It appears that "x" and "arguments" sync each other on any value change, but "arguments" CAN be deleted and "x" can NOT be deleted. Why? Maybe because "arguments" is object, so object parameters can be deleted; "x" is var witch can NOT be deleted. :-?
+ 2
Đorđe Labat From what I know, yes. Delete remove a property from an object. Not only var, let and const also cannot be deleted. delete also does not work on the object arguments itself, just its properties.
I also tried your experiment, that's why I called it extra pointer.
+ 1
I am not sure but my guess is that it acts like some kind of reference or pointer. So when you delete it, you are just deleting an extra pointer to x.
Just a guess.