search
HomeWeb Front-endJS TutorialDetailed explanation of extended examples of various data types in es6

1. Extension of string

  1. Adds an Iterator to the string, which can be traversed by for...of

  2. 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

  3. 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)

  4. 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.

  5. Template String `${...}`

2. Numeric expansion

  1. Number.isFinite checks whether a value is finite, All non-numeric values ​​return false

  2. 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 value

  3. Number.parseInt(), Number.parseFloat() are the same as the traditional methods, the purpose is to reduce the global method and language modularization

  4. Number.isInteger() determines whether it is an integer

  5. Number.EPSILON is a very small constant. If the error of floating point calculation is less than this value, it is ok

  6. 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;

  7. 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

  1. 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;

  2. Array.of converts a set of values ​​into an array;

  3. copyWithin copies the member at the specified location to other locations;
    Array.prototype.copyWithin(target, start = 0, end = this.length)

  4. 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

  5. fill fills the array with the given value, and the second and third parameters can specify the start and end positions;

  6. keys, values, entries

  7. 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 values

  8. The 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

  1. 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 , the length 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 the length attribute will not be counted. Enter the following parameters;

  2. 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.

  3. Rest parameter (... variable name)

  4. 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);
  5. 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

  1. Abbreviated representation of attributes

  2. Attribute name expression, [variable name]

  3. The name attribute of the method returns the function name (i.e. method name)

  4. Object.is is basically the same as ===. The difference is that +0 is not equal to -0, and NaN 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});
  5. 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;

  6. Object.getOwnPropertyDescriptor method can obtain the description object of the property.

  7. Traversal of properties:
    for...in, Object.keys, Object.getOwnPropertyNames(obj), Object.getOwnPropertySymbols(obj), Reflect.ownKeys(obj)

  8. __proto__ has the same function as Object.setPrototypeOf(), used to set the prototype object of an object, Object.getPrototypeOf()

  9. Object. keys(), Object.values(), Object.entries()

  10. 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!

Statement
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
JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

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.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

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 Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

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

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

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.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

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.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

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 of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

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.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

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

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use