Skip to content

Instantly share code, notes, and snippets.

@kuzminT
Last active July 19, 2019 17:22
Show Gist options
  • Save kuzminT/37041f833174c5a168f614ee03685c89 to your computer and use it in GitHub Desktop.
Save kuzminT/37041f833174c5a168f614ee03685c89 to your computer and use it in GitHub Desktop.
JavaScript Tips

Полезные статьи

При делегировании событий удобно использовать метод Element.matches(), которому передаётся любой css-селектор.

О структурах данных

Private data with WeakMap use case

var Person = (function() {
  var privateData = new WeakMap();

  function Person(name) {
    privateData.set(this, { name: name });
  }

  Person.prototype.getName = function() {
    return privateData.get(this).name;
  };

  return Person;
}());

Private instance members with weakmaps in JavaScript

Webpack

Для использования extract-text-webpack-plugin с webpack 4 нужно установить его обновлённую версию:

$ npm install extract-text-webpack-plugin@next

Destructuring in practice examples

Destructuring object arguments

const profile = {
  title: 'Engineer',
  department: 'Engineering'
};

function isEngineer({title, department}) {
  return title === 'Engineer' && department === 'Engineering';
}

Convert array of arrays in array of objects

const classes = [
  [ 'Chemistry', '9AM', 'Mr. Darnick' ],
  [ 'Physics', '10:15AM', 'Mrs. Lithun'],
  [ 'Math', '11:30AM', 'Mrs. Vitalis' ]
];

const classesAsObject = classes.map(([subject, time, teacher]) => {
    return {subject, time, teacher};
});

Fetch

fetch polyfill

fetch('/users.json')
  .then(function(response) {
    return response.json()
  }).then(function(json) {
    console.log('parsed json', json)
  }).catch(function(ex) {
    console.log('parsing failed', ex)
  })

A practical ES6 guide on how to perform HTTP requests using the Fetch API

Decorators

Exploring EcmaScript Decorators

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment