0
var AwesomeClass = function() {
    this.awesomeProp = 'awesome!'
    this.awesomeFunc = function(text) {
        console.log(text + ' is awesome!')
    }
}
var awesomeObject = new AwesomeClass()
function middleware(func) {
   var oldFunc = func.awesomeFunc
    func.awesomeFunc = function(text) {
        text = text + ' really'
        oldFunc(text)

    }
}
function anotherMiddleware(func) {
    func.anotherProp = 'super duper'
}
function caller(input) {
    input.awesomeFunc(input.anotherProp)

}
middleware(awesomeObject)
anotherMiddleware(awesomeObject)
caller(awesomeObject)

In the above code you will see that the caller function is the one that actually makes the call to awesomeFunc but when we called middleware function we changed the function inside the awesomeobject and made oldFunc variable a part of the awesomeFunc. But what i do not understand is how come when the awesomeFunc is called from inside caller function the awesomeFunc executes properly? As awesomeFunc has oldFunc mentioned inside it now which is local variable of the function middleware so the caller function should not be able to recognize it.

Nick Div
  • 5,338
  • 12
  • 65
  • 127

1 Answers1

1

I also updated my answer to your original question with this content, but am putting this here as well.

The reason is that it is available via closure, which to simplify, makes the variables of the enclosing function scope available to the enclosed scope. Incredibly powerful feature, also easy to have some unintended behavior crop up - inner function closures mistakenly accessing an outer looping variable (when they really just wanted to access the value of the loop counter at a specific point in time) are a very common source of headache for some. You can find some very good descriptions of closures on Stack Overflow. This question covers closures pretty well. I also recommend Douglas Crockford's Javscript: The Good Parts for coverage of many of Javascript's feature nuances.

Community
  • 1
  • 1
barry-johnson
  • 3,204
  • 1
  • 17
  • 19
  • I feel like i am reading some other language totally.. lol but thanks for the help, appreciate it. – Nick Div Mar 13 '14 at 20:09
  • I see in your profile a lot of Java tags - that's more my background as well, so I am very sympathetic. :-) It takes some getting used to, but Javascript is actually pretty slick once you forget everything else you've ever learned. LOL – barry-johnson Mar 13 '14 at 20:23
  • Thank you.. The link that you posted in your other answer has a link in it called "javascript closure for dummies", that has explained closures pretty well. And I hope I dont encountner anymore surprises while learning Node.js – Nick Div Mar 13 '14 at 20:35
  • The "everything is async" is probably the only other big thing that takes some getting used to with Node (along with getting used to reading some of the common idiomatic usage of the language when looking at others code). The other general Javascript thing is its functional nature, the handiness of which goes a long way toward making up for the more irritating idiosyncrasies. Prototype versus class based inheritance means you may want to think through inheritance strategies differently as well. If you have a reasonable programming background, the Crockford book is excellent. – barry-johnson Mar 13 '14 at 20:43
  • isnt Crokford for the JavaScript? I have fair knowledge of JavaScript, its the Node.js that is bugging me. The event based coding on server etc and few more new concepts. – Nick Div Mar 14 '14 at 05:05