Hurricane Development Hurricane Development - 11 months ago 42
Javascript Question

UncaughtType error only in anonymous function

I have the following code to animate a div. It works perfectly:

function fadeAndSlideIn(el,m,t) { = 0; = 0;

var oi = 1 / (t * 60);
var mi = m / (t * 60);

var tick = function() { = + oi; = (parseInt( + mi) + 'px';

if ( < 1)
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16);


(function() {
setTimeout(function() {
setTimeout(function() {

However when I change the
function to an anonymous function like this:

var fadeAndSlideIn = function(el,m,t) {

my program does not work and I get the following error

Uncaught TypeError: Cannot set property 'opacity' of undefined

Why does the function decleration change the performance? All the code is in a
> tag at the end of the body.

Answer Source

JS has got automatic semicolon insertion. This is not clever because sometimes the engine fails to do so and youre producing errors. So always use semicolons:

var a=function(){} //bad
var a=function(){}; //good

In your code the engine fails because as Felix Kling said, it thinks its an IIFE:

var a=function(){}
 (some other code);

So actually it passes your IIFE to the fadeInSlide as el, and of your function is undefined...