Home > Article > Web Front-end > A Pitfall in Object Questions Understanding Javascript Objects_Javascript Skills
The examples in this article are mainly to attract understanding and attention to Javascript objects. In fact, it is a pitfall 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; console.log(first[second])//这里会输出什么内容呢?
If you want to do this question yourself, don’t read the explanation below.
What kind of results will be output here? Most people may think that the result is 100, or that there is an error in the question, or that the result is 200.
In fact, the final result is 200.
Why? Because second and third are both objects, and both are [object object], so first[second] is equivalent to first[[object object]] and first[third] is equivalent to first[[object object]] So the final result can be first["[object object]"]. In the example, this expression is assigned twice, so the final result is 200.
So in JavaScript, there are many details that we need to pay attention to. Maybe those interviewers don’t know what interview questions to ask, so they just ask these trap-style questions. However, we just need to lay a solid foundation and don’t be afraid of anything! I will continue to give some questions to explain in the future!
Through a simple example, it triggered everyone's thinking and provided a lot of inspiration for everyone to learn javascript objects. I hope everyone will gain something.