On 26 August 2011 10:21, Matthias Reuter <[email protected]> wrote:
>> Is hoisting doing this:
>> var addEvent = window.addEventListener ? function ( e, s, f ) {
>>    e.addEventListener( s, f );
>> } : function ( e, s, f ) {
>>    e.attachEvent( 'on' + n, f );
>> };
>> ?
>
> No, this is feature detection.
>
> Hoisting is the reason why this works:
>
> foo(); // using a function before it is defined works because of hoisting
> function foo() {}
>
>
> because internally the browser handles this as
>
> function foo() {}
> foo();
>
> The process of putting functions first no matter where they are defined is
> called hoisting. So hoisting is nothing you do, but something the browser
> does.
>
> To avoid the confusion, Crockford recommends putting functions first in you
> script.
>

Just for completeness, hoisting also applies to variable
*declarations*. The difference between variable hoisting and function
hoisting is that with variables only the declaration (the `var x`) bit
is hoisted, not the definition (the `= 5`). So if there is a var
declaration somewhere in the function, that variable will effectively
be declared at the top of the function (its declaration is 'hoisted').
That's why Crockford recommends putting one var statement at the top
of the function.
-- 
Nick Morgan
http://skilldrick.co.uk
@skilldrick

Save our in-boxes! http://emailcharter.org

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to