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

zunda

鳥にも書いたけどわからんjs

$ cat this.js
obj = {
is: "obj",
f: function(){return this.is;},
a: () => {return this.is;}
}

this.is = "top";
console.log("f: " + obj.f());
console.log("a: " + obj.a());
$ node this.js
f: obj
a: top

@zundan JSのラムダ式はラムダ式を生成したコンテキストのthisが保持されるのでそうなる (トップレベルのthisはglobalThis)

@rinsuki ラムダ式の場合は定義時のthis (soto)じゃなくて実行時のthis (naka)のような気もするです… (ぜんぜんわからん)

$ cat this.js
soto = {
is: "soto",
naka: {
is: "naka",
f: function(){return this.is;},
a: () => {return this.is;}
}
}

this.is = "top";
console.log("f: " + soto.naka.f());
console.log("a: " + soto.naka.a());
$ node this.js
f: naka
a: top

@zundan 定義時のthisはsotoではないんですよ

~/playground $ cat aaa.js
this.is = "top";

soto = {
is: "soto",
naka: {
is: "naka",
b: console.log(this.is),
f: function(){return this.is;},
a: () => {return this.is;}
}
}

console.log("f: " + soto.naka.f());
console.log("a: " + soto.naka.a());
~/playground $ node aaa.js
top
f: naka
a: top