-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[] JavaScript

, 08 2017 . 16:39 +
, JavaScript , ? , , . , , : ? , .



, JavaScript , .



, . , . .js . .

, , . ESLint, , ( --fix) IDE.

Prettier, ESLint, . .

:


, , StandardJS. , . :

  • 2
  • ( , )
  • ( if)

StandardJS Node . , ESLint predefined config. no-mixed-operators import/no-webpack-loader-syntax.

ES2015+


, ES2015+ ( ES6, ES7). , :

  • Arrow functions: x => x*2
  • : ,
  • :

function doSomething() {
  const a = doSomethingElse()
  const b = doSomethingWithA(a)
  const otherResults = { c: '', d: '' }
  return { a, b, ...otherResults } // equivalent to { a: a, b: b }
}
const { a, c, ...rest } = doSomething() // Also works with arrays!
// `rest` looks like { b: ..., d: '' }

  • async/await:

// Please try to write the same code with classic promises ;)
async function doSomething() {
  const a = await getValueForA()
  const b = await getValueForBFromA(a)
  const [c, d] = await Promise.all([
    // parallel execution
    getValueForC(), getValueForDFromB(b)
  ])
  const total = await calculateTotal(a, b, c, d)
  return total / 1000
}



. , JavaScript. ? , . :

for . . :

const arr = [{ name: 'first', value: 13 }, { name: 'second', value: 7 }]

// Instead of:
const res = {}
for (let i = 0; i < arr.length; i++) {
  const calculatedValue = arr[i].value * 10
  if (calculatedValue > 100) {
    res[arr[i].name] = calculatedValue
  }
}

// Prefer:
const res = arr
  .map(elem => ({ name: elem.name, calculatedValue: elem.value * 10 }))
  .filter(elem => elem.calculatedValue > 100)
  .reduce((acc, elem) => ({
    [elem.name]: calculatedValue,
    ...acc
  }), {})

, , , . :

const enrichElementWithCalculatedValue =
  elem => ({ name: elem.name, calculatedValue: elem.value * 10 })
const filterElementsByValue = value =>
  elem => elem.calculatedValue > value
const aggregateElementInObject = (acc, elem) => ({
  [elem.name]: calculatedValue,
  ...acc
})
const res = arr
  .map(enrichElementWithCalculatedValue)
  .filter(filterElementsByValue(100))
  .reduce(aggregateElementInObject, {})

, , .
: ( ) , !

, , . , pure () . :

  • , , ;
  • ;
  • .

, : !



  • , , observales RxJS ( )
  • ! , . JavaScript (front backend) , .
  • : , arr.indexOf(elem) !== -1. arr.includes(elem).
  • : subreddit JavaScript .

, : ! , . var const, async / await .
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/330534/

:  

: [1] []
 

:
: 

: ( )

:

  URL