A pitfall of object questions to understand Javascript objects,

Source: Internet
Author: User

A pitfall of object questions to understand Javascript objects,

The example in this article is mainly intended to attract understanding and attention to Javascript objects. In fact, it is a kind of challenge during the interview, and it is rarely used in actual projects. However, in order to improve the vigilance, let's 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]) // What will be output here?

If you want to answer this question by yourself, I will not explain it later.

What results will be output here? Most people may think that the result is 100, the question is wrong, or the result is 200.

The final result is 200..

Why?Because second and third are both objects and [object], therefore, first [second] is equivalent to first [[object]. Similarly, first [third] is equivalent to first [object]. Therefore, the final result can be first ["[object ].] "], in this example, the expression is assigned a value twice, so the final result is 200.

So in javascript, we need to pay attention to a lot of details, maybe those interviewers, who do not know what interview questions are, and try their best to answer these traps. However, we just need to build a solid foundation, so we are not afraid of anything! I will continue to explain some questions in the future!

Through a simple example, we have aroused everyone's thinking and provided a lot of inspiration for everyone to learn javascript objects. I hope you will gain some benefits.

Articles you may be interested in:
  • Js object external access or call problems
  • Three methods for creating objects in JavaScript
  • Functions that display all attributes and methods of a js object
  • Comprehensive Analysis of javascript XMLHttpRequest objects
  • Js converts a class array object to an array object
  • Multiple variable definitions in js (direct object volume, direct array volume, and direct function volume)
  • Use eval in js to generate a JSON object
  • Notes for converting text into JSON objects using JQuery

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.