www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17

Thread: Accessing things in a self executing function

  1. #16
    Join Date
    Jan 2014
    Posts
    28
    Quote Originally Posted by tech_soul8 View Post
    No problem man


    Sometimes I have a lot of 'free' time at work so I'm bored

    There's also one interesting feature and it's called hoisting.

    Example:

    Code:
    var scope = 'global'
    
    function checkScope() {
    console.log(scope)
    
    var scope = 'local'
    console.log(scope)
    }
    
    checkScope()
    Can you explain this?
    Yeah I believe I understand that one, The first console.log is undefined because the variable is not global but the second console.log says "local" because your logging it inside of the function where the variable exists right?

    And I'll have to look up JSON vars \\.\, I have yet to learn JSON I'm only in javascript fundamentals.

  2. #17
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Hm...close but not exactly like that.

    All variables inside of a function are visible throughout the body of the function. This is known as hoisting! All variables (but not associated assignments) are "hoisted" to the top of the function. That means that if you try to reference your variable prior to the point in a code where the actual assignment happens you'll get a value of undefined because that's the value of uninitialized variables.

    The same thing applies in a case of a function expressions and a function declaration statements but you'll get to that in your future lectures I guess.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles