javascript
JavaScript一些常用 API整理汇总
JavaScript一些常用 API整理匯總
Array
new Set()
數(shù)組去重
const arr = [3,4,4,5,4,6,5,7]; console.log(new Set(arr)); // {3,4,5,6,7} const a = Array.from(new Set(arr)) // [3, 4, 5, 6, 7] //或者 const a = [...new Set(arr)]//推薦sort()
對(duì)數(shù)組元素進(jìn)行排序(改變?cè)瓟?shù)組)。
const arr = [1,2,3,4,5,6]; console.log(arr.sort((a,b)=>a-b)) // [1,2,3,4,5,6] 大小正序 cosole.log(arr.sort((a,b)=>b-a))// [6,5,4,3,2,1] 大小倒序 //不可以進(jìn)行運(yùn)算的則比較編碼大小 'b' > 'a' => truereverse()
反轉(zhuǎn)數(shù)組中的元素(改變?cè)瓟?shù)組)。
const arr = [3,4,4,5,4,6,5,7]; conosle.log(arr.reverse()); // [7, 6, 5, 5, 4, 4, 4, 3]delete
刪除一個(gè)數(shù)組成員,會(huì)形成空位,并不會(huì)影響 length 屬性(改變?cè)瓟?shù)組), 同樣適用于對(duì)象。
//數(shù)組 const arr = [0,1,2]; delete arr[1]; conosle.log(arr); // [0, empty, 2]//對(duì)象 const obj = {name: '謝大腳',age: '18',sex: '女'}; delete obj.sex; console.log(obj); // {name: "謝大腳", age: "18"}shift()
把數(shù)組的第一個(gè)元素從其中刪除,并返回第一個(gè)元素的值(改變?cè)瓟?shù)組)。
const arr = [0,1,2]; const a = arr.shift(); // 0 console.log(arr); // [1, 2]unshift()
向數(shù)組的起始處添加一個(gè)或多個(gè)元素,并返回新的長(zhǎng)度(改變?cè)瓟?shù)組)。
const arr = [3,4,4,5,4,6,5,7]; const a = arr.unshift(8); console.log(a); // 9(a為返回的數(shù)組的新長(zhǎng)度) console.log(arr); // [8, 3, 4, 4, 5, 4, 6, 5, 7]push()
在數(shù)組的末端添加一個(gè)或多個(gè)元素,并返回添加新元素后的數(shù)組長(zhǎng)度(改變?cè)瓟?shù)組)。
const arr = [3,4,4,5,4,6,5,7]; const a = arr.push(8,9); console.log(a); // 10(a為返回的數(shù)組的新長(zhǎng)度) console.log(arr); // [3, 4, 4, 5, 4, 6, 5, 7, 8, 9]toString()
可把值轉(zhuǎn)換成字符串。
const arr = [3,4,4,5,4,6,5,7]; console.log(arr.toString()); // 3,4,4,5,4,6,5,7concat()
在原始數(shù)據(jù)尾部添加另外數(shù)據(jù)組成新數(shù)據(jù)(字符串也適用)。
不過(guò)一般沒(méi)什么人用了 不考慮 ie 的話 直接用擴(kuò)展運(yùn)算… 就可以了
//數(shù)組 const a = [1,2,3]; const b = [4,5]; const c = a.concat(b); // [1, 2, 3, 4, 5]//字符串 const x = 'abc'; const y = 'def'; const z = x.concat(y); // abcdefjoin()
以參數(shù)作為分隔符,將所有參數(shù)組成一個(gè)字符串返回(默認(rèn)逗號(hào)分隔)。
const arr = [3,4,4,5,4,6,5,7]; console.log(arr.join('-')); // 3-4-4-5-4-6-5-7slice(start, end)
用于提取原來(lái)數(shù)組的一部分,會(huì)返回一個(gè)提取的新數(shù)組,原數(shù)組不變(字符串適用,不包括 end)。
//數(shù)組 const arr = [3,4,4,5,4,6,5,7]; const a = arr.slice(2, 5); // [4, 5, 4]//字符串 const x = 'abcdefgh'; const y = x.slice(3, 6); // defsplice()
用于刪除原數(shù)組的一部分,并且可以在刪除的位置添加新的數(shù)組成員,返回值是被刪除的數(shù)組元素。(改變?cè)瓟?shù)組)
splice(t, v, s)t: 被刪除元素的起始位置;v:被刪除元素個(gè)數(shù);s:s 以及后面的元素為被插入的新元素。
const arr = [3,4,4,5,4,6,5,7]; const a = arr.splice(3, 2, 12); // [5, 4] console.log(arr); // [3, 4, 4, 12, 6, 5, 7]map()
依次遍歷數(shù)組成員,根據(jù)遍歷結(jié)果返回一個(gè)新數(shù)組。(不會(huì)改變?cè)紨?shù)組)。
const arr = [3,4,4,5,4,6,5,7]; const a = arr.map(item => item*2;) // [6, 8, 8, 10, 8, 12, 10, 14]forEach()
跟 map 方法類(lèi)似,遍歷數(shù)組,區(qū)別是無(wú)返回值。
const arr = [3,4,4,5,4,6,5,7]; arr.forEach(function(item,index,arr){console.log(value)}))34454657for in()
跟 map 方法類(lèi)似,遍歷對(duì)象或者數(shù)組。
但值得注意的是 for in 循環(huán)返回的值都是數(shù)據(jù)結(jié)構(gòu)的鍵值名。遍歷對(duì)象返回的對(duì)象的 key 值,遍歷數(shù)組返回的數(shù)組的下標(biāo) (key)。
// 對(duì)象 const obj = {a: 123, b: 12, c: 2 }; for (let a in obj) {console.log(a) } // abc //數(shù)組 const arr = [3,4,4,5]; for(let a in arr) {console.log(a) } // 0123filter()
一個(gè)過(guò)濾方法,參數(shù)是一個(gè)函數(shù),所有的數(shù)組成員依次執(zhí)行該函數(shù),返回結(jié)果為 true 的成員組成一個(gè)新數(shù)組返回。(不會(huì)改變?cè)紨?shù)組)。
const arr = [3,4,4,5,4,6,5,7]; const a = arr.filter(item => item % 3 > 1); console.log(a); // [5, 5]some()& every()
這兩個(gè)方法類(lèi)似于 “斷言”(assert),用來(lái)判斷數(shù)組成員是否符合某種條件。
const arr = [3,4,4,5,4,6,5,7];console.log( arr.some( function( item, index, array ){ console.log( 'item=' + item + ',index='+index+',array='+array ); return item > 3; })); // item=3,index=0,array=3,4,4,5,4,6,5,7 // item=4,index=1,array=3,4,4,5,4,6,5,7 // trueconsole.log( arr.every( function( item, index, array ){ console.log( 'item=' + item + ',index='+index+',array='+array ); return item > 3; })); // item=3,index=0,array=3,4,4,5,4,6,5,7 //falsesome
some 方法是只要有一個(gè)數(shù)組成員的返回值為 true,則返回 true,否則 false;
every
every 方法是需要每一個(gè)返回值為 true,才能返回 true,否則為 false;
reduce()
依次處理數(shù)組的每個(gè)成員,最終累計(jì)成一個(gè)值。
格式:
reduce(() => (pre, cur, curIndex, arr), initialValue)pre: 必填,累計(jì)變量;cur:必填,當(dāng)前變量;curIndex: 可選,當(dāng)前位置;arr: 可選,原數(shù)組;initialValue: 傳遞給函數(shù)的初始值。
indexOf()
返回給定元素在數(shù)組中的第一次出現(xiàn)的位置,如果沒(méi)有則返回 - 1 (同樣適用于字符串)。
//數(shù)組 const arr = [3,4,4,5,4,6,5,7]; console.log(arr.indexOf(4)) // 1 console.log(arr.indexOf('4')) // -1//字符串 const string = 'asdfghj'; console.log(string.indexOf('a')) // 0 lastIndexOf()返回給定元素在數(shù)組中最后一次出現(xiàn)的位置,沒(méi)有返回 - 1 (同樣適用于字符串)。
const arr = [3,4,4,5,4,6,5,7]; console.log(arr.lastIndexOf(4)) // 4(從左到右數(shù)最后出現(xiàn)的位置,字符串同理)flatten()
簡(jiǎn)寫(xiě)為 flat(),接收一個(gè)數(shù)組,無(wú)論這個(gè)數(shù)組里嵌套了多少個(gè)數(shù)組,flatten 最后都會(huì)把其變成一個(gè)一維數(shù)組 (扁平化)。
const arr = [[1,2,3],[4,5,[6,7]]]; const a = arr.flatten(3); console.log(a); // [1, 2, 3, 4, 5, 6, 7]Array.isArray()
用來(lái)判斷是不是數(shù)據(jù)是不是一個(gè)數(shù)組,返回值為 true 或 false。
const arr = [3,4,4,5,4,6,5,7]; console.log(Array.isArray(arr)) // truefind()
返回符合傳入測(cè)試(函數(shù))條件的數(shù)組元素。
const arr = [3,4,4,5,4,6,5,7]; const a = test.find(item => item > 3); console.log(a); //4(find() 方法返回通過(guò)測(cè)試(函數(shù)內(nèi)判斷)的數(shù)組的第一個(gè)元素的值。)const b = test.find(item => item == 0); console.log(b); //undefined(如果沒(méi)有符合條件的元素返回 undefined)String
charAt()
用于返回指定位置的字符。
const str = 'hello guys'; console.log(str.charAt(3)) // 1charCodeAt()
用于返回指定位置的字符的 Unicode 編碼。
const str = 'hello guys'; console.log(str.charCodeAt(3)) // 111match()
用于在字符串內(nèi)檢索指定的值或找到一個(gè)或多個(gè)正則表達(dá)式的匹配,返回的是值而不是值的位置。
const str = 'hello guys'; console.log(str.match('guys')) // ["guys"]// 使用正則匹配字符串 const strs = '1.hello guys, 2.are you ok?'; console.log(strs.match(/\d+/g)) // ["1", "2"]replace()
替換匹配的字符串。
const str = 'hello guys'; console.log(str.replace('guys', 'man')) // hello man抽出字符串中的某一項(xiàng)指定字符。
const str = 'AA_BB_CC.bin'; console.log(str.replace(/[^\_]/g),'') // __(兩個(gè)下劃線)search()
用于檢索與字符串匹配的子串,返回的是地址,與 indexOf() 的區(qū)別是 search 是強(qiáng)制正則的,而 indexOf 只是按字符串匹配的。
const str = 'hello guys'; console.log(str.search('guys')) // 6 console.log(str.indexOf('guys')) // 6 // 區(qū)別 const string = 'abcdefg.1234'; console.log(string.search(/\./)) // 7(轉(zhuǎn)譯之后可以匹配到 . 的位置) console.log(string.indexOf(/\./)) // -1 (相當(dāng)于匹配/\./,找不到則返回-1,只能匹配字符串)split()
將字符串切割成數(shù)組。
const str = 'hello guys'; console.log(str.split('')) // ["h", "e", "l", "l", "o", " ", "g", "u", "y", "s"] console.log(str.split('', 3)) // ["h", "e", "l"]toLocaleLowerCase()& toLowerCase()
將字符串轉(zhuǎn)換成小寫(xiě)。
const str = 'hello guys'; console.log(str.toLocaleLowerCase()) // hello guys console.log(str.toLowerCase()) // hello guystoLocaleUpperCase() & toUpperCase()
將字符串轉(zhuǎn)換成大寫(xiě)。
const str = 'hello guys'; console.log(str.toLocaleUpperCase()) // HELLO GUYS console.log(str.toUpperCase()) // HELLO GUYSsubstr()
用于從起始索引號(hào)提取字符串中指定數(shù)目的字符。
const str = 'hello guys'; console.log(str.substr(2)) // llo guys console.log(str.substr(2, 7)) // llo guysubstring()
用于提取字符串中兩個(gè)指定索引號(hào)之間的字符。(與 slice() 和 substr() 方法不同的是,substring() 不接受負(fù)的參數(shù)。)
const str = 'hello guys'; console.log(str.substring(2)) // llo guys console.log(str.substring(2, 7)) // llo g.trim()
去掉字符串兩端的空格。
const str = ' hello guys '; console.log(str.trim()) // hello guys(不會(huì)改變?cè)瓟?shù)組)JSON
JSON.parse()
用于把字符串轉(zhuǎn)化為對(duì)象。
const str = '{"name": "phoebe", "age": 20}'; const obj = JSON.parse(str) // {name: "phoebe", age: 20}(object類(lèi)型)JSON.stringify()
用于把對(duì)象轉(zhuǎn)化為字符串。
const obj = {"name": "Tins", "age": 22}; const str = JSON.stringify(obj) // {"name":"Tins","age":22}(string類(lèi)型)Object
Object.Prototype.valueOf()
返回當(dāng)前對(duì)象對(duì)應(yīng)的值。(Object.valueOf()相當(dāng)于 Object.Prototype.ValueOf())
我們創(chuàng)建一個(gè)取代 valueOf() 方法的函數(shù),但是需要注意的是方法必須不能傳入?yún)?shù) 。假設(shè)我們有個(gè)對(duì)象叫 ObjectrType 而我想為它創(chuàng)建一個(gè) valueOf() 方法。下面的代碼為 valueOf() 方法賦予了一個(gè)自定義函數(shù):
ObjectrType.prototype.valueOf = function() { return customValue; };有了這樣的一個(gè)方法,下一次每當(dāng) ObjectrType 要被轉(zhuǎn)換為原始類(lèi)型值時(shí),JavaScript 在此之前會(huì)自動(dòng)調(diào)用自定義的 valueOf() 方法。valueOf() 方法一般都會(huì)被 JavaScript 自動(dòng)調(diào)用,但我們也可以像下面代碼那樣自己調(diào)用:
ObjectrType.valueOf()valueOf 同樣適用于 string,number, symbol,boolean,date。
Object.Prototype.toString()
返回當(dāng)前對(duì)象對(duì)應(yīng)的字符串形式。
function Dog(name) {this.name = name; }const dog1 = new Dog('Gabby');Dog.prototype.toString = function dogToString() {return '' + this.name; }console.log(dog1.toString()); // GabbyObject.Prototype.toLocaleString()
返回當(dāng)前對(duì)象對(duì)應(yīng)的模塊字符串。
語(yǔ)法:obj.toLocaleString();
let foo = {}; foo.toLocaleString(); // "[object Object]"Object.Prototype.isPrototypeOf()
判斷當(dāng)前對(duì)象是否為另一個(gè)對(duì)象的原型。語(yǔ)法:Object.prototype.isPrototypeOf(targetObj)
const arr = [];Array.prototype.isPrototypeOf(arr); // true// 修改obj的原型 Object.setPrototypeOf(arr, String.prototype); Array.prototype.isPrototypeOf(arr); // false String.prototype.isPrototypeOf(arr); // trueObject.Prototype.hasOwnProperty()
判斷某個(gè)屬性是否為當(dāng)前對(duì)象自身的屬性,還是繼承自原型對(duì)象的屬性,并返回一個(gè)布爾值。
語(yǔ)法:Object.prototype.hasOwnProperty(prop)
let obj = {};// 定義一個(gè)object實(shí)例 obj.prop1 = 'value1'; // prop1是一個(gè)自有屬性 obj.constructor.prototype.prop2 = 'value2'; // prop2是一個(gè)原型鏈屬性 // 無(wú)論自有屬性還是原型鏈屬性,我們都可以訪問(wèn)到 console.info(obj.prop1); // value1 console.info(obj.prop2); // value2 // 使用`hasOwnProperty()`方法判斷屬性是否為自有屬性 obj.hasOwnProperty('prop1'); // true obj.hasOwnProperty('prop2'); // falseObject.Prototype.PropertyIsEnumerable()
判斷某個(gè)屬性是否可枚舉。
語(yǔ)法:Object.prototype.propertyIsEnumerable(prop)
const obj = { name: 'ecmaer'};Object.getOwnPropertyDescriptor(obj, 'name').enumerable; // true obj.propertyIsEnumerable('name'); // true// 將屬性name設(shè)置成不可枚舉 Object.defineProperty(obj, 'name', {enumerable: false}); obj.propertyIsEnumerable('name'); // falsefor(let i in obj){console.info(obj[i]); // 沒(méi)有遍歷出'ecmaer' }判斷一個(gè)值的類(lèi)型的辦法
typeOf()
typeof 可用來(lái)檢測(cè)數(shù)據(jù)類(lèi)型:需要注意的是 typeof 無(wú)法區(qū)分 null、Array 和 通常意義上的 object。
typeof 123 //number typeof '123' //string typeof true // boolean typeof false //boolean typeof undefined // undefined typeof Math.abs // function typeof function () {} // function// 當(dāng)遇上`null`、`Array`和通常意義上的`object`,都會(huì)返回 object typeof null // object typeof [] // object(所以判斷數(shù)組時(shí)可以使用Array.isArray(arr)) typeof {} // object// 當(dāng)數(shù)據(jù)使用了new關(guān)鍵字和包裝對(duì)象以后,數(shù)據(jù)都會(huì)變成Object類(lèi)型,不加new關(guān)鍵字時(shí)會(huì)被當(dāng)作普通的函數(shù)處理。 typeof new Number(123); //'object' typeof Number(123); // 'number'typeof new Boolean(true); //'object' typeof Boolean(true); // 'boolean'typeof new String(123); // 'object' typeof String(123); // 'string'instanceOf()
instanceOf()運(yùn)算符用于檢測(cè)構(gòu)造函數(shù)的 prototype 屬性是否出現(xiàn)在某個(gè)實(shí)例對(duì)象的原型鏈。
function Car(make, model, year) {this.make = make;this.model = model;this.year = year; } const auto = new Car('Honda', 'Accord', 1998);console.log(auto instanceof Car); // true console.log(auto instanceof Object); // trueObject.Prototype.toString()(推薦)
可以精準(zhǔn)的判斷對(duì)象類(lèi)型。
對(duì)于 array、null、object 來(lái)說(shuō),其關(guān)系錯(cuò)綜復(fù)雜,使用 typeof 都會(huì)統(tǒng)一返回 object 字符串,要想?yún)^(qū)別對(duì)象、數(shù)組、函數(shù)單純使用 typeof 是不行的,想要準(zhǔn)確的判斷對(duì)象類(lèi)型,推薦使用 Object.Prototype.toString(), 它可以判斷某個(gè)對(duì)象值屬于哪種內(nèi)置類(lèi)型。
const arrs = [1,2,3]; console.log(typeof arrs) // object console.log(Object.Prototype.toString.call(arrs)) // [object Array]call,apply 以及 bind 的用法,區(qū)別及相似住處
用法
call
直接調(diào)用該執(zhí)行函數(shù),在執(zhí)行的時(shí)候,將函數(shù)內(nèi)部的作用域綁定到指定的作用域。(call() 方法接受若干個(gè)參數(shù)的列表)
const arr = [2,5,4,7,6] const a = Function.prototype.apply.call(Math.max, null,arr) console.log(a) // 7apply
直接調(diào)用該執(zhí)行函數(shù),在執(zhí)行的時(shí)候,將函數(shù)內(nèi)部的作用域綁定到指定的作用域。
call() 是 apply() 的一顆語(yǔ)法糖,作用和 apply() 一樣,同樣可實(shí)現(xiàn)繼承,唯一的區(qū)別就在于 call() 接收的是參數(shù)列表,而 apply() 則接收參數(shù)數(shù)組。
const arr = [2,5,4,7,6] const a = Function.prototype.call.apply(Math.max, arr) console.log(a) // 7//如果apply的第二個(gè)參數(shù)是個(gè)null,會(huì)返回-Infinity const b = Function.prototype.call.apply(Math.max, null, arr) console.log(b) // -Infinitybind
創(chuàng)建一個(gè)新的函數(shù)的引用,并綁定到一個(gè)作用域特定作用域上,同時(shí)支持傳參。
bind 則和 call 的用法差不多,唯一區(qū)別是,call 和 apply 會(huì)立刻調(diào)用函數(shù),bind 只是綁定 this。
格式為:bind(作用域參數(shù),參數(shù) 1,參數(shù) 2)
const fruits = {"name": "apple",getOtherFriut: function() {return this.name;} }const color = {"name": " is red" }const fruitColor = fruits.getOtherFriut.bind(this, color) console.log(fruitColor()) //is red相似之處
1、都是用來(lái)改變函數(shù)的 this 對(duì)象;
2、第一個(gè)參數(shù)都是 this 要指向的對(duì)象;
3、都可利用后繼參數(shù)傳參;
區(qū)別
1、都可以用來(lái)代替另一個(gè)對(duì)象調(diào)用一個(gè)方法,將一個(gè)函數(shù)的對(duì)象上下文從初始的上下文改變?yōu)橛?thisObj 指定的新對(duì)象。
2、bind() 是返回一個(gè)新函數(shù),供以后調(diào)用,而 apply() 和 call() 是立即調(diào)用。
3、call() 和 apply() 唯一區(qū)別是參數(shù)不一樣,call() 是 apply() 的語(yǔ)法糖;
選擇使用
1、如果不需要關(guān)心具體有多少參數(shù)被傳入函數(shù),選用 apply();
2、如果確定函數(shù)可接收多少個(gè)參數(shù),并且想一目了然表達(dá)形參和實(shí)參的對(duì)應(yīng)關(guān)系,用 call();
3、如果想要將來(lái)再調(diào)用方法,不需立即得到函數(shù)返回結(jié)果,則使用 bind();
Date 對(duì)象的用法
首先需要定義一個(gè)變量:
const date = new Date();接下來(lái)就可以直接使用常見(jiàn)的 Date 對(duì)象方法。
Date (): 返回當(dāng)日的日期和時(shí)間;
getDate (): 從 Date 對(duì)象返回一個(gè)月中的某一天(1~31)console.log(date.getDate());
getDay (): 從 Date 對(duì)象返回一周中的某一天(0~6);
getMonth (): 從 Date 對(duì)象返回月份(0~11);
getFullYear (): 從 Date 對(duì)象以四位數(shù)字返回年份;
getYear ():可以使用 getFullYear () 代替;
getHours (): 返回 Date () 對(duì)象的小時(shí)(0~23);
getMinutes (): 返回 Date () 對(duì)象的分鐘(0~59);
getSeconds (): 返回 Date () 對(duì)象的分鐘(0~59);
getMillseconds (): 返回 Date () 對(duì)象的毫秒(0~999);
getTime (): 返回 1970 年 1 月 1 日至今的時(shí)間;
getTimezoneOffset (): 返回本地時(shí)間與格林威治標(biāo)準(zhǔn)時(shí)間(GMT)的分鐘差;
getUTCDate (): 根據(jù)世界時(shí)從 Date 對(duì)象返回月中的一天(1~31);
getUTCDay (): 根據(jù)世界時(shí)從 Date 對(duì)象返回周中的一天(1~6);
getUTCMonth (): 根據(jù)世界時(shí)從 Date 對(duì)象返回月份(0~11);
getUTCFullYear (): 根據(jù)世界時(shí)從 Date 對(duì)象返回四位數(shù)的年份;
getUTCHours (): 根據(jù)世界時(shí)從 Date 對(duì)象返回對(duì)象的小時(shí)(0~23);
getUTCMinutes (): 根據(jù)世界時(shí)從 Date 對(duì)象返回對(duì)象的分鐘(0~59);
getUTCSeconds (): 根據(jù)世界時(shí)從 Date 對(duì)象返回對(duì)象的秒鐘(0~59);
getUTCMillseconds (): 根據(jù)世界時(shí)從 Date 對(duì)象返回對(duì)象的毫秒(0~999);
parse (): 返回 1970 年 1 月 1 日午夜到指定日期(字符串)的毫秒數(shù);
setDate (): 設(shè)置 Date 對(duì)象中月的某一天(1~31);
setMonth (): 設(shè)置 Date 對(duì)象中月份(0~11);
setFullYear (): 設(shè)置 Date 對(duì)象中的年份(四位數(shù)字);
Math.xx 開(kāi)頭的方法
Math.ceil (): 對(duì)數(shù)進(jìn)行上舍入(天花板函數(shù))
大于等于 x,并且與它最接近的整數(shù)。
Math.floor (): 對(duì)數(shù)進(jìn)行下舍入(地板函數(shù))。
Math.max (x,y): 返回 x,y 中的最大值。
Math.min (x,y): 返回 x,y 中的最小值。
Math.pow (x,y): 返回 x 的 y 次方。
Math.random () : 返回 0-1 之間的隨機(jī)數(shù)。
Math.round (x): 四舍五入。
Math.abs (x): 返回?cái)?shù)的絕對(duì)值。
Math.acos (x): 返回?cái)?shù)的反余弦值。
Math.asin (x): 返回?cái)?shù)的反正弦值。
Math.atan (x): 返回?cái)?shù)的反正切值。
Math.atan2 (y,x): 返回由 x 軸到點(diǎn)(x,y)的角度(以弧度為單位)。
Math.cos (x): 返回?cái)?shù)的余弦值。
Math.exp (e): 返回 e 的指數(shù)。
Math.log (x): 返回?cái)?shù)的自然對(duì)數(shù)(以 e 為底數(shù))。
Math.sin (x): 返回?cái)?shù)的正弦值。
Math.sqrt (x): 返回?cái)?shù)的平方根。
Math.tan (x): 返回角的正切值。
Math.toSource (): 返回該對(duì)象的源代碼。
Math.valueOf (): 返回 Math 對(duì)象的原始值。
總結(jié)
以上是生活随笔為你收集整理的JavaScript一些常用 API整理汇总的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: JavaScript创建或填充任意长度的
- 下一篇: 小程序canvas绘制商品海报实现分享朋