-1

So i have this code:

function Class1() {
    this.i = 1;
    var that=this;

    function nn() {
        return 21;
    }
    this.aa = function() {
        nn();
    };
    this.bb = function() {
        this.aa();
    };
    this.cc = function() {
        this.bb();
    };
}
var o = new Class1();
var b=o.cc();
alert(b);   //undefined 

But when the alert is fired, I get an undefined error and not 21, Does the private method can not use a return? Thanks!

1
  • 1
    Because only Class1.nn() returns anything. Commented Mar 21, 2017 at 15:39

3 Answers 3

2

When using the function() {} syntax to define a function, you always explicitly need to return the value, i.e. not only from nn, but from all intermediate functions as well.

function Class1() {
  this.i = 1;
  var that = this;

  function nn() {
    return 21;
  }
  this.aa = function() {
    return nn();
  }
  this.bb = function() {
    return this.aa();
  }
  this.cc = function() {
    return this.bb();
  }
}
var o = new Class1();
var b = o.cc();
alert(b); // "21"

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

Comments

0

Apart from the answer above, the 'this' context seems weird in your functions. Maybe you are better of with arrow functions if you dont want to bind the this context to each function. I also think that it is better to actually separate private and public functions when using a 'class' like this.

function Class1() {
  var _nn = function () {
    return 21;
  }
  var _aa = function () {
    return _nn();
  }
  var _bb = function () {
    return _aa();
  }
  var cc = function () {
    return _bb();
  };

  return {
    cc
  };
}
var o = new Class1();
var a = o.cc();
console.log(a);

Much easier to understand that it is only cc that is a public function.

Comments

0

So with arrow function it would instead look like this, and you can use the Class1 this context inside of your private functions without doing var that = this; or using bind.

function Class1() {
  this.privateThing = 'private';

  var _nn = ()  => { return this.privateThing; };
  var _aa = () => { return _nn(); };
  var _bb = () => { return _aa(); };
  var cc = () => { return _bb(); };

  return {
    cc
  };
}

2 Comments

Note that this is semantically different than the code in the question, as aa, bb etc. are no longer accessible from the outside. Also, you can omit the explicit return for arrow functions: var _nn = () => this.privateThing
This was just an extension of my first answer. The 'this context in the code in the question was weird. So I just wanted to point out how one normally would separate private function from public and to use arrow function instead. Nothing else, but thanks anyway.

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.