By AngryHacker


2018-03-13 15:24:42 8 Comments

Consider the following chunk of code:

function Bar() {
    var _self = this;

    this.addPin = function (param1) {
        console.log('addPin');
    }

    function endPin(param1) {
        console.log('endPin');
    }

    _self.addPin(1);
    this.addPin(1);
    addPin(1);        // failse

    endPin(2);
    this.endPin(2);   // fails
}

var foo = new Bar();
foo.addPin(1);  // works
foo.endPin(1);  // does not work

From what I understand from playing with this code, the function declaration of endPin effectively makes it a private method to any outside callers. While this.methodHere type of declaration makes it a public method.

What I am not clear on is the usage of this in the class itself. this refer to the instance of the class. Why, in that case, can't I use this.endPin inside the class. Conversely, why do I have to use this.methodHere syntax when referring to addPin (which was declared using this.methodHere syntax).

Finally the difference between this and _self - I've read that _self preserves the original state of the class (or this). How does it do that if it's a reference supposedly pointing to the same memory location? How is this different from _self in this particular example?

2 comments

@Andrea Magazzini 2018-03-13 15:34:18

You can't use the this.endPin because the function is not defined in the class itself but inside a function.

If you write:

function Bar() {
    // do whatever
    this.endPin(2); // now it should work
}

function endPin(param1) {
    console.log('endPin');
}

@Jonathan Lonowski 2018-03-13 15:38:37

This will only work when endPin is defined in the global scope, Bar() is called without new, and strict mode isn't used. Barring those conditions, this will default to the global object, permitting this. But, that's perhaps a rather fragile solution.

@Andrea Magazzini 2018-03-14 08:30:36

Yes I know, but he was asking so many questions that I was just trying to solve what I assumed was his main problem.

@Keith 2018-03-13 15:32:23

You have asked a lots of questions here, so lets try and break them down.

this refer to the instance of the class. Why, in that case, can't I use this.endPin inside the class.

Because endPin is not part of the class, it a local function inside your constructor.

Conversely, why do I have to use this.methodHere syntax when referring to addPin (which was declared using this.methodHere syntax).

Because addPin is part of the class, basically the reverse of the first question. Doing func() does not automatically call the function of class.

How is this different from _self in this particular example?

In this case it's not, but if you used _self inside your local function, it would capture scope so it's still pointing to this of the class, instead of the functions scope.

Related Questions

Sponsored Content

58 Answered Questions

[SOLVED] How can I merge properties of two JavaScript objects dynamically?

15 Answered Questions

[SOLVED] "Thinking in AngularJS" if I have a jQuery background?

31 Answered Questions

[SOLVED] How to execute a JavaScript function when I have its name as a string

  • 2008-12-11 15:47:10
  • Kieron
  • 432263 View
  • 886 Score
  • 31 Answer
  • Tags:   javascript

23 Answered Questions

[SOLVED] Event binding on dynamically created elements?

9 Answered Questions

[SOLVED] How to implement private method in ES6 class with Traceur

11 Answered Questions

[SOLVED] Why don't self-closing script tags work?

2 Answered Questions

[SOLVED] Understanding public/private instance variables

  • 2015-08-05 12:38:13
  • Tony54
  • 75 View
  • 1 Score
  • 2 Answer
  • Tags:   javascript

1 Answered Questions

[SOLVED] Private members in the revealing module pattern

1 Answered Questions

What's the difference b/w old and new javascript function syntax?

  • 2012-07-23 14:53:29
  • ychaouche
  • 145 View
  • 0 Score
  • 1 Answer
  • Tags:   javascript

Sponsored Content