Detailed explanation of extended examples of various data types in es6
1. Extension of string
Adds an Iterator to the string, which can be traversed by for...of
includes, startsWith and endsWith both return Boolean values and support the second parameter (the starting position of the search). endsWith targets the first n characters, and the other two are from the nth to the end
repeat returns a new string, and the parameter is the number of repetitions (the decimal will be rounded down, a negative number or Infnity will report an error, 0 to -1 is equivalent to 0, and the string will be converted to a number)
padStart and padEnd are completed at the head or tail. The first parameter is the minimum length of the string, and the second parameter is the string used for completion.
Template String `${...}`
2. Numeric expansion
Number.isFinite checks whether a value is finite, All non-numeric values return false
Number.isNaN checks whether a value is NaN, and only NaN returns true;
The traditional method isFinite isNaN will first call Number() to convert the non-numeric value Convert to a numerical valueNumber.parseInt(), Number.parseFloat() are the same as the traditional methods, the purpose is to reduce the global method and language modularization
Number.isInteger() determines whether it is an integer
Number.EPSILON is a very small constant. If the error of floating point calculation is less than this value, it is ok
The exact integer range of JS: -2^53~2^53 (excluding both ends),
Number.MAX_SAFE_INTEGER = Math.pow(2, 53) - 1;
Number.MIN_SAFE_INTEGER = -Number.MAX_SAFE_INTEGER;
Number.isSafeInteger() is used to determine whether an integer falls within this range;Math.trunc() removes the decimal part and returns an integer Part;
Math.sign() determines whether it is a negative integer or 0;
Math.cbrt() calculates the cube root of a number; there are also some logarithmic methods and trigonometric function methods;
Exponential operator 2 * * 3 === 8, which is different from the Math.pow implementation. For particularly large operations, the last digit of the operation result is different;
3. Array expansion
Array.from can convert array-like objects and traversable objects into arrays, and the spread operator can also convert certain objects into arrays;
can accept a second parameter, similar to the map method , returns the processed value to an array;Array.of converts a set of values into an array;
copyWithin copies the member at the specified location to other locations;
Array.prototype.copyWithin(target, start = 0, end = this.length)find finds the first qualified member, the parameter is one Callback function;
findIndex returns the position of the first array member that meets the conditions-
fill fills the array with the given value, and the second and third parameters can specify the start and end positions;
keys, values, entries
includes, indexOf is not semantic enough, and is used internally == judgment,
[NaN] .indexOf(NaN) // -1 , [NaN].includes(NaN) // true
Map's has is used to find key names, Set's has is used to find valuesThe empty position of the array has no value, 0 in [,,] // false, es6 will convert the empty position to undefined, and the empty position should be avoided
four . Function extension
allows setting default values for functions. If default values are set for non-tail parameters, in fact this parameter cannot be omitted;
After setting the default value , thelength
attribute of the function will return the number of parameters without a specified default value;
If the parameter with a default value set is not the tail parameter, then thelength
attribute will not be counted. Enter the following parameters;Once the default value of the parameter is set, when the function is declared and initialized, the parameters will form a separate scope (context). When the initialization is completed, this scope will disappear. This syntax behavior will not appear when the parameter default value is not set.
Rest parameter (... variable name)
-
Extension operator (...), convert an array to comma-separated parameter sequence.
/* 替代数组的apply方法 */// ES5的写法function f(x, y, z) { // ...}var args = [0, 1, 2]; f.apply(null, args);// ES6的写法function f(x, y, z) { // ...}var args = [0, 1, 2]; f(...args);/* --------------------------------- */// ES5的写法Math.max.apply(null, [14, 3, 77])// ES6的写法Math.max(...[14, 3, 77])// 等同于Math.max(14, 3, 77);/* --------------------------------- */// ES5的写法var arr1 = [0, 1, 2];var arr2 = [3, 4, 5]; Array.prototype.push.apply(arr1, arr2);// ES6的写法var arr1 = [0, 1, 2];var arr2 = [3, 4, 5]; arr1.push(...arr2);
-
Notes on using arrow functions:
(1) The
this
object in the function body is the definition The object in which it is located at the time of use, not the object in which it is used.(2) cannot be used as a constructor, that is to say, the
new
command cannot be used, otherwise an error will be thrown.(3) The
arguments
object cannot be used, as the object does not exist in the function body. If you want to use it, you can use the rest parameter instead.(4) The
yield
command cannot be used, so the arrow function cannot be used as a Generator function.
5. Object extension
Abbreviated representation of attributes
Attribute name expression, [variable name]
The
name
attribute of the method returns the function name (i.e. method name)-
Object.is is basically the same as ===. The difference is that
+0
is not equal to-0
, andNaN
is equal to itself
// es5实现Object.isObject.defineProperty(Object, 'is', { value: function(x, y) {if (x === y) { // 针对+0 不等于 -0的情况 return x !== 0 || 1 / x === 1 / y; }// 针对NaN的情况return x !== x && y !== y; }, configurable: true, enumerable: false, writable: true});
Object.assign(target, o1, o2) is used to merge objects. If there are properties with the same name, the previous ones will be overwritten later; the shallow copy executed
cannot be converted due to undefined and null into objects, so if they are used as parameters, an error will be reported;Object.getOwnPropertyDescriptor method can obtain the description object of the property.
Traversal of properties:
for...in, Object.keys, Object.getOwnPropertyNames(obj), Object.getOwnPropertySymbols(obj), Reflect.ownKeys(obj)__proto__ has the same function as Object.setPrototypeOf(), used to set the prototype object of an object, Object.getPrototypeOf()
Object. keys(), Object.values(), Object.entries()
Object.getOwnPropertyDescriptors returns the description object of all its own properties (non-inherited properties) of the specified object;
The above is the detailed content of Detailed explanation of extended examples of various data types in es6. For more information, please follow other related articles on the PHP Chinese website!

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use
