formations/javascript/base/fonctions-4.md

24 lines
693 B
Markdown
Raw Normal View History

# .cadoles-slide-title[Fonctions (4/5)]
2015-03-26 17:36:34 +01:00
**Manipulation du contexte d'exécution et injection d'arguments**
```js
var showThisAndArgs = function() {
console.log(this, arguments);
};
showThisAndArgs(); // -> Window, []
// Lier une fonction à un contexte
2015-03-26 17:36:34 +01:00
var myContext = {};
var bound = showThisAndArgs.bind(myContext);
bound(); // -> myContext, []
// Invoquer une fonction en modifiant son contexte et/ou en injectant des arguments
2015-03-26 17:36:34 +01:00
showThisAndArgs.call(myContext, 'arg1', 'arg2'); // -> myContext, ['arg1', 'arg2']
showThisAndArgs.apply(myContext, ['arg1', 'arg2']); // -> myContext, ['arg1', 'arg2']
```