ES6 Getting Started tutorial---Numeric extension array extensions and object extensions

Source: Internet
Author: User
Tags square root

1. Numeric expansion

var num = 0b11; Console.log (num); 3

var num = 0o11;console.log (num); 9

var num = 1.234;

Console.log (Math.trunc (num))//Kill the decimal point
Console.log (Math.sign (-0));//Determine whether the parameter is positive, negative, positive 0 or negative 0

Console.log (Math.hypot (3,4))//returns the square root of the sum of squares of all parameters (Pythagorean theorem)

2. Array expansion

var str = ' Xuniannian ';
var arr = array.from (str);//Convert the class array to a true array.
Console.log (arr);

var arr = array.of (1,2,3,4,5); the//array.of () method is used to convert a set of parameters to an array.
Console.log (arr);

var arr = new Array (5);
Console.log (Arr.length)
var arr = [];

Arr.find ()
Find the first array element that matches a condition
Parameters:
1. Callback function
2, the callback function within the direction of this
Iterates through the entire array, calls the callback function during traversal, and returns the element that is currently being traversed if the return value of the callback function is true.
Returns undefined if all elements do not meet the criteria

Arr.findindex ()
Find the position of the first qualified array element
Parameters:
1. Callback function
2, the callback function within the direction of this
Iterates through the entire array, calls the callback function during traversal, and returns the position of the array element if the return value of the callback function is true.
Returns 1 if all elements do not meet the criteria

Arr.fill ()
Used to populate an array
Parameters:
1, the content of the fill
2. Starting position
3. End Position

var arr = [1,2,3,4,5];

var n = arr.find (function (value,index) {

return value > 4;
})
Console.log (n);

var n = arr.findindex (function (value,index) {
return value > 5;
})
Console.log (n);


var arr = [1,2,3,4,5];

Arr.fill (6,2,4);

Console.log (arr);

Arr.length = 10;
Arr.fill (0);

3. Extension of objects

function fn (x, y) {

x + +;
y++;

return {
X:x,
Y:y
}
return {x, y};
}
Console.log (FN ())

var obj = {
Name: ' Momo ',
Showname:function () {
return this.name;
}

};
Console.log (Obj.showname ());

var sex = ' male ';
var person = {
Name: ' Momo ',
[Sex]:false,
[' Get ' + ' name '] (){
return this.name;
}
};

Console.log (Person.getname ());
Console.log (Person[sex])

ES6 Getting Started tutorial---Numeric extension array extensions and object extensions

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.