The examples in this article are mainly to attract understanding and attention to Javascript objects. In fact, it is a pit during interviews and is rarely used in actual projects. However, in order to increase vigilance, let’s take a look at this example:
Code name
var first = {}; var second = {k:"second"}; var third = {k:"third"}; first[second] = 100; first[third] = 200; (first[second])//What content will be output here?
If you want to do this question yourself, I won’t read the explanation below.
What kind of results will be output here? Most people may think that the result is 100, or the question is wrong, or the result is 200.
In fact, the final result is 200。
Why?Because second and third are objects, and they are both [object object], first[second] is equivalent to first[[object object]]. Similarly, first[third] is equivalent to first[[object object]], so the final result can be first["[object object]"]. In the example, this expression was assigned twice, so the final result is 200.
So in JavaScript, many details that we need to pay attention to may be those interviewers who don’t know what interview questions they have, and they all have these trap-like questions. However, we just need to lay a solid foundation and be afraid of nothing! I will continue to give some questions to explain in the future!
Through a simple example, it aroused everyone's thinking and provided a lot of inspiration for everyone to learn JavaScript objects. I hope everyone can gain something.