Home  >  Article  >  Web Front-end  >  New APIs for Math, Number, String, Array, and Object

New APIs for Math, Number, String, Array, and Object

PHP中文网
PHP中文网Original
2017-07-14 18:10:341396browse

Math

Math.trunc()

Take out the decimal part of a decimal and return the integer part

<code>Math.trunc(1.239);  //1
Math.trunc(-3,1415926);  //-2
Math.trunc(3.9);  //3</code>

For null values ​​and values ​​that cannot be intercepted as integers, return NaN

<code>Math.trunc("a");  //NaN
Math.trunc();  //NaN
Math.trunc(NaN);  //NaN</code>

Math.sign()

Judge positive number, negative number or 0, return 1 for positive number, -1 for negative number, 0 for 0 and NaN for other values

<code>Math.sign(11.22);  //1
Math.sign(253);  //1
Math.sign(0);  //0
Math.sign(0.0);  //0
Math.sign(-0.0);  //-0
Math.sign(-0);  //-0
Math.sign(-345);  //-1
Math.sign(-2.983958);  //-1
Math.sign('a');  //NaN
Math.sign();  //NaN
Math.sign(NaN);  //NaN</code>

Math.cbrt()

Calculate the cube root of a number and return NaN for other values

<code>Math.cbrt(8);  //2
Math.cbrt(-64);  //-4
Math.cbrt(-27);//3
Math.sign('a');  //NaN</code>

Number.isInteger()

Determine whether a value is an integer. It returns true or it returns false

<code>Number.isInteger(25) // true  
Number.isInteger(25.0) // true  
Number.isInteger(25.1) // false  
Number.isInteger("15") // false  
Number.isInteger(true) // false  </code>

Number.isNaN()

Used to check whether a value is NaN

<code>Number.isNaN(NaN) // true  
Number.isNaN(15) // false  
Number.isNaN('15') // false  
Number.isNaN(true) // false </code>

String

includes()

Used to check whether any connected items in the array are included. If true is included, false is not included

<code>var str="weirenshi";
str.includes("shi")//ture
str.includes("ei")//ture
str.includes("df")//false
str.includes("dfghjk")//false</code>

startsWidth()

Starting with a string letter (one or two) returns true instead of false

<code>var str="weirenshi";
str.startsWidth("w")//ture
str.startsWidth("we")//ture
str.startsWidth("d")//false</code>

endsWidth

Ending with a string letter (one or two) returns true and does not contain false

<code>var str="weirenshi";
str.endsWidth("i")//ture
str.endsWidth("hi")//ture
str.endsWidth("d")//false</code>

repeat

Copy: Define as many as you want and you will copy as many as you want

<code>"abc".repeat(3) // "abcabcabc"
"wei".repeat(5) // "weiweiweiweiwei"</code>

Array

Array.from is copied to an array and copied unchanged

<code>var arr=[1,2,3,4,5];
var ass=Array.from(arr);//[1,2,3,4,5]</code>

Array.of put into an array

Similar to new Array

<code>Array.of(1, 2, 3);//[1,2,3]</code>

fill

1 means starting from the first position, 7 means starting from the first position and followed by 7

<code>[0, 0, 0].fill(7, 1) // [0,7,7]
[0, 0, 0, 4, 6, 3, 4].fill(9, 3) // [0,0,9,9,9,9,9]</code>

Object

Object.assign shallow copy The first parameter is the target object, and the following parameters are the source object

<code>var aa = { a: 1 };  
var qq = { b: 2 };  
var zz = { c: 3 };  
Object.assign(aa, qq, zz);  
target // {a:1, b:2, c:3} 

var ss={x:1,y:2};
var bb={};
Object.assign(ss,bb);
bb.x=3;
ss//x:1,y:2;
bb//x:3,y:2;</code>

Copy directly and merge objects

<code>var ff={x:1,y:2};
var vv={k:9,l:8};
var kk=Object.assign(ff,vv)//{x:1,y:2,k:9,l:8}</code>

The above is the detailed content of New APIs for Math, Number, String, Array, and Object. 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