tags:

views:

110

answers:

6

// test.js //

    var testObj = {};
    testObj.init = function(){
        console.log('google');
    }

var onload = testObj.init;

/// what does it mean, does it mean it gets executed when script loaded or what, I just can't understand it as it is not looging into console anything under Google Chrome plugin...

A: 

Nothing is logged because you are simply setting onload to be a pointer to the function testObj.init which only gets the function's code. To actually run it, you must call testObj.init().



More about onload…

onload is a property of an HTML element that can be set to run javascript. For example:

<html>…
… <body onload="testObj.init()"> …
…</html>

This means that when the "body" element is loaded, the function testObj.init() is run.


The "onload" property can also be attatched by javascript, as in:

window.onload=myFunction();
pop850
ok what does the other one mean ? just a var?
simple
yep — "onload" is a standalone variable in your example.
pop850
You're wrong onload is just a variable.
eskimoblood
Everything you said is true, but you didn't answer the OP's question.
musicfreak
Thanks for pointing that out musicfreak, lost my train of thought for a moment there… hehe
pop850
A: 

It just means that your variable onload now points to

function(){
    console.log('google');
}

onload is just the name of a local variable here.

Hugo Migneron
A: 

In your code, onload is simply the name of a local variable. The var keyword declares local variables. You're setting the value of onload to testObj.init, which is a function that prints 'google' to the console.

To make it run the function on page load, set window.onload to the value of the function.

window.onload = testObj.init;

Or, better yet, use event handlers to attach an "onload" event to the window object. (To make this easier, use a JavaScript library such as jQuery, but I recommend you first learn how it all works.)

musicfreak
A: 

It means that variable onload is a reference to the function testObj.init. onload() will execute the function and output 'google' to the console.

eskimoblood
A: 

No, it only means that you assign it to a variable named onload.

Depending on the scope of the code it might actually work, if the variable name collides with the onload property of the window object. In that case a variable would not be created, but it would use the existing property instead. You should not rely on this behaviour though, you should always specify it as a property of the object:

window.onload = testObj.init;
Guffa
thanks, it is just a oxjs library that I am stuck with, there is not really much of a documentation , and example is just not working....=(
simple
That would only work without the `var` keyword. `var` declares a local variable. If the code was simply `onload = testObj.init` then it may work.
musicfreak
indeed it is with a var, here is a code part "var onload = DemoApp.JSJaC.init; "I just was confused, anyhow thanks a lot to ya'll
simple
@musicfreak: It may be tempting to use `onload = fn;` but should be [avoided](http://perfectionkills.com/onloadfunction-considered-harmful/)...
CMS
@CMS: I know, I wasn't saying that you should, I was just disproving @Guffa's statement.
musicfreak
+2  A: 

Think of it like giving your dog 2 names:

var spot = new Dog();
var comeHereSpot = function ()  { return spot; }    
var comeHereBoy = comeHereSpot;

Whether you call comeHereSpot or comeHereBoy the same dog will come running.

ChaosPandion