这篇文章主要介绍“es6的使用技巧有哪些”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“ES6的使用技巧有哪些”文章能帮助大家解决问题。前言:ECMAScript 6(简称ES6)是于201
这篇文章主要介绍“es6的使用技巧有哪些”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“ES6的使用技巧有哪些”文章能帮助大家解决问题。
ECMAScript 6(简称ES6)是于2015年6月正式发布的javascript语言的标准,正式名为ECMAScript 2015(ES2015)。它的目标是使得JavaScript语言可以用来编写复杂的大型应用程序,成为企业级开发语言 。
另外,一些情况下ES6也泛指ES2015及之后的新增特性,虽然之后的版本应当称为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 izeyeixconst reverseEachWord = reverseBySeparator(reverseSentence, " ")console.log(reverseEachWord)// izeyeix sj os ,emosdnah .lolfunction reverseBySeparator(string, separator) { return string.split(separator).reverse().join(separator)}
const num = 45num.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'// }
// == -> 类型转换 (浅比较)// === -> 无类型转换 (严格比较)0 == false // true0 === false // false1 == "1" // true1 === "1" // falsenull == undefined // truenull === 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); // Swedenconsole.log(animals); // 3console.log(lions); // Lionsconsole.log(tigers); // Tigersconsole.log(bears); // Bears
let bears = 'bears'let tigers = 'tigers'[bears, tigers] = [tigers, bears]console.log(bears) // tigersconsole.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')) // trueconsole.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')) // trueconsole.log(isAnagram('rat', 'car')) // falseconsole.log(isAnagram('heArT', 'traEH')) // true
判断两个字符串是否为互相排列: 给定两个字符串,一个是否是另一个的排列
const player = { name: 'xieyezi', rating: 1000, click: () => { return 'click' }, pass: (teammate) => { return `Pass to ${teammate}` },}console.log(player?.name) // xieyeziconsole.log(player?.click?.()) // clickconsole.log(player?.teammate?.()) // undefined
// condition ? expression if true : expression if falseconst oxygen = 10const 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, 不会改变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.123456789const fixed2 = num.toFixed(2)const fixed3 = num.toFixed(3)console.log(fixed2) // 0.12console.log(fixed3) // 0.123
const numbers = [1, 2, 3, 4, 5]numbers.length = 0console.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) // 99console.log(min) // -45
const nullval = nullcost emptyString = ''const someNum = 13const a = nullval ?? 'A default'const b = emptyString ?? 'B default'const c = SomeNum ?? 'C default'console.log(a) // A defaultconsole.log(b) // '' // empty string != undefined or nullconsole.log(c) // 13
const nums = [1, 0 , undefined, null, false];const truthyNums = nums.filter(Boolean);console.log(truthyNums) // [1]
关于“ES6的使用技巧有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网精选频道,小编每天都会为大家更新不同的知识点。
--结束END--
本文标题: ES6的使用技巧有哪些
本文链接: https://www.lsjlt.com/news/333447.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0