0

I hava a simple js function

var myFunction=function(){
    var age=20;
    this.name="bar";
    var show=function(){
        console.log(age);
    }
}

and creating an instance like:

 var obj=new myFunction();
 console.log(obj.show())

logs:this

how can I resolve it?? I have tried to search SO for this but could not find my solution. UPDATE I want all private members to remain same.

6
  • 1
    show is not a member of obj. It's a local variable of myFunction. Commented Jan 25, 2017 at 18:05
  • 1
    make var show into this.show so it is accessible on the instance of myFunction Commented Jan 25, 2017 at 18:06
  • Why are you doing console.log(obj.show())? Doesn't show() already call console.log()? Commented Jan 25, 2017 at 18:07
  • 3
    @Anonymous: Then you can't call out outside of the function. Commented Jan 25, 2017 at 18:07
  • 1
    I think the point is to make that function private i.e. not accessible outside the object itself. If so, to access it you'd need to do so within a method on that object. Commented Jan 25, 2017 at 18:08

4 Answers 4

3

The problem with your code is that the show method is not accessible outside the function scope of myFunction.

In Javascript, the scope is defined within the function (Ignoring ES6 block scope). So, the scope of variables age, name, show is within the function. However, when you use this and use the constructor function (new) the newly created object contains the property which are associated with the this keyword. In your case, you associate only the name property with the new object and did not associate the show function with this.

One of the solution is using the Module Pattern. You can make the public accessible property by returning an Object which encapsulates the publicly exposed properties. So, in your case, you can expose name and show methods from the myFunction. Also, no need to do console.log twice. You can get the output by obj.show(). Otherwise it will log undefined as show function doesn't return anything.

var myFunction=function(){
    var age=20;
    var name="bar";
    var show=function(){
        console.log(age);
    }
    
    return {
      name : name,
      show : show,
    }
}

 var obj=new myFunction();
 obj.show()

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

Comments

0

Try changing your code to

var myFunction=function(){
var age=20;
this.name="bar";
this.show=function(){
console.log(age);
}
}

Changing your show var to be a property

Comments

0

A clean solution would be to make the function publicly accessible as an object property:

var myFunction = function() {
  var age = 20;
  this.name = "bar";
  
  this.show = function() {
    console.log(age);
  }
}

var obj = new myFunction();
obj.show();

Comments

0

With var you declared show as a variable and not a member of myFunction. The same with age, if it is a class member, you should also use this

This one way to change it:

var myFunction = function() {
    this.age = 20;
    this.name="bar";
    this.show = function() {
        console.log(age);
    }
}

var obj = new myFunction();
console.log(obj.name);
obj.show();
Find further information here: best approach to member variables in object-oriented javascript?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.