JavaScript this question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JavaScript this question

var user = { name: "John", age: 30, sayHi() { alert(this.name); } }; var admin = user; user = null; admin.sayHi(); Why does it still has reference to admin object.??

24th Feb 2022, 2:16 PM
Vijay Prakash
Vijay Prakash - avatar
5 Answers
+ 2
If you know the answer why are you asking?
24th Feb 2022, 2:27 PM
Lisa
Lisa - avatar
+ 1
User is just a variable. When you assign user to admin, it gets copied and admin has the same values and doesn't refer to user. https://code.sololearn.com/coqFdEmJyZ9W/?ref=app
24th Feb 2022, 2:20 PM
Lisa
Lisa - avatar
0
Not exactly
24th Feb 2022, 2:22 PM
Vijay Prakash
Vijay Prakash - avatar
0
It is not referring to admin , it is the copy which is being referred
24th Feb 2022, 2:27 PM
Pariket Thakur
Pariket Thakur - avatar
0
We can't create copy of object this way
24th Feb 2022, 3:07 PM
Vijay Prakash
Vijay Prakash - avatar