0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

bindとthis

Last updated at Posted at 2021-09-26

bindとthisに関して勉強した自分の備忘禄。

関数内関数で外側にあるidを内側にあるthisで使いたい時に
bindしたいが、その書き方にちょっと詰まって書いたのがこれ。

show.bind(this)()で出来ました。

let Outer = {
    id: 'mk1000',
    show: function(){
        console.log(this.id);
        let show = function(){
            console.log(this.id);
        }
        show.bind(this)();
    }
}
Outer.show();

アロー関数でも同じことができるのか...

let Outer = {
    id: 'mk1000',
    show: function(){
        console.log(this.id);
        let show = () => {
            console.log(this.id);
        }
        show();
        //show.bind(this)();
    }
}
Outer.show();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?