PDA

View Full Version : About renderer function declaration



mx_starter
23 Jan 2015, 5:11 AM
Hello gyus,
can you please explain the difference in the following 3 cases:
(a grid column definition together with a renderer function definition)


1. This works:


...
{ dataIndex: 'firstName', renderer: foo}
...

function foo() {...} //renderer declared AFTER the column config


2. This also works:


var foo = function() {...} //renderer declared BEFORE the column config
...
{ dataIndex: 'firstName', renderer: foo}



3. This does not (and no console errors):


...
{ dataIndex: 'firstName', renderer: foo}
...
var foo = function() {...} //renderer declared AFTER the column config



So - why in the thord case the renderer is not executed?

mx_starter
23 Jan 2015, 5:39 AM
In fact - this is a normal behaviour as described here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#Calling_functions

However, i'm not sure i understood why?

skirtle
24 Jan 2015, 8:03 AM
You need to research...

The differences between function expressions and function statements.
Function statement hoisting.
Variable hoisting.


Your first example is a function statement, so it'll be hoisted.

Your second example uses a function expression so it won't be hoisted but as it comes first that makes no difference.

In your third example the variable foo will be undefined but it will exist (thanks to variable hoisting). Only the var part is hoisted, not the assignment part. As it exists there's no error.