Et bien d'autres...
if (true) {
var foo = 1;
let bar = 2;
}
console.log(foo);
// 1
console.log(bar);
// ReferenceError: bar is not defined
var foo = 1;
const bar = 2;
foo = 3;
// foo === 3
bar = 4;
// Line 6: "bar" is read-only
const foo = "string with double quote";
const bar = 'string with simple quote';
const fooBar = `string with back tick!
multiline!!
with interpolation ${foo} ${bar}`;
class Foo {
constructor() {
console.log('constructor');
}
bar() {
console.log('bar');
}
}
var Foo = (function () {
function Foo() {
//(...)
console.log('constructor');
}
Foo.prototype.bar = function bar() {
console.log('bar');
};
return Foo;
})();
let foo = (arg1, arg2) => {
console.log('foo called', arg1, arg2);
};
foo(1, 2); // foo called 1 2
foo = onlyOneArg => {
console.log('foo called', onlyOneArg);
}
foo(1); // foo called 1
foo = onlyOneArg => onlyOneArg + 1;
console.log(foo(1)); // 2
class Foo {
constructor() {
this.values = [1, 2, 3, 4, 5];
this.increment = 42;
this.values.map((value) => {
return value + this.increment;
});
}
}
// foo.js
export const foo = 42;
export const log = console.log;
// bar.js
import foo from './foo';
foo.log(foo.foo); // => 42
let [a, b] = [1, 2];
console.log(a, b); //=> 1 2
var foo = () => [1, 2, 3];
var [a, , b] = foo();
console.log(a, b);// => 1 3
var {user: x} = {user: 5};
console.log(x); // => 5
var { prop, prop2 } = {prop: 5, prop2: 10};
console.log(prop, prop2); // => 5 10
// foo.js
export const foo = 42;
export const log = console.log;
// bar.js
import { foo, log } from './foo';
log(foo); // => 42
<module>
<script>
// Callback
xhr(options, (response) => {
// do something
});
// Promise
const promise = xhr(option);
promise.then((response) => {
// do something
});
// Callback
xhr(options1, (response) => {
xhr(options2, (response) => {
xhr(options3, (response) => {
// do something
});
});
});
// Promise
xhr(options1).then((response) => {
return xhr(options2);
}).then((response) => {
return xhr(options3);
}).then((response) => {
// do something
});
const promise = new Promise(function (resolve, reject) {
/*...*/
if (/*...*/) {
resolve(value); // success
} else {
reject(reason); // failure
}
});
function *foo() {
yield 1;
yield 2;
yield 3;
}
var it = foo();
console.log( it.next() ); // { value:1, done:false }
console.log( it.next() ); // { value:2, done:false }
console.log( it.next() ); // { value:3, done:false }
console.log( it.next() ); // { value:undefined, done:true }
// Pseudo code, without any checks
function co(g) {
var it = g(), ret;
// asynchronously iterate over generator
(function iterate(val){
ret = it.next( val );
if (!ret.done) {
ret.value.then( iterate );
}
})();
}
co(function *() {
yield xhr(options1);
yield xhr(options2);
yield xhr(options3);
});
async function foo() {
await xhr(options1);
await xhr(options2);
await xhr(options3);
}
let arr = [3, 5, 7];
arr.foo = "hello";
for (let i in arr) {
console.log(i); // => 0 1 2 foo
}
for (let i of arr) {
console.log(i); // => 3 5 7
}
function multiplier(a, b = 1) {
return a * b;
}
multiplier(5); // => 5
function f(x=1, y) {
return [x, y];
}
f(); // => [1, undefined]
function foo(...args) {
console.log(args.length);
}
foo(); // => 0
foo(5); // => 1
foo(5, 6, 7); // => 3
foo(...[0, 1, 2]); // => 3