Outils du site

Des nouvelles de la Bourse : hausse de la chemise, baisse du pantalon et va-et-vient dans la fourrure ! [Coluche]

56-tools:javascript

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
56-tools:javascript [2017/01/12 09:48] – [Tests scripts web: jsbin] Roge56-tools:javascript [2018/01/20 02:59] (Version actuelle) – [références] Roge
Ligne 11: Ligne 11:
 [[http://www.crockford.com/javascript/private.html|Doub Crockford]] //"an inner function always has access to the vars and parameters of its outer function, **even after the outer function has returned**"// [[http://www.crockford.com/javascript/private.html|Doub Crockford]] //"an inner function always has access to the vars and parameters of its outer function, **even after the outer function has returned**"//
  
 +http://2ality.com/2011/04/modules-and-namespaces-in-javascript.html
 ===== ES6 ===== ===== ES6 =====
  
Ligne 38: Ligne 39:
 // this === objA: false // this === objA: false
 // self === objA: true // self === objA: true
 +</code>
 +
 +===== Avoid globals =====
 +
 +//"Global variables and function names are an incredibly bad idea. The reason is that every JavaScript file included in the page runs in the same scope."// http://dev.opera.com/articles/javascript-best-practices/
 +
 +Instead of 
 +<code:js>
 +var current = null;
 +function init() {
 +
 +}
 +function change() {
 +
 +}
 +function verify() {
 +
 +}
 +</code>
 +
 +Prefer:
 +
 +<code:js>
 +myNameSpace = function() {
 + var current = null;
 + function init() {
 +
 + }
 + function change() {
 +
 + }
 + function verify() {
 +
 + }
 + return{
 + init:init,
 + set:change
 + }
 +}();
 +</code>
 +
 +Calling myNameSpace.set() will now invoke the change() method.
 +
 +===== Bind =====
 +//"The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called."//
 +https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
 +
 +<code>
 +// Example showing binding some parameters
 +var sum = function(a, b) {
 +  return a + b;
 +};
 +
 +var add5 = sum.bind(null, 5);
 +console.log(add5(10));
 </code> </code>
 ===== Closure ===== ===== Closure =====
Ligne 65: Ligne 121:
 var say2 = sayHello2('Bob'); var say2 = sayHello2('Bob');
 say2(); // logs "Hello Bob" say2(); // logs "Hello Bob"
 +</code>
 +
 +
 +<code>
 +// closure example
 +function makeAdder(x) {
 +  return function(y) {
 +    return x + y;
 +  };
 +}
 +
 +var add5 = makeAdder(5);
 +var add100 = makeAdder(100);
 +
 +console.log(add5(2));  // 7
 +console.log(add100(2)); // 102
 +</code>
 +
 +Closure in loop:
 +<code>
 +for ( var d = 0; d < 3; d++ ) (function(d){ 
 + setTimeout(function(){ 
 +   console.log( "Value of d: ", d ); 
 +   console.log( d == d, "Check the value of d." ); 
 + }, d * 200); 
 +})(d);
 </code> </code>
  
Ligne 104: Ligne 186:
 console.log( namespace.foo ); // foo console.log( namespace.foo ); // foo
 </code> </code>
 +
 +http://tobyho.com/2011/11/02/callbacks-in-loops/
 +===== Public & Private methods =====
 +
 +<code>
 +var myObject = (function() {
 +  var privateVar = '';
 +
 +  function privateMethod () {
 +    // ...
 +  }
 +
 +  return { // public interface
 +    publicMethod: function () {
 +      // all private members are accesible here
 +    },
 +  };
 +})();
 +</code>
 +
 ===== Privileged method ===== ===== Privileged method =====
 +
  
 <code javascript> <code javascript>
Ligne 168: Ligne 271:
  
    * http://exploringjs.com/es6/ch_promises.html    * http://exploringjs.com/es6/ch_promises.html
 +   * http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html
  
 ===== promise library ===== ===== promise library =====
Ligne 282: Ligne 386:
 [[http://jsfiddle.net/jk3vstnb/|Fade in out message]] [[http://jsfiddle.net/jk3vstnb/|Fade in out message]]
  
 +[[http://jsfiddle.net/na7bkoo8/|JS Sorting an HTML table]]
 ===== Tests scripts web: jsbin ===== ===== Tests scripts web: jsbin =====
  
Dernière modification : 2017/10/06 23:38