javascript - How do I use 'this' when converting code into an object? -


So I can reuse the code on more than one place on a web page, I want it to be in Object Oriented JavaScript I am converting. The original code looks like this:

  foo = 42; Function bar (a, b) {foo = a * foo + b; } ... bar (1,1)   

I think I want to see it like this:

  function example () {this. Foo = 42; This.bar = function () {this.foo = a * this.foo + b; }} Var = a new instance (); Var two = new instance (); ... One. Bar (1,1); Two.bar (2,3);   

However, I'm not sure that I am using 'this' correctly within the nested function. I have noticed that some of the functions I'm going to convert are event handlers which already mention 'this' in my body. How can I differentiate this 'event' already in this 'event' , I want to use to reach my local variable?

Create a local var in the example class / function and then click the appropriate < Code> this object.

  function example () {var self = this; This.foo = 42; This.bar = function () {self.foo = a * self.foo + b; }}    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -