Skip to content

Instantly share code, notes, and snippets.

View manekinekko's full-sized avatar
:octocat:
Check me out on BlueSky @wassim.dev

Wassim Chegham manekinekko

:octocat:
Check me out on BlueSky @wassim.dev
View GitHub Profile
function Foo() {
this.bar = function() {
return this;
}
}
// exemple 1
var foo = new Foo();
console.log(foo); //=> Foo
console.log(foo.bar() instanceof Foo); //=> true
function foo() {
return this;
}
// invocation en tant que fonction
foo(); //=> this === window
var bar = {
'foo': foo
};
function foo() {};
foo(); //=> this === window
var bar = function() {};
bar(); //=> this === window
// ou en notation ES6
var bar = () => {};
bar(); //=> this === window
// astuce #1
var args = [].slice.call(arguments, 0);
// astuce #2
[].forEach.call(arguments, ( x => /* expression */ ) );
// astuce #3
const args = Array.from(arguments);
function foo() {
console.log(arguments.length); //=> 3
console.log(arguments); //=> [1,2,'bar']
console.log(arguments[2]); //=> 'bar'
console.log(arguments.filter); //=> undefined
}
foo(1,2,'bar');
function fonction1() {
var a = 1;
function fonction2() { /* ... */ }
var b = 2;
if (a === 1) {
var c = 3;
}
}
if (true) {
var foo = 'bar';
}
console.log(foo); //=> 'bar'
var dire = function(callback) {
alert(callback());
};
// ou en notation ES2015
const dire = (callback) => alert(callback());
function dire(callback) {
alert(callback());
}
function executerAuChargement() {
return dire(() => "Bonjour, JavaScript !");
}
window.onload = executerAuChargement;
function dire(callback) {
alert(callback());
}
function bonjour() {
return "Bonjour, JavaScript !";
}
function executerAuChargement() {
return dire(bonjour);
}
window.onload = executerAuChargement;