3

I am wondering if I need public method, I need to use this

var TestClass = function() {

    this.pub = function() {
              blahblah;
        };

If need private method(inner method), I need to use

var TestClass = function() {

    var pri = function() {
              blahblah;
        };

Is this true?

2
  • 1
    For your private function, I'd prefer function pri() { blah; } instead of using a var declaration. Commented May 31, 2012 at 17:02
  • 2
    That "private" concept really isn't the same as in other languages. It's basically futile to try and map the semantics of C++, Java, C#, etc. inheritance onto what JavaScript provides. They're fundamentally different. Commented May 31, 2012 at 17:03

2 Answers 2

5

No, this is not true. The inner function (pri) is a function, not a method. Although the difference is negligible in javascript (since every function can be used as a method and vice versa), you still can't call it as this.pri(), which would be possible with true private methods.

As a side note, despite its Java-alike syntax, Javascript, especially its object model, is significantly different from Java/C++/C#. In particular, such concepts as class and encapsulation don't exist in Javascript.

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

Comments

0

I normally use this pattern, that I haven't seen many do. I do this to avoid having to order my methods in any special way. If all is public, then one normally has to ensure that the methods called, are declared before the method call

var person = new Person("Mo", "Yo");
person.getFullname();
person.getFirstname();
person.getLastname();           

function Person(firstname, lastname) {
    var firstname, lastname;

    (function constructor(){
        setFirstname(firstname);
        setLastname(lastname)
    })();

    this.getFullname = getFullname;   // Makes getFullName() public 
    function getFullname() {
        // Will allow you to order method in whatever order you want. 
        // If we where to have it as just this.getFullname = function () {...} and same for firstname 
        // as it is normally done, then this.getFirstname would have to be placed before this method. 
        // A common pain in the ass, that you cannot order methods as you want!    
        return getFirstname() + ", " + getLastname();   
    }               

    this.getFirstname = getFirstname;
    function getFirstname() {
        return firstname;
    }

    function setFirstname(name){
        firstname = name;
    }

    this.getLastname = getLastname;
    function getLastname() {
        return lastname;
    }
    function setLastname(name) {
        lastname = name;
    }    
}

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.