Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
56-tools:javascript [2017/01/12 01:51] – [IIFE] Roge | 56-tools:javascript [2018/01/20 02:59] (Version actuelle) – [références] Roge | ||
---|---|---|---|
Ligne 11: | Ligne 11: | ||
[[http:// | [[http:// | ||
+ | http:// | ||
===== ES6 ===== | ===== ES6 ===== | ||
Ligne 38: | Ligne 39: | ||
// this === objA: false | // this === objA: false | ||
// self === objA: true | // self === objA: true | ||
+ | </ | ||
+ | |||
+ | ===== Avoid globals ===== | ||
+ | |||
+ | //" | ||
+ | |||
+ | Instead of | ||
+ | < | ||
+ | var current = null; | ||
+ | function init() { | ||
+ | … | ||
+ | } | ||
+ | function change() { | ||
+ | … | ||
+ | } | ||
+ | function verify() { | ||
+ | … | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Prefer: | ||
+ | |||
+ | < | ||
+ | myNameSpace = function() { | ||
+ | var current = null; | ||
+ | function init() { | ||
+ | … | ||
+ | } | ||
+ | function change() { | ||
+ | … | ||
+ | } | ||
+ | function verify() { | ||
+ | … | ||
+ | } | ||
+ | return{ | ||
+ | init: | ||
+ | set: | ||
+ | } | ||
+ | }(); | ||
+ | </ | ||
+ | |||
+ | 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:// | ||
+ | |||
+ | < | ||
+ | // Example showing binding some parameters | ||
+ | var sum = function(a, b) { | ||
+ | return a + b; | ||
+ | }; | ||
+ | |||
+ | var add5 = sum.bind(null, | ||
+ | console.log(add5(10)); | ||
</ | </ | ||
===== Closure ===== | ===== Closure ===== | ||
Ligne 65: | Ligne 121: | ||
var say2 = sayHello2(' | var say2 = sayHello2(' | ||
say2(); // logs "Hello Bob" | say2(); // logs "Hello Bob" | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | // closure example | ||
+ | function makeAdder(x) { | ||
+ | return function(y) { | ||
+ | return x + y; | ||
+ | }; | ||
+ | } | ||
+ | |||
+ | var add5 = makeAdder(5); | ||
+ | var add100 = makeAdder(100); | ||
+ | |||
+ | console.log(add5(2)); | ||
+ | console.log(add100(2)); | ||
+ | </ | ||
+ | |||
+ | Closure in loop: | ||
+ | < | ||
+ | for ( var d = 0; d < 3; d++ ) (function(d){ | ||
+ | | ||
+ | | ||
+ | | ||
+ | }, d * 200); | ||
+ | })(d); | ||
</ | </ | ||
Ligne 100: | Ligne 182: | ||
})( namespace ) | })( namespace ) | ||
- | console.log( namespace ); | + | console.log( namespace ); // { foo: ' |
- | console.log( namespace.speak() ); | + | console.log( namespace.speak() ); // bla bla |
- | console.log( namespace.foo ); | + | console.log( namespace.foo ); // foo |
- | // { foo: ' | + | |
- | // bla bla | + | |
- | // foo | + | |
</ | </ | ||
+ | |||
+ | http:// | ||
+ | ===== Public & Private methods ===== | ||
+ | |||
+ | < | ||
+ | var myObject = (function() { | ||
+ | var privateVar = ''; | ||
+ | |||
+ | function privateMethod () { | ||
+ | // ... | ||
+ | } | ||
+ | |||
+ | return { // public interface | ||
+ | publicMethod: | ||
+ | // all private members are accesible here | ||
+ | }, | ||
+ | }; | ||
+ | })(); | ||
+ | </ | ||
+ | |||
===== Privileged method ===== | ===== Privileged method ===== | ||
+ | |||
<code javascript> | <code javascript> | ||
Ligne 171: | Ligne 271: | ||
* http:// | * http:// | ||
+ | * http:// | ||
===== promise library ===== | ===== promise library ===== | ||
Ligne 285: | Ligne 386: | ||
[[http:// | [[http:// | ||
+ | [[http:// | ||
===== Tests scripts web: jsbin ===== | ===== Tests scripts web: jsbin ===== | ||
[[http:// | [[http:// | ||
+ | [[http:// | ||
===== Scritps ===== | ===== Scritps ===== | ||