-1

I'm trying to call a function nested within a prototype method. I get undefined when I try to reference this nested function. example -

testObj.prototype.funcA = function() {
    var that = this;
    that.funcB.subFuncA(); //call nested function within funcB - undefined
}

testObj.prototype.funcB = function() {
    var subfuncA = function() {
        //call this function from funcA
    }
}

Thanks for any help provided.

Bookamp
  • 672
  • 8
  • 19
  • 1
    Yeah... not possible. As simple as that. – deceze Aug 28 '14 at 14:16
  • 1
    `subfuncA` is local to `funcB`, and only exists at all when `funcB()` is called. – Bergi Aug 28 '14 at 14:19
  • -1? Why? Okay, what the OP wants isn't possible because of the scope issue, but how will anyone learn anything if questions are downvoted based only on their technical level? – Bob Sammers Aug 28 '14 at 14:25

1 Answers1

1

subfuncA is locally scoped. It is not exposed outside funcB at all. This is (one way, sort of) how you create private functions in JS.

If you want subfuncA to be public, then stick it somewhere public.

testObj.prototype.funcA = function() {
    var that = this;
    that.funcB.subFuncA(); //call nested function within funcB - undefined
}

testObj.prototype.funcB = function() {
}

testObj.prototype.funcB.subfuncA = function () {

};
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I don't think you really want to do that, [`this` doesn't work any more then](http://stackoverflow.com/a/15884332/1048572) – Bergi Aug 28 '14 at 14:21
  • 1
    @Bergi — There isn't enough information about what `subfuncA` is supposed to do to know if `this` will be significant or not. – Quentin Aug 28 '14 at 14:22
  • That's true. Since in the original code it wasn't a method either, this is probably fine, though as it doesn't seem to be an instance method at all it might be better to put it as a static helper function `TestObj.funcA`. – Bergi Aug 28 '14 at 14:31