Home > Web Front-end > JS Tutorial > A Pitfall in Object Questions Understanding Javascript Objects_Javascript Skills

A Pitfall in Object Questions Understanding Javascript Objects_Javascript Skills

WBOY
Release: 2016-05-16 15:24:10
Original
1163 people have browsed it

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])//这里会输出什么内容呢?
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template