Saturday 15 September 2012

javascript - Variables in function expressions -


Over the past few days, I do a lot about function expression and lots of announcements in Javascript. Most of the time it was said that function expression is the preferred way of creating functions in Javascript. Sometimes this is the opposite, so I'm not sure, but now I want to be with the expression.

But there is one thing about expression which I could not find as an example. It may be a stupid question, but I would really like to know that we have a function expression like

  var nav = {init: function () {// code}, open : Function () {// code}, close: function () {// code}};   

In that case it should be able to open and close it as an example, it is a drop down navigation or a function for some things. Now I'm asking you, how do I define variables in that code, so I can use them both in "open", "off" and "init" functions? In that particular case, I probably need the variable:

  var $ nav = $ (". Nav"), navHeight = $ nav.height (), navTest = "test";   

How would you usually do this? Maybe these questions were answered somewhere else, but I could not really find anything, probably because I discovered the wrong keywords.

If someone can explain to me / I can send me a link on a tutorial - Javascript function for beginner :)

thanks :)

You can put shared variables in the closure, which makes them private for your object's methods. There is a general technique.

You may also be interested.

No comments:

Post a Comment