| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Functions/Arrow_functions | [Back] [Original] |
Get to know MDN better
const materials = ["Hydrogen", "Helium", "Lithium", "Beryllium"];
console.log(materials.map((material) => material.length));
// : Array [8, 6, 7, 9]
() =>
=>
() =>
(1, N) =>
() => {
}
=> {
}
(1, N) => {
}
(a, b, ...r) =>
(a = 400, b = 20, c) =>
([a, b] = [10, 20]) =>
({ a, b } = { a: 10, b: 20 }) =>
async async
async =>
async (1, 2, ...N) => {
}
:
//
(function (a) {
return a + 100;
});
// 1. "function"
(a) => {
return a + 100;
};
// 2. "return" return
(a) => a + 100;
// 3.
a => a + 100;
//
(function (a, b) {
return a + b + 100;
});
//
(a, b) => a + b + 100;
const a = 4;
const b = 2;
//
(function () {
return a + b + 100;
});
//
() => a + b + 100;
return
//
(function (a, b) {
const chuck = 42;
return a + b + chuck;
});
//
(a, b) => {
const chuck = 42;
return a + b + chuck;
};
//
function bob(a) {
return a + 100;
}
//
const bob2 = (a) => a + 100;
(expression body) (block body)
return return undefined return return
//
const add = (a, b) => a + b; // a + b
//
const add2 = (a, b) => {
console.log(a, b);
return a + b; //
};
//
const add3 = (b) => {
a += b;
// return undefined
};
(params) => { object: literal }
const func = () => { foo: 1 };
// func() undefined
const func2 = () => { foo: function () {} };
// SyntaxError: function statement requires a name
const func3 = () => { foo() {} };
// SyntaxError: Unexpected token '{'
const func = () => ({ foo: 1 });
this
"use strict";
const obj = {
i: 10,
b: () => console.log(this.i, this),
c() {
console.log(this.i, this);
},
};
obj.b(); // undefined, Window { /* */ } ()
obj.c(); // 10, Object { /* */ }
"use strict";
const obj = {
a: 10,
};
Object.defineProperty(obj, "b", {
get: () => {
console.log(this.a, typeof this.a, this); // undefined 'undefined' Window { /* */ } ()
return this.a + 10; // 'Window' 'this.a' 'undefined'
},
});
class C {
a = 1;
autoBoundMethod = () => {
console.log(this.a);
};
}
const c = new C();
c.autoBoundMethod(); // 1
const { autoBoundMethod } = c;
autoBoundMethod(); // 1
//
class C {
a = 1;
constructor() {
this.method = this.method.bind(this);
}
method() {
console.log(this.a);
}
}
:
arguments arguments
function foo(n) {
const f = () => arguments[0] + n; // foo arguments arguments[0] n
return f();
}
foo(3); // 3 + 3 = 6
function foo(n) {
const f = (...args) => args[0] + n;
return f(10);
}
foo(1); // 11
const Foo = () => {};
const foo = new Foo(); // TypeError: Foo is not a constructor
console.log("prototype" in Foo); // false
const func = (a, b, c)
=> 1;
// SyntaxError: Unexpected token '=>'
const func = (a, b, c) =>
1;
const func2 = (a, b, c) => (
1
);
const func3 = (a, b, c) => {
return 1;
};
const func4 = (
a,
b,
c,
) => 1;
let callback;
callback = callback || () => {};
// SyntaxError: invalid arrow-function arguments
=> callback || ()
callback = callback || (() => {});
// undefined
const empty = () => {};
(() => "foobar")();
// "foobar"
//
const simple = (a) => (a > 15 ? 15 : a);
simple(16); // 15
simple(10); // 10
const max = (a, b) => (a > b ? a : b);
//
const arr = [5, 6, 13, 0, 1, 18, 23];
const sum = arr.reduce((a, b) => a + b);
// 66
const even = arr.filter((v) => v % 2 === 0);
// [6, 0, 18]
const double = arr.map((v) => v * 2);
// [10, 12, 26, 0, 2, 36, 46]
//
promise
.then((a) => {
//
})
.then((b) => {
//
});
//
setTimeout(() => {
console.log("I happen sooner");
setTimeout(() => {
// deeper code
console.log("I happen later");
}, 1);
}, 1);
const obj = {
num: 100,
};
// "num" window
globalThis.num = 42;
// "this"
function add(a, b, c) {
return this.num + a + b + c;
}
console.log(add.call(obj, 1, 2, 3)); // 106
console.log(add.apply(obj, [1, 2, 3])); // 106
const boundAdd = add.bind(obj);
console.log(boundAdd(1, 2, 3)); // 106
add window () this window
const obj = {
num: 100,
};
// "num" window
globalThis.num = 42;
//
const add = (a, b, c) => this.num + a + b + c;
console.log(add.call(obj, 1, 2, 3)); // 48
console.log(add.apply(obj, [1, 2, 3])); // 48
const boundAdd = add.bind(obj);
console.log(boundAdd(1, 2, 3)); // 48
DOM setTimeout() EventTarget.prototype.addEventListener()call()apply()bind()
const obj = {
count: 10,
doSomethingLater() {
setTimeout(function () {
// window
this.count++;
console.log(this.count);
}, 300);
},
};
obj.doSomethingLater(); // "NaN" "count" window
this
const obj = {
count: 10,
doSomethingLater() {
// "this" "obj"
setTimeout(() => {
//
// setTimeout
// "obj"
this.count++;
console.log(this.count);
}, 300);
},
};
obj.doSomethingLater(); // logs 11
| ECMAScript 2027 LanguageSpecification # sec-arrow-function-definitions |
functionfunction | Web Proxy Viewer | New URL | Original Page |