Liaoche JS Tutorial Note 1

Source: Internet
Author: User
Tags iterable new set

Traversal Array can take the subscript loop, traverse Map and Set cannot use Subscript. In order to unify collection types, the ES6 standard introduces new iterable types Array , Map and Set all of them belong to iterable Types.

A iterable collection with a type can traverse through a new for ... of loop.

however, a better approach is to use the built-in iterable forEach method directly, which receives a function that automatically callbacks the function each time it iterates. Take Array for Example:

var a = [‘A‘, ‘B‘, ‘C‘];a.forEach(function (element, index, array) { // element: 指向当前元素的值 // index: 指向当前索引 // array: 指向Array对象本身 alert(element);});

note that the forEach() method is introduced by the ES5.1 standard and you need to test whether the browser supports it.

SetArraysimilar but Set no index, so the first two parameters of the callback function are the elements themselves:

var s = new Set([‘A‘, ‘B‘, ‘C‘]);s.forEach(function (element, sameElement, set) { alert(element);});

MapThe callback function parameters are sequentially value , key and map themselves:

var m = new Map([[1, ‘x‘], [2, ‘y‘], [3, ‘z‘]]);m.forEach(function (value, key, map) { alert(value);});

If you are not interested in certain parameters, they can be ignored because JavaScript function calls do not require parameters to be Consistent. For example, you only need to obtain Array element :

var a = [‘A‘, ‘B‘, ‘C‘]; a.forEach(function (element) { alert(element); });

Defining Functions

In javascript, you define a function as Follows:

function abs(x) { if (x >= 0) { return x; } else { return -x; }}

abs()the above functions are defined as Follows:

    • functionIndicates that this is a function definition;
    • absis the name of the function;
    • (x)The parameters of the function are listed in parentheses, and multiple parameters are , separated;
    • { ... }The code between is a function body, can contain several statements, and even can not have any statements.

Note that when the statement inside the function body executes, the function executes and returns the result as soon as it is executed return . therefore, it is possible to implement very complex logic within a function through conditional judgments and loops.

 
If there is no return statement, The result is returned after the function is executed, except for the result undefined .

Since the function of JavaScript is also an object, the abs() function defined above is actually a function object, and the functions name abs can be considered as a variable pointing to the Function.

therefore, the second way to define a function is as follows

 }};

In this way, function (x) { ... } it is an anonymous function that does not have a function name. however, This anonymous function assigns a value to the variable abs , so abs the function can be called by a variable.

The two definitions are completely equivalent , and note that the second way is to add one at the end of the function body according to the complete syntax ; , indicating the end of the assignment Statement.

 
Calling functions

When you call a function, you pass in the arguments sequentially:

abs(10); // 返回10abs(-9); // 返回9

Because JavaScript allows you to pass in any parameter without affecting the call, there is no problem with the parameters passed in that are more than the defined parameters, although they are not required inside the function:

abs(10, ‘blablabla‘); // 返回10abs(-9, ‘haha‘, ‘hehe‘, null); // 返回9

There is no problem with the parameters passed in that are less than defined:

// 返回NaN

abs(x)the parameters of the function are received at this time x undefined , and the computed result is NaN .

To avoid receipt undefined , you can check the Parameters:



Liaoche JS Tutorial Note 1

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.