On Github get-focus / formation-js
A retenir on utilise const au maximum et let dans les autres cas
						
'déjà' < 'demain'              // => false
'déjà'.localeCompare('demain') // => -1 ('déjà' avant 'demain')
'déjà !'.toLocaleUpperCase()      // => 'DÉJÀ !'
'ÇA POUTRE'.toLocaleLowerCase()   // => 'ça poutre'
'one,two,three'.split(',')        // => ['one', 'two', 'three']
'one,,two,three'.split(/\W+/)     // => ['one', 'two']
'hello'.substring(1)      // => 'ello'
'hello'.slice(1, -2)      // => 'el' -> [1;length-2[
						
					
				new Date // maintenant ! new Date(y,m,d[,h,m,s,ms]) // Valeur découpée. (un peu lourd...) date.getFullYear() // JAMAIS date.getMonth() // NAZE Java-like qui démarre à… zéro (janvier). #ugly date.getDay() // PERDUUUU ! C'est le DOW (Day Of Week), 0 = dim., 6 = s date.getDate() // Le jour du mois. Super logique. date.getHours() date.getMinutes() date.getSeconds() date.getMilliseconds() // "Milliseconds", un seul mot : une seule initiale. // Heure locale du navigateur/système. On a les mêmes en getUTCXxx()…
// "J'ai bien lu la doc" date.getTime() // => ex. 1290852603042 // "#superSmart" +date // Et sinon… new Date(1301733937452) // Mais aussi (car souci pour partir de composantes UTC) : Date.UTC(y, m, d, h, mn, s, ms) // => numérique
						
let obj = { first: 'David', last: 'Lopez', age: 77 };
obj.first  // => 'David'
obj['age'] // => 42
obj.first = 'pierr';
obj['first'] // => 'pierr'
obj[77] = 'roberto';
obj[77]   // => 'roberto'
obj['77'] // => 'roberto'
obj.77    // => SyntaxError
						
					
										
let defaults = { first: 'John', last: 'Doe', age: 42 };
let trainer  = { last: 'Smith', age: 35 };
trainer      = { ...defaults, ...trainer, age: 36 }
// => { first: 'John', last: 'Smith', age: 36 }
trainers['a'+ 12] = 'Gino';
trainers.a12;
// Gino
						
					let names = ['John', 'David', 'Rodrigo']; names.length // => 3. R/W : astuce pour réutiliser un tableau! names[0] // => 'John' names[12] = 'Pierre'; names.length // => 13 names[9] // => undefined (comme 10 et 11): c'est appelé "sparse array"
						
const ARRAY = [1,2,3,4];
for(let i = 0, _l=ARRAY.length; i < _l; i++ ){console.log(ARRAY[i])}
ARRAY.forEach(function(element, idx){console.log(element, idx)});
ARRAY.map(function(element, idx){console.log(element, idx)});
						
					
										
function maFonction() {
	return 'test';
}
maFonction() // test;
const a = () => 'test';
a(); //test
						
					
				== ou === ?! 42 == '42' // => true -- Argh, ça sent le PHP, là… null == undefined // => true -- hmmm… null == 0 // => false -- heureusement ! 0 == undefined // => false -- heureusement ! 0 == false // => true -- Façon C… 1 == true // => true -- Façon C… 42 == true // => false -- Watcha ?! (x == ToNumber(y), ES3 §11.9.3) '0' == false // => true -- Woah ! '' == false // => true -- Yowza ! 8-O On y reviendra… NaN == NaN // => false -- Bin oué, c’est le principe…
// avec ===, fini de jouer : vérif valeur ET TYPE ! 42 === '42' // => false null === undefined // => false null === 0 // => false 0 === undefined // => false 0 === false // => false '0' === false // => false NaN === NaN // => false -- rien à faire !On utilise toujours le ===
						
let person = { name: 'Joe', langs: ['fr', 'en'] };
'name' in person        // => true
person.age = 35;
'age' in person         // => true
person.age = null;
'age' in person         // => true
delete person.age
'age' in person         // => false
person.age              // => undefined
0 in person.langs       // => true
'0' in person.langs     // => true
person.langs[3] = 'de';
2 in person.langs       // => false
3 in person.langs       // => true
delete person.langs[3]
person.langs            // => ['fr', 'en', undefined, undefined]
						
					
									
