This article mainly talks about some effective JS tips with you. Friends in need can refer to it. I hope it can help everyone.
1. Ternary operator
When you want to write an if...else statement in the project, if it is not a multiple judgment, You can consider ternary operations instead
let a = 1, answer = null if( a > 5 ) { answer = '大帅比' }else{ answer = '前端强无敌' } 简写:answer = a > 5 ? '大帅比' : '前端强无敌'
2, for simplification (new method provided by ES6)
for(let i = 0; i < arr.length; i++){} 简写:for (let [index, item] of arr.entries()) { index为下标,item为每一项内容 }
3, arrow function (es6)
function test (){ console.info(name) } setTimeout(function() { console.info(''okay) }, 1500) arr.forEach(function (item) { console.info(item) }) 简写: let test = (args) => { console.info(name) } setTimeout(() => { console.info('okay') }, 1500) arr.forEach(item => console.info(item))
It seems more convenient, this will not change when the arrow function is used~!
4. Extension operator
Array merging:
const a = [1, 3, 5] const nums = [2, 4, 6].concat(a);
Array cloning: //Because the array is a reference type, it is often necessary to clone a separate one The operation
const arr = [1, 2, 3, 4] cons arr2 = arr.slice() 简写: const nums = [2, 4, 6, ...a] // [2, 4, ...a, 6]
a can be inserted at any position instead of appending only at the end, which is more convenient than concat!
Used in objects:
const obj = { a:1 , b:2, c:3, ...obj2 } = { a:1 , b:2, c:3, d:5, e: 6} obje = {d:5, e: 6}
5. Object attribute abbreviation
When the key value is the same
const obj = {x:x,y:y} 简写:cont obj = {x,y}
6. The abbreviation of the line string
const learn = 'study vue react rn nih\n\t' + 'study vue react rn nih\n\t' + 'study vue react rn nih\n\t' + 'study vue react rn nih\n\t' + 'study vue react rn nih\n\t' 简写:const learn = `study vue react rn nih study vue react rn nih study vue react rn nih study vue react rn nih study vue react rn nih`
7. The template string
const url = 'http://ab.cc.com/' + data + '/beta/info' 简写:const url = `http://ab.cc.com/${data}/beta/info`
${} can be directly put into the variable, which is very easy to use. Easy~remember the backticks! ! !
8. Array.find
If you want to find the required data in an array, you need to loop through it. Find in ES6 can simplify its operation
cont data = [ {'type': 'dog', 'color': 'red'}, {'type': 'cat', 'color: 'white'}, {'type': 'bird', 'color': 'blue'} ] function findeClor(color) { for(let i = 0; i < data.length; i ++ ) { if(data[i].type== 'cat' && data[i].color== color) { return data[i] } } } 简写:let cat = data.find(item => item.type == 'cat' && item.color== 'red')
9. Convert pseudo array to real array (Pseudo array has no interator interface)
let p = document.getElementById('p')
p is a pseudo array and cannot be traversed using methods such as forEach. You can use the more troublesome for loop
let p = Array.from(p) 或者 let p = [...p]
At this time, p is a truly traversable array
10. Array deduplication, Set
let a = [2, 3, 1, 2]
new Set() returns a pseudo array. You need to use the spread operator to convert it into a real array. Array.from() is also good. Selection
let b = [... new Set(a)] // [2, 3, 1]
11. Default parameters
function test(a, b, c = 'Dashuaibi', d = false){
}
test('front-end', 'Beima')
If the two parameters c and d are not passed in, then c defaults to 'Dashuaibi' and d defaults to false
12. Mandatory parameters
JS , if the function does not pass parameters, the parameters will default to undefined. This may cause a program exception, and an exception needs to be thrown
function(data) { if(data === undefined) { throw new Error('Missing') } return bar } 简写: mandatory = () => { throw new Erroe ('Missing') } foo = (bar = mandatory()) => { return bar }
The above is the detailed content of Some effective JS tips. For more information, please follow other related articles on the PHP Chinese website!