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.

classのprivate functionの呼び出し

Posted at

classのprivate functionを呼びたい。

まずクラスFooを定義する。

class Foo {
    func1() {
        console.log('func1が呼ばれました');
        this.#func2();
    }
    #func2(){
        console.log('func2が呼ばれました');
    }
}

で呼び出し処理。これで呼べる。

call.js
$(function(){
let obj = new Foo();
obj.func1();
});

次にjqeuryのblur内で呼び出す。これは手こずった。

index.html
    <p>名前:<input type="text" id="text" size="40"></p>

テキストボックスのフォーカスが外れた時。

call.js
let obj = new Foo();
$('#text').blur(obj.func1.bind(obj));

このケースではthis.#func2();呼び出し時にthisがFooオブジェクトを指していないので、thisがFooオブジェクトを指すようにbindしてあげる。

昨日勉強したbindが早速役に立った。

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?