<menu id="file" value="File">
  <popup>
    <menuitem value="New" onclick="CreateNewDoc()"></menuitem>
    <menuitem value="Open" onclick="OpenDoc()"></menuitem>
    <menuitem value="Close" onclick="CloseDoc()"></menuitem>
  </popup>
</menu>
					
				
									
{
    "menu": {
        "id": "file",
        "value": "File",
        "popup": {
            "menuitem": [
                { "value": "New", "onclick": "CreateNewDoc()" },
                { "value": "Open", "onclick": "OpenDoc()" },
                { "value": "Close", "onclick": "CloseDoc()" }
            ]
        }
    }
}
					
				
									
//Avant
const monElement = $('[data-my-selector]');
//Après
const monElement = document.querySelector('[data-my-selector]');
					
				
									
/* bling.js */
window.$ = document.querySelectorAll.bind(document);
Node.prototype.on = window.on = function (name, fn) {
  this.addEventListener(name, fn);
}
NodeList.prototype.__proto__ = Array.prototype;
NodeList.prototype.on = NodeList.prototype.addEventListener = function (name, fn) {
  this.forEach(function (elem, i) {
    elem.on(name, fn);
  });
}
				
Voir le gist de Paul Irish
			
										
function Person(first, last) {
  this.first = first;
  this.last = last;
}
var joeLopezPerson = new Person('Joe', 'Lopez');
var davidLopezz = new Person('David', 'lopez');
joeLopezPerson.first // => 'Joe'
davidLopezz.first   // => 'David'
// Si on jouait aux cons ?!
function LopezPerson(first, last) {
  this.first = first;
  this.last = last;
  return { first: 'Anne', last: 'Pas Lopez' };
}
var oops = new LopezPerson('Henry', 'Lopez');
oops.first // => Anne
						
					
										
function Person(first, last) {
  this.first = first;
  this.last = last;
}
// On augmente l'existant…
Person.prototype.fullName = function fullName() {
  return this.first + ' ' + this.last;
}
Person.prototype.greet = function greet() {
  alert('Salut je m’appelle ' + this.first);
}
var john = new Person('John', 'Smith');
john.fullName() // => 'John Smith'
john.greet()    // 'Salut je m’appelle John'
						
					
										
function Person(first, last) {
  this.first = first;
  this.last = last;
  this.fullName = function fullName() {
    return this.first + ' ' + this.last;
  }
  this.greet = function greet() {
    alert('Salut je m’appelle ' + this.first);
  }
}
var john = new Person('John', 'Smith');
john.fullName() // => 'John Smith'
john.greet()    // 'Salut je m’appelle John'
						
					
					Pas bon... on copie le truc dans chaque constructeur
										
class TodoItem extends Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      editing: false
    };
  }
  handleDoubleClick() {
    this.setState({ editing: true });
  }
  …
}
						
					
					Attention il s'agit de sucre syntaxique pas d'une classe comme en Java ou .NET. Le JS reste un langage prototypale.
										
function Person(first, last) {
  this.first = first;
  this.last = last;
}
Person.prototype.fullName = function fullName() {
  return this.first + ' ' + this.last;
};
const davidLopez = new Person('David', 'Lopez');
davidLopez.first      // => 'David',         own property
davidLopez.fullName() // => 'David Lopez', Person.prototype
davidLopez.toString() // => '[object Object]', Object.prototype
Person.prototype.toString = function personToString() {
  return '#Person ' + this.fullName();
};
davidLopez.toString() // => "#Person David Lopez"
						
					
										
function publicFx() {
  let dateAppel = Date.now();
  return function() { console.log(dateAppel); };
}
let privilegedFx1 = publicFx();
// Attendre un bref instant
let privilegedFx2 = publicFx();
// privilegedFx(1,2) sont en fait les fonctions internes construites au
// sein de publicFx, qui grâce aux règles de portée "voient"
// dateAppel.  Elles sont *closed over* par publicFx, ce qui fait
// que les valeurs de dateAppel au moment où les fonctions ont été
// renvoyéees sont préservées
privilegedFx1(); // => affiche la dateAppel du moment de la création de la fonction privilegedFx1!
privilegedFx2(); // => affiche la dateAppel d'après !
						
					
				
