0

I am looking to create public and private methods for javascript functions with return statements.

I like the idea of using return to pass back the publicly accessible methods because it easy to see all of your public properties and methods in one place.

var Base = function(){
  var method1 = function(){
    console.log("method1");
  }
  var method2 = function(){
    console.log("method2");
  }
  //private
  var method3 = function(){
    console.log("method3");
  }
  // public methods
  return {
    method1:method1,
    method2:method2
  }
}
var Child = function(){
  var method4 = function(){
    console.log("method4");
  }
  var method5 = function(){
    console.log("method5");
  }
  //private
  var method6 = function(){
    console.log("method6");
  }
  // public methods
  return {
    method4:method4,
    method5:method5

  }
}
Child.prototype = new Base();

var base = new Base();
base.method1();
base.method2();

var child = new Child();
try {
  child.method1();
} catch(e){
  console.log(e.message);
}
try {
  child.method2();
} catch(e){
  console.log(e.message);
}
child.method4();
child.method5();

I know if I do it like this I will get the public/private methods, but I was wondering if anyone knew how to do it with the return statement.

var Base = function(){
  // public methods
  this.method1 = function(){
    console.log("method1");
  };
  this.method2 = function(){
    console.log("method2");
  };
  // private methods
  var method3 = function(){
    console.log("method2");
  };
};
var Child = function(){
  // public methods
  this.method4 = function(){
    console.log("method4");
  };
  this.method5 = function(){
    console.log("method5");
  };
  // private methods
  var method6 = function(){
    console.log("method6");
  };
};
Child.prototype = new Base();

var base = new Base();
base.method1();
base.method2();

var child = new Child();
child.method1();
child.method2();
child.method4();
child.method5();
2
  • there is no such thing as a private method in javascript. it's an inherent contradiction because if it's a method, it's not private; it's a property of the object as seen by all. Commented Jan 20, 2015 at 23:18
  • 1
    Have a look at the following answer, it may help. First it'll explain why setting prototype of Child to an instance of Parent shows a lack of understanding of the role of the constructor function and the prototype and it covers private functions (shared on prototype) as well as a pattern for instance specific "protected" members stackoverflow.com/a/16063711/1641941 Commented Jan 21, 2015 at 1:10

1 Answer 1

1

No. You must not use return if you want to use prototypical inheritance, you should use this as it is the instance object that inherits from your prototype.

Of course, nothing prevents you from congregating your public interface in the end of the constructor:

function Base() {
  function method1() {
    console.log("method1");
  }
  function method2() {
    console.log("method2");
  }
  //private
  function method3() {
    console.log("method3");
  }
  // public methods
  this.method1 = method1;
  this.method2 = method2;
}

Notice that you should not use new Base for the inheritance of Child.prototype.

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

Comments

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.