0

I am new to Node.js and I am trying to run this simple program:

var AwesomeClass = function() {
    this.awesomeProp = 'awesome!'
    this.awesomeFunc = function(text) {
        console.log(text + ' is awesome!')
    }
}
var awesomeObject = new AwesomeClass()
function middleware(func) {
    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)
    oldFunc("John");
}
middleware(awesomeObject)
anotherMiddleware(awesomeObject)
caller(awesomeObject)

The above code runs really fine.. But what I dont understand is oldFunc is a local variable of middleware function but still I am able to call it from another function i.e. caller function............. HOW!!

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

1 Answers1

1

I believe the lack of the var keyword means oldFunc is attached to the global namespace.

If you change

oldFunc = func.awesomeFunc

to

var oldFunc = func.awesomeFunc

I think you will lose access.

You might find this war story of interest.

Update in response to your question in the comment

You asked, given the following code, why func.awesomeFunc still has access to the local var oldFunc from its enclosing function.

function middleware(func) {
    var oldFunc = func.awesomeFunc
    func.awesomeFunc = function(text) {
        text = text + ' really' oldFunc(text)
    }
}
function caller(input) {
    input.awesomeFunc(input.anotherProp)
}

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. I also recommend Douglas Crockford's Javscript: The Good Parts for coverage of many of Javascript's feature nuances.

barry-johnson
  • 3,204
  • 1
  • 17
  • 19
  • that was helpful.. thanks a lot.. but there still one small doubt that I have. If you change function middleware(func) { var oldFunc = func.awesomeFunc func.awesomeFunc = function(text) { text = text + ' really' oldFunc(text) } } function caller(input) { input.awesomeFunc(input.anotherProp) } How come this works fine, since again oldFunc is a local variable in middleware and when awesomeobject is referred from caller function the oldFunc is called easily – Nick Div Mar 13 '14 at 18:02
  • You are welcome. See my edit above regarding your follow-up question. Also [this question](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) covers closures pretty well. – barry-johnson Mar 13 '14 at 19:16