구문 설탕, 왜, 언제, 어떻게
10561 단어 codequalitytodayilearned
왜
네이티브 API의 영역을 보여주고 코드에 더 많은 유연성과 가독성을 제공하며 다른 사람들이 더 짧고 간결한 코드를 작성하는 새로운 방법을 찾는 데 도움이 될 수 있으며 저는 하루 종일 진행할 수 있습니다.
언제
변화가 있을 때마다!
어떻게... 가자
// don't
let alex = personal.alex
// do destructuring
let {alex} = personal
// don't if variable asignation may not vary
var isSuperHuman = superhuman ? "is super human" : "nahh"
// use const instead
const isSuperHuman = superhuman ? "is super human" : "nahh"
// arrays to avoid for loops o foreach
arr.find(i => i.id === "01") // returns the object
// with destructuring
arr.find(({id}) => id === "01") // returns the object
arr.filter(({id}) => id === "01") // returns and array of matches elements
arr.some(({id}) => id === "01") // returns boolean value
arr.every(({type}) => type === "superhumen") // returns boolean value if every nodes matched the criteria
arr.reduce((acc, {age}) => acc + age, 0) // returns a reduced value in this case the sum of all ages
// concat and push are from the pass now we spread!
const a = [...arr, newElemet]
// or to place the new element at the top of the array
const a = [newElemet, ...arr]
// mergin to arrays
const a = [...arrB, ...arrC]
// or flatted!!
const a = [arrB, arrC].flat()
// get the unique values (names) from array, lets map?
const a = ...new Set(data.map(i => i.name))
// as array?
const a = [...new Set(data.map(i => i.name))]
// number to strings?
100.toString() // "100"
what about!
100 + "" // "100"
// strings to number?
Number("100") // 100
parseInt("100") // 100
parseFloat("100").toFixed(2) // 100.00
what about!
"100" * 1 // 100
그렇다면 내 친구 당신은 세상에서 가장 달콤한 사람이 될 수 있습니다!
함수를 선언하는 방법을 알려주십시오.
// as a declaration
function sum(a,b) {
return a + b
}
// as expression
const sum = function(a,b) {
return a + b;
};
// as an arrow?
const sum = (b,c) => b + c
// as shorthand method definition?
const operation = {
add: (a,b) => a + b
}
// or
const operation = {
add(a,b){return a + b}
다음 설탕에서 만나요! 당신의 정보를 알려주세요.
Reference
이 문제에 관하여(구문 설탕, 왜, 언제, 어떻게), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/alexandprivate/syntactic-sugar-why-when-and-how-29n8텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)