Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the details of JavaScript that are easily overlooked?

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the details of JavaScript that are easy to be ignored, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Statement / expression

Understand statements (statemaents) and expressions (expressions) from another perspective: expressions do not change the running state of the program, but statements do. Another kind of expression statement is called expression statement, which can be understood as the intersection of expression and statement, such as ({aura 1}), "use strict;", and so on. I don't think it's necessary to buckle, it doesn't make much sense.

Character set

ES3 requires that JS must implement Unicode 2.1 and later, while ES5 only requires support for Unicode 3 and subsequent versions. Unicode characters exceeded 100, 000 characters in 2005 and are still being updated. The * * version is 8.0.

semicolon

If you don't like to write JS code with a semicolon and don't know when you have to add a semicolon, you can do this: precede statements that start with "(", "[", "/", "+", "-", such as; (a + b) .semicolon ().

Binary system

Octal is prohibited in ES5 strict mode. At present, there are differences in the implementation of JS among various engines, some support octal and some do not. The reason why octal is banned: String and Number are often converted to each other, while octal data that begins with 0 is particularly confusing and confusing to machines, such as whether 09 should be converted to 9 or simply report an error. Hexadecimal does not have this problem, such as 0x98. See here for more information.

Precision.

JS uses IEEE-754 floating-point representation, which is a binary representation, and JS cannot represent all real numbers due to precision. The number of floating-point numbers it can display is limited, for example, it cannot accurately represent the literal value of 1/3. This also leads to errors in the calculation of floating-point numbers, such as 0.3-0.2! = 0.2-0.1, because in the process of calculation, there is a data overflow and the precision is lost.

Null/undefined

System-level, unexpected, or similar error value gaps use undefined, while program-level, normal, or expected value gaps use null. When programming to assign values to variables, you should use null instead of undefined. It is worth noting that the undefined in ES3 can be reassigned, and ES5 fixed the bug. Usually we use void 0 to restore / replace the value of undefined.

Eval

Eval is a difficult thing to grasp, it is more like Function in ES3 and an operator in ES5 (aliases are not allowed to be set in strict mode, otherwise an error is reported, and it is used as a reserved word). In fact, setting aliases for eval is not allowed in ES3, but many implementations still allow it and execute it as global code. Browsers, especially IE, have no rules to follow, but IE provides an execScript function, similar to global eval, which returns null every time it is executed.

There are not many scenarios that need to use eval, use it as little as possible, and the general requirements can be met by using new Function.

Quote

Delete the pit where the attribute exists: a = {n: {XRV 2}}, b = a.n; delete a.n; after this code is executed, B. x is still equal to 2 because the object {XRV 2} is referenced by both an and b, and the delete directive removes only a's reference to it, and the reference on b still exists. This problem can cause memory leaks.

Object extension

Object's freeze method is too strict; defineGetter/lookupGetter and the corresponding Setter are useful attributes.

ToLocalString

As shown in the picture, you may not know that JavaScript's toLocaleString can still play like this.

This semantics

There are only two semantics in the this context: one is as a method call, and the this points to the object that calls it; the other is as a function call, pointing to the Global object (undefined in strict mode). It has no scope restrictions, as shown in the following figure, an is called as a function, so it points to window, so it returns false.

Types

JavaScript can be called and executed of type Function, but there are also callable Object, such as some host objects in lower versions of IE: document.getElementById, alert, and so on. In many browsers, typeof RegExp is also Object. This is definitely a non-standard implementation, and you should rely on these error types as little as possible before browsers discard / correct them.

IE8 getter/setter

Although Object.defineProperty is something of ES5, it has been supported as early as IE8, but the support is not perfect, such as writable, enumerable, configurable, these configuration items are invalid, and getter/setter is mainly supported under IE8.

JSON.stringify

JSON.stringify accepts three parameters, many people know that the third parameter can set white space characters to beautify the output, but you may not know the role of the second parameter, it is {Array | Function} type, if it is Array, it is used to filter key, if it is Function, you can handle value, as shown in the figure.

Symbol

ES6 added a new data type, Symbol, which is an original data type (figure I), with the characteristics of an object (figure II), and can point to the same reference (figure III), can be used as an object of key but not enumerated (figure IV), built-in Symbol will affect the implementation of the program (figure V), Symbol.iterator is an important symbol, so that elements with iterative attributes (figure VI), a lot of tricks.

See http://weibo.com/1812166904/DqMwR8O6z for the attached picture.

There are several ways to add Symbol.iterator to pseudo arrays: duck-typed iterator functions, yield functions, and traversal symbols that directly use Array.

See http://weibo.com/1812166904/DqMBYebPw for the attached picture.

Set/WeakSet

Set/WeakSet this data structure, can not be said to be useless, but it is not very useful, the former is not allowed to duplicate members of the array, by the way with a bit of ES6 features, the latter although to a certain extent can prevent memory leaks, but also prone to errors, such as a reference has been garbage collected, and then use it may return null. They are all matching products of ES6. But Map/WeakMap is two very good designs, the conventional Object structure is String-Val key-value pair, and it is extended to AllType-Val, any type can be used as its Key, whether server-side programming or client-side programming, this attribute brings great convenience.

Regular pattern

Understand the meaning of regular zero width: regular zero width assertions, similar to anchor characters, match the specified position but not the content, such as ^ match beginning, $match end,\ b match word boundary; (? = p) match "next character matches p" position, (?! P) match "next character does not match p" position. The\ b character matches the word boundary, which actually matches the position between\ w and\ W (\ w matches [a-zA-Z0-9]). Few people use\ B, which matches the boundary position of a non-word, which simply means the position between\ w &\ w or\ W &\ W.

After reading the above, do you have any further understanding of the details of JavaScript that are easy to be ignored? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report