1

Given the following Javascript example:

var obj = function() {
    var _c = function() {
        console.log("c")
    }
    return {
        a: function() {
            b();
        },
        b: function() {
             console.log("b");
        },
        c: function() {
            _c();
        }
    }
}();

calling obj.a() gives an error that b is not defined. This error can be solved by changing b() to this.b(). Can anyone help explain why this is necessary while c can access _c?

2
  • Because b is a property, not a variable. Commented Mar 22, 2016 at 2:15
  • There is no function b(), there is just {b : function() {}} which is completely different, it's a property of an object Commented Mar 22, 2016 at 2:15

3 Answers 3

2

_c is a local variable that is in scope when _c() is executed. b is not; it is only found on the object itself, so it can be found by this.b.

This confusion is indicative of transplanting object-oriented concepts from other languages to JavaScript. There are no private and public members in JS, and you can't call methods on the same object by leaving the object prefix. There's just local variables (v when var v or let v), global variables (v when no var v or let v) and object properties (obj.prop). A method is object property that contains a function; no more, no less.

Sign up to request clarification or add additional context in comments.

1 Comment

Good explanation. It's worth mentioning that a call to a bare b() would execute if there were a global function b (albeit not calling the function that OP would expect). Also, I think you meant that _c is "a local variable that is in scope when c() [not _c()] is defined" [not executed]; this is actually a closure, which would be another good JS concept for OP to understand..
0

Need to use this.b because you are returning an object and 'b' in this context is the property of that object. you need to explicitly stated that using this

While _c is a local function defined inside the closure, you can access access it directly in the return object and does not refer to the return object.

Comments

-1

You should not call b() directly.

var obj = function() {
  var _c = function() {
      console.log("c")
  }
  return {
      b: function() {
           console.log("b");
      },
      a: function() {
          this.b();
      },
      c: function() {
          _c();
      }
  }
}();
obj.a();

Explanation added :
You are calling b() inside a function where b() is defined on the object. So to call the function b() inside any other function, you should call b with reference to the object like

this.b();

2 Comments

This does not answer the question "Can anyone help explain why this is necessary while c can access _c?"
@MobyDisk sorry my bad, forgot to add the explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.