function yourModule(require, module, exports) {
  let widgets = {};
  let util = require('util');
  let Widget = require('widgets/base');
  function CoolWidget(elt) { … }
  util.inherits(CoolWidget, Widget);
  // …
  module.exports = Widget;
}
					
					Dans node on ne voit pas la fonction enrobante...
										
var name = 'Mr X';
let obj = {
  name: 'Joe Lopez',
  greet: function greet(whom) {
	console.log(this) 
    console.log(this.name + ' salue ' + whom);
  },
  greetAll: function greetAll(first, second, last) {
	console.log(this)
    [first, second, last].forEach(this.greet);
  }
};
obj.greet("les lopezs de France");
// => 'Joe Lopez salut les lopezs de France !'
let fx = obj.greet;
fx("l’atelier") // => '"Mr X salue l’atelier"'
obj.greetAll('David', 'Joe', 'Guénolé'); // => 'Mr X salue David, Mr X salue Joe, Mr X salue undefined'
						
					
										
const obj = {
  // …
  greetAll: function greetAll(first, second, last) {
    var that = this;
    [first, second, last].forEach(function(name) {
      that.greet(name);
    });
  }
}
						
					
										
const obj = {
  // …
  greetAll(first, second, last) {
    [first, second, last].forEach(name => this.greet(name));
	//Ultra fat
  }
}
						
					
										
//let fx = obj.greet;
fx.call(obj, 'les singes') // Joe Lopez salue les singes
let xy = { 0: 'Zero', 1: 'One', length: 2 };
Array.prototype.join.call(xy, '-') // 'Zero-One'
fx.apply(obj, ['']) // => 'Joe salue l’atelier'
Array.prototype.push.apply(xy, ['Two', 'Three', 'Four']) // => 5
xy // => { 0: 'Zero', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', length: 5 }
										
function delayedAlert() {
  window.setTimeout(slowAlert, 2000);
}
function slowAlert() {
  alert("That was really slow!");
}
					
				Une promesse a un état (pending, fullfilled, rejected). Elle est asynchrone, et se termine soit par un succès soit par une erreur et renvoie une nouvelle promesse.
promise.then(successCb,errorCb).then(otherSuccess, otherCb).catch(errorHandlingFn)
const myPromise = new Promise((resolve, reject) => {
	ajaxCall({success: resolve, error: reject});
})
Promise.resolve([1,2,3,4]);
Promise.reject('ma super erreur')
					
				
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)
  })					
										
//Dans un fichier
module.exports = monObjetAExporter;
//Utilisation
require('./mon_module_locale');
//Utilisation d'un module npm
require('mon_module');
					
										
import * as types from '../constants/ActionTypes';
export function addTodo(text) {
  return { type: types.ADD_TODO, text };
}
import React, { PropTypes, Component } from 'react';
import classnames from 'classnames';
import { SHOW_ALL, SHOW_COMPLETED, SHOW_ACTIVE } from '../constants/TodoFilters';
import 'http://material.js'
…
export default Footer;
export const ADD_TODO = 'ADD_TODO';
export const DELETE_TODO = 'DELETE_TODO';
export const EDIT_TODO = 'EDIT_TODO';
					
										
const { activeCount } = this.props;
…
const { filter: selectedFilter, onShow } = this.props;
const [, filters] = output.props.children;
…
const [,, clear] = output.props.children;
var { op: a, lhs: { op: b }, rhs: c } = getASTNode();
Détails
					
										
const person = { first: 'Thomas', last: 'Anderson', age: 25, nickname: 'Neo' };
// Interpolation de JS quelconque
console.log(`${person.first} aka ${person.nickname}`)
// => 'Thomas aka Neo'
// Multi-ligne !
const markup = `<li>
${person.first} ${person.last}, age ${person.age}
</li>`;
					
					
										
function add(source, numToAdd = 1){
	return source + numToAdd;
}
						
					
										
function editTodo(id, text) {
  return { type: types.EDIT_TODO, id, text };
  //On créé un objet avec pour clef le nom de la variable.
}
const FILTERS = {
  [maVar1]: 'All',
  [maVar2]: 'Active',
  [maVar3]: 'Completed'
};