您好,登錄后才能下訂單哦!
這篇文章主要介紹“ES6的使用技巧有哪些”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“ES6的使用技巧有哪些”文章能幫助大家解決問(wèn)題。
ECMAScript 6(簡(jiǎn)稱ES6)是于2015年6月正式發(fā)布的JavaScript語(yǔ)言的標(biāo)準(zhǔn),正式名為ECMAScript 2015(ES2015)。它的目標(biāo)是使得JavaScript語(yǔ)言可以用來(lái)編寫復(fù)雜的大型應(yīng)用程序,成為企業(yè)級(jí)開(kāi)發(fā)語(yǔ)言 。
另外,一些情況下ES6也泛指ES2015及之后的新增特性,雖然之后的版本應(yīng)當(dāng)稱為ES7、ES8等
let arr = ['????', 67, true, false, '55'] arr = arr.sort(() => 0.5 - Math.random()) console.log(arr) // [ '????', '55', 67, false, true ]
const str = 'xieyezi 23213 is 95994 so hansome 223333' const numbers = str.replace(/\D/g, '') console.log(numbers) // 2321395994223333
const sentence = 'xieyezi js so handsome, lol.' const reverseSentence = reverseBySeparator(sentence, "") console.log(reverseSentence); // .lol ,emosdnah os sj izeyeix const reverseEachWord = reverseBySeparator(reverseSentence, " ") console.log(reverseEachWord) // izeyeix sj os ,emosdnah .lol function reverseBySeparator(string, separator) { return string.split(separator).reverse().join(separator) }
const num = 45 num.toString(2) num.tostring(16)
const city = { name: 'Chongqing', population: '1,234,567,890' } const location = { longitude: '116.4', latitude: '39.9' } const fullCity = { ...city, ...location } console.log(fullCity) // { // name: 'Chongqing', // population: '1,234,567,890', // longitude: '116.4', // latitude: '39.9' // }
// == -> 類型轉(zhuǎn)換 (淺比較) // === -> 無(wú)類型轉(zhuǎn)換 (嚴(yán)格比較) 0 == false // true 0 === false // false 1 == "1" // true 1 === "1" // false null == undefined // true null === undefined // false
const forest = { location: 'Sweden', animals: 3, animalsTypes: ['Lions', 'Tigers', 'Bears'], }; const { location, animals, animalsTypes } = forest; const [lions, tigers, bears] = animalsTypes; console.log(location); // Sweden console.log(animals); // 3 console.log(lions); // Lions console.log(tigers); // Tigers console.log(bears); // Bears
let bears = 'bears' let tigers = 'tigers' [bears, tigers] = [tigers, bears] console.log(bears) // tigers console.log(tribes) // bears
const isRevervse = (str1, str2) => { const normalize = (str) => str.toLowerCase() .normalize('NFD') .split('') .reverse() .join('') return normalize(str1) === str2 } console.log(isRevervse('anagram', 'margana')) // true console.log(isRevervse('rac', 'car')) // true
回文字符串: 正著寫和反著寫都一樣的字符串)
const isAnagram = (str1, str2) => { const normalize = (str) => str.toLowerCase() .normalize('NFD') .split('') .sort() .join('') return normalize(str1) === normalize(str2) } console.log(isAnagram('anagram', 'nagaram')) // true console.log(isAnagram('rat', 'car')) // false console.log(isAnagram('heArT', 'traEH')) // true
判斷兩個(gè)字符串是否為互相排列: 給定兩個(gè)字符串,一個(gè)是否是另一個(gè)的排列
const player = { name: 'xieyezi', rating: 1000, click: () => { return 'click' }, pass: (teammate) => { return `Pass to ${teammate}` }, } console.log(player?.name) // xieyezi console.log(player?.click?.()) // click console.log(player?.teammate?.()) // undefined
// condition ? expression if true : expression if false const oxygen = 10 const diver = (oxygen < 10 ) ? 'Low oxygen' : 'High oxygen' console.log(diver) // High oxygen
const elements = [24, 'You', 777, 'breaking', 99, 'full'] const random = (arr) => arr[Math.floor(Math.random() * arr.length)] const randomElement = random(elements) console.log(randomElement) // 777
const octopus = { tentacles: 8, color: 'blue', } Object.freeze(octopus) octopus.tentacles = 10 // Error, 不會(huì)改變 console.log(octopus) // { tentacles: 8, color: 'blue'}
const animals = ['bears', 'lions', 'tigers', 'bears', 'lions'] const unique = (arr) => [...new Set(arr)] console.log(unique(animals)) // [ 'bears', 'lions', 'tigers' ]
const num = 0.123456789 const fixed2 = num.toFixed(2) const fixed3 = num.toFixed(3) console.log(fixed2) // 0.12 console.log(fixed3) // 0.123
const numbers = [1, 2, 3, 4, 5] numbers.length = 0 console.log(numbers) // []
const rgbToHex = (r, g, b) => { const toHex = (num) => { const hex = num.toString(16) return hex.length === 1 ? `0${hex}` : hex } return `#${toHex(r)}${toHex(g)}${toHex(b)}` } console.log(rgbToHex(46, 32, 67)) // #2e2043
const nums = [1, 2, 3, 4, 5, -3, 99, -45, -1] const max = Math.max(...nums) const min = Math.min(...nums) console.log(max) // 99 console.log(min) // -45
const nullval = null cost emptyString = '' const someNum = 13 const a = nullval ?? 'A default' const b = emptyString ?? 'B default' const c = SomeNum ?? 'C default' console.log(a) // A default console.log(b) // '' // empty string != undefined or null console.log(c) // 13
const nums = [1, 0 , undefined, null, false]; const truthyNums = nums.filter(Boolean); console.log(truthyNums) // [1]
關(guān)于“ES6的使用技巧有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。