Learn the ES6 of the Web from scratch (vi) ES6 Basic Grammar Four

Source: Internet
Author: User
Tags new set

Hello everyone, here is "learn the Web series from scratch" and synchronize updates at the following address ...

  • Github:https://github.com/daotin/web
  • Public number: The top of the Web front
  • Blog Park: http://www.cnblogs.com/lvonve/
  • csdn:https://blog.csdn.net/lvonve/

Here I will start with the Web front End 0 Foundation, step-up learning web-related knowledge points, during the period will also share some fun projects. Now let's go to the Web Front end learning Adventure tour!

One, the extension of the string
    • includes(str): Determines whether the specified string is included

    • startsWith(str): Determines whether to start with the specified string

    • endsWith(str): Determines whether to end with the specified string
    • repeat(count): Repeat specified number of times

    let str = 'abcdefg';    console.log(str.includes('a')); //true    console.log(str.includes('h')); //false    //startsWith(str) : 判断是否以指定字符串开头    console.log(str.startsWith('ab')); //true    console.log(str.startsWith('ac')); //false    //endsWith(str) : 判断是否以指定字符串结尾    console.log(str.endsWith('fg')); //true    console.log(str.endsWith('d')); //false    //repeat(count) : 重复指定次数a    console.log(str.repeat(3)); // abcdefgabcdefgabcdefg
Second, the expansion of the value

Binary and octal numeric representations: binary with 0b the beginning, octal with the 0o beginning.

    • Number.isFinite(i): To determine whether a finite number is a large

    • Number.isNaN(i): Determine if it is Nan

    • Number.isInteger(i): Determines whether an integer

    • Number.parseInt(str): Converts a string to the corresponding numeric value
    • Math.trunc(i): Direct removal of decimal parts

 Console.log (0b1010);//10 Console.log (0o56);//46 console.log ('--------------------'); Number.isfinite (i): Determine whether a finite number of Console.log (Number.isfinite (NaN)); False Console.log (Number.isfinite (5)); True Console.log (Number.isfinite (Infinity)); False Console.log ('--------------------'); Number.isnan (i): Determine if it is a Nan Console.log (Number.isnan (Nan)); True Console.log (Number.isnan (5)); False Console.log (Number.isnan (undefined)); False Console.log ('--------------------'); Number.isinteger (i): Determine whether it is an integer console.log (Number.isinteger (5.23)); False Console.log (Number.isinteger (5.0)); True Console.log (Number.isinteger (5)); True Console.log ('--------------------'); Number.parseint (str): Converts a string to the corresponding numeric Console.log (Number.parseint (' 123abc ')); 123 Console.log (Number.parseint (' a123abc ')); NaN console.log ('--------------------'); Math.trunc (i): direct removal of the minor part Console.log (Math.trunc (13.123)); 
Three, array expansion
    • Array.from(v): Converts a pseudo-array object or a traversed object to a true array
    • Array.of(v1, v2, v3): Converts a series of values to an array

    • find(function(value, index, arr){return true}): Find the first element that satisfies a condition that returns true
    • findIndex(function(value, index, arr){return true}): Finds the first element subscript that satisfies a condition that returns true

<body> <button> test 1</button> <br> <button> test 2</button> <br> <bu tton> Test 3</button> <br> <!--1. Array.from (v): Converts a pseudo-array object or a traversed object to a true array of 2. Array.of (v1, V2, v3): Converts a series of values to an array of 3. Find (function (value, index, arr) {return true}): Finds the first element that satisfies a condition that returns true 4. FindIndex (function (value, index, arr) {return true}): Find the first element that satisfies the condition returns true subscript--<script type= "Text/javascript"        >//array.from (v): Converts a pseudo-array object or a traversal object to a true array, the return value is a true array//the collection of elements obtained using DOM manipulation is a pseudo-array.        Let Btns = document.getelementsbytagname (' button '); Console.log (btns.length);        3 Array.from (Btns). ForEach (function (item, index) {console.log (item, index);        });        Array.of (v1, V2, v3): Converts a series of individual values to an array of let arr = array.of (1, ' abc ', TRUE);        Console.log (arr);        Find (function (value, index, arr) {return true}): Find the first element that satisfies the condition return true Let arr1 = [1, 3, 5, 2, 6, 7, 3]; Let result = Arr1.find (function (itEM, index) {//Find the first element in the ARR1 array that is greater than 3 return item > 3;        }); Console.log (result); 5//findindex (function (value, index, arr) {return true}): Find the first element that satisfies the condition returns true subscript let Result1 = Arr1.findinde        X (function (item, index) {//finds the first element in the ARR1 array that is greater than 3 subscript value return item > 3;        }); Console.log (RESULT1); 2 </script></body>
Four, object expansion
    • Object.is(v1, v2): Determines whether 2 data is exactly equal. (internal is the implementation principle is to compare whether strings are exactly equal)
console.log(Object.is('abc', 'abc'));//trueconsole.log(NaN == NaN);//falseconsole.log(Object.is(NaN, NaN));//trueconsole.log(0 == -0);//trueconsole.log(Object.is(0, -0));//false
    • Object.assign(target, source1, source2..): Copy the properties of the source object onto the target object
let obj = {name : 'Daotin', age : 18, c: {d: 2}};let obj1 = {};Object.assign(obj1, obj);console.log(obj1, obj1.name);
    • Direct Manipulation __proto__ Properties
let obj3 = {name : 'Daotin', age : 18};let obj4 = {};// obj4的隐式原型指向obj3obj4.__proto__ = obj3;console.log(obj4, obj4.name, obj4.age);// {name : 'Daotin', age : 18}  Daotin 18
V. Set container and Map container

Set Container : An unordered collection of multiple value that cannot be duplicated.

The set container needs to create a set container object through new, and the parameter is multiple value values.

let set = new Set([1,2,3,4,3,2,1,6]);

The methods and properties of the Set container object:

    • Set(): Constructor for Set container (without parameter value collection)
    • Set(array): Constructor for set container (with parameter value collection)
    • add(value): Adds a value to the Set container object
    • delete(value): Delete value value of Set container object
    • has(value): Determines whether the set container has value values
    • clear(): Emptying the Set container
    • size: equivalent to the length of the array

map Container : An unordered key does not repeat multiple key-value of the collection body.

Note: The collection of map parameter arrays, each array is in the form of Key-value. The entire array of the outside of the collection is [] surrounded by, instead of {} .

let map = new Map([['name', 'Daorin'],['age', 18]]);
    • Map ()
    • Map (Array)

    • Set (key, value)//Add method similar to set container
    • Get (Key)
    • Delete (key)
    • Has (key)
    • Clear ()
    • Size

The role of set and map:

1. Set container can be heavy for an array.

let arr = [1, 2, 3, 4, 5, 2, 4, 5];    let set = new Set(arr);    arr = [];    for (let i of set) {      arr.push(i);    }    console.log(arr); // 1,2,3,4,5

2, set and map containers can be used for ..of.. to traverse.

Vi. Introduction of ES7 method

1. Exponential operator:**

console.log(3**2); // 9

2 Array.prototype.includes(value); : Determines whether the array contains the specified value. (In ES6, str.includes(str1) : Determine if str contains the specified string str1)

let arr = [1,2,3,4, 'abc'];console.log(arr.includes(2));//trueconsole.log(arr.includes('a'));//false

Learn the ES6 of the Web from scratch (vi) ES6 Basic Grammar Four

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.