0

I have a piece of code which has two nested functions inside a main one.

How do I retrieve the nested functions using this keyword? Is it possible?

I have tried times().present() and new times().present() , none of them seem to work and return undefined.

I have found similar examples on w3School but cant seem to implement it in this case.

Thanks in advance.

function times() {
  var timingObj = function() {
    this.present = currentTime;
    this.past = pastTime;
  };

  var currentTime = function() {
    var hourMin = new Date().getHours() + ":" + new Date().getMinutes();
    return hourMin;
  };

  var pastTime = function() {
    if (new Date().getDay() == 5) {
      return "07:40"
    } else {
      return "16:30"
    }
  };
  return timingObj;
}

console.log(times().present());
//console.log(new times().present());

5
  • 1
    Any reason you aren't using class for this? Commented Mar 29, 2019 at 12:08
  • @BrandonDyer am new to using objects. but I will look up class now Commented Mar 29, 2019 at 12:11
  • 1
    new (times())().present() Commented Mar 29, 2019 at 12:13
  • @adiga I knew I was close! thanks :) Commented Mar 29, 2019 at 12:19
  • 1
    You want to use the new operator on the function returned by times. Not on times itself. Commented Mar 29, 2019 at 12:22

2 Answers 2

3

function times() {
  var currentTime = function() {
    var hourMin = new Date().getHours() + ":" + new Date().getMinutes();
    return hourMin;
  };

  var pastTime = function() {
    if (new Date().getDay() == 5) {
      return "07:40"
    } else {
      return "16:30"
    }
  };
  
  return {
    present: currentTime,
    past: pastTime
  };
}

console.log(times().present())

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

Comments

1

You can use Method call().

function times() {
  var timingObj = function() {
    this.present = currentTime;
    this.past = pastTime;
  };

  var currentTime = function() {
    var hourMin = new Date().getHours() + ":" + new Date().getMinutes();
    return hourMin;
  };

  var pastTime = function() {
    if (new Date().getDay() == 5) {
      return "07:40"
    } else {
      return "16:30"
    }
  };
  return timingObj;
}

times().call(null);
console.log(present(), past());

OR define them as prototype

function times() {
  var timingObj = function() {
    this.present = timingObj.prototype.currentTime;
    this.past = timingObj.prototype.pastTime;
  };

  timingObj.prototype.currentTime = function() {
    return new Date().getHours() + ":" + new Date().getMinutes();
  };

  timingObj.prototype.pastTime = function() {
    return new Date().getDay() === 5 ? "07:40" : "16:30";
  };

  return timingObj;
}

console.log(times().prototype.currentTime(), times().prototype.pastTime());

//times().call(null);
//console.log(present(), past());

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.