In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about the example analysis of JavaScript array test questions. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
When we got off work in the evening, we began to discuss boring problems again. A colleague proposed the following strange scenario, which is said to be an interview question:
Var o = {1 push:Array.prototype.push', 2 push:Array.prototype.push', o.push ('c')
Colleague: what does the internal value of O look like now?
My first reaction was crowding out. Why should I study the action of the explanation engine under unfair circumstances? But this kind of inference is sometimes very attractive, so I thought about it carefully when I came back and found that it was actually very simple.
For the push method, what I think of conditionally is the stack. In the classic stack of data structures, stack pressing and bullet stack operations are all based on the top pointer of the stack, which always points to the top of the stack, which means that it will automatically increase or decrease due to pressing the stack. The pointer in the array in javascript is length. So in the above code, o.push ('c') is o.2 ='c' (although o.2 cannot be accessed directly, this is just pseudo code), so the code completes the following data in o:
{1 length+1 operation, 2 push:Array.prototype.push operation, length:3 / / push:Array.prototype.push operation
Supplementary note:
In JavaScript, everything is an object, but there are some differences between javascript objects and strongly typed objects. It can be understood that grades are a set of key-value pairs. Its array type is no exception, its subscript access is key access (but its keys are all natural numbers), and the literal amount of the object assigned to an in the above example actually mimics an array (an array from the beginning of the subscript)-although there are only some of the characteristics of the array, such as the real array when making key access, it is checked to cross boundaries according to length.
As long as you know that the location of push is based on length, the following seemingly strange phenomena are easy to understand:
/ / 1.length does not exist. The engine is set to
0 var o = {'1mm / v / s / b, push:Array.prototype.push}
O.push ('c'); / / c {0kind of people, 1 of them, 1 of them, of 2 of them, of course.}
/ / 2.length is negative, which is an interesting question, which touches on the source code inverse code and complement code [1] var o = {'1bicycle var / b', length: ⑴, push:Array.prototype.push}
O.push ('c')
/ / c {1 VOLTHAYALER 2RHYDROBULING 4294967295 PULTHANGZHANGHANG.
/ / 3.length is a character or object
Var o = {1 push:Array.prototype.push', 2 push:Array.prototype.push', length:'A', etc.
O.push ('c')
/ / c {0VOULTHINGCINGRAPHY 1RAPHANALERATION 2RAPHING BUBTHERMORAPHY.
I thought the js interpreter would convert An into ASCII code to assign values to length, but I finally saw that the freedom of javascript still has integrity.
[1]: all the values in the computer are stored in the form of complement. In order to facilitate the operation, the complement of ⑴ is the same as the complement of 4294967295. According to the semantics of length, it is an unsigned number.
[⑴] complement = 1111 1111 1111 = [4294967295] complement
So we press an object into the O in 2, key takes 4294967296, but the maximum length of the array is limited to 4294967296, that is, the subscript can only take 4294967295, only 32 bits will be taken-- for 4294967296 = 1 0000 0000 0000, so this time the position of push is 0.
These are all the contents of the article "case Analysis of JavaScript Array interview questions". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.