0

Say I have a function:

function parent() {
   var address = "house";
   var getAddress = function () {
       return address;
   }
}

How can I make another function that inherits these things?

7
  • Inherits what? Local variables? You said properties and methods in your question title but your function has neither of these. You should probably be asking a different question. Commented Jul 12, 2014 at 3:28
  • FYI, this is scope nesting where a child function has access to its parent scope, not inheritance. It's not clear to me what you're asking to do. Commented Jul 12, 2014 at 3:28
  • 2
    look into javascript prototype Commented Jul 12, 2014 at 3:28
  • You probably want to have a look at developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…. You can't "inherit" local variables (whatever that is supposed to mean anyway). Commented Jul 12, 2014 at 3:33
  • I would like it to inherit the local variables and function. Commented Jul 12, 2014 at 3:58

1 Answer 1

1

how about below :

function otherFunc()
{
    addr = parent();
    //other stuff..
}

slight modification in your function to make it as per above usage:

function parent() {
   var address = "house";
   var getAddress = function () {
       return address;
   }
return address;
}
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.