mastodon.zunda.ninja is one of the many independent Mastodon servers you can use to participate in the fediverse.
Zundon is a single user instance as home of @zundan as well as a test bed for changes of the code.

Administered by:

Server stats:

1
active users

JavaScriptの関数定義と呼び出しとthisをもういちどおもいださないといけにゅ

> The arrow function syntax does not have access to arguments or this.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions

うむわからん (=>以外で定義された関数はthisで実行時のレシーバを得ることができるんだっけな)

$ cat f.js
x = {
x: "x",
f: function() { console.log(this) },
a: () => { console.log(this) }
}
x.f()
x.a()

y = {
y: "y",
f: x.f,
a: x.a
}
y.f()
y.a()
$ node f.js
{ x: 'x', f: [Function: f], a: [Function: a] }
{}
{ y: 'y', f: [Function: f], a: [Function: a] }
{}

MDN Web DocsFunctions - JavaScript | MDNGenerally speaking, a function is a "subprogram" that can be called by code external (or internal, in the case of recursion) to the function. Like the program itself, a function is composed of a sequence of statements called the function body. Values can be passed to a function as parameters, and the function will return a value.

それにつけても省略形の多さよ

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_function

> () => expression
>
> param => expression
>
> (param) => expression
>
> (param1, paramN) => expression
>
> () => {
> statements
> }
>
> param => {
> statements
> }
>
> (param1, paramN) => {
> statements
> }

引数が1個の場合は()を省略できる、文が1個の場合は{}を省略できる、と思えばいいのかしら。

MDN Web DocsArrow function expressions - JavaScript | MDNAn arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate limitations in usage:

@zundan 文というとstatementに聞こえるけど、expressionが1個の場合は、ですかね。そしてオブジェクトリテラルを書きたくなるが {} が紛らわしいので怒られる…それでもreturnをサボりたいと x => ({ y: x + 1 }) みたいなことを書きがち…

zunda

@unarist ですです。expressionの日本語を思い出せなくて文と書いちゃったのですが式でしたねー。そうか、expressionの場合はreturnが省略されてるんだ。