When to use this in javascript OO?

前端 未结 2 1734
失恋的感觉
失恋的感觉 2021-02-15 22:16

In Javascript OO, when should I use the this keyword?

Also, if I want to call a method of a class from another method of the same class, should I use

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-15 22:48

    When it comes to "object-oriented" JavaScript, here's a nice guide Mark Dickinson here on SO linked to: Private Members in JavaScript. It does go into detail about some other stuff you don't really need now, but once you understand how JavaScript works, you'll see that it's quite different from your ordinary object-oriented language when it comes to things like what this really means.

    I'd say that in your case, you should definitely use this, but maybe your functions should be in the prototype part of your "class" (this avoids redefining the function every time a new instance is created.)

提交回复
热议问题