+ 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))

18th Jan 2023, 12:35 PM
Đorđe Labat
Đorđe Labat - avatar
5 ответов
+ 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.
18th Jan 2023, 2:13 PM
Jayakrishna 🇮🇳
18th Jan 2023, 1:55 PM
Chris Coder
Chris Coder - avatar
+ 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. :-?
18th Jan 2023, 3:01 PM
Đorđe Labat
Đorđe Labat - avatar
+ 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.
18th Jan 2023, 7:32 PM
Lochard
Lochard - avatar
+ 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.
18th Jan 2023, 1:18 PM
Lochard
Lochard - avatar