Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my JavaScript function sometimes "not defined"?

Tags:

javascript

I call my JavaScript function. Why do I sometimes get the error 'myFunction is not defined' when it is defined?

For example. I'll occasionally get 'copyArray is not defined' even in this example:

function copyArray( pa ) {
    var la = [];
    for (var i=0; i < pa.length; i++)
        la.push( pa[i] );
    return la;
}

Function.prototype.bind = function( po ) {
    var __method = this;
    var __args = [];

    // Sometimes errors -- in practice I inline the function as a workaround.
    __args = copyArray( arguments );

    return function() {
        /* bind logic omitted for brevity */
    }
}

As you can see, copyArray is defined right there, so this can't be about the order in which script files load.

I've been getting this in situations that are harder to work around, where the calling function is located in another file that should be loaded after the called function. But this was the simplest case I could present, and appears to be the same problem.

It doesn't happen 100% of the time, so I do suspect some kind of load-timing-related problem. But I have no idea what.

@Hojou: That's part of the problem. The function in which I'm now getting this error is itself my addLoadEvent, which is basically a standard version of the common library function.

@James: I understand that, and there is no syntax error in the function. When that is the case, the syntax error is reported as well. In this case, I am getting only the 'not defined' error.

@David: The script in this case resides in an external file that is referenced using the normal <script src="file.js"></script> method in the page's head section.

@Douglas: Interesting idea, but if this were the case, how could we ever call a user-defined function with confidence? In any event, I tried this and it didn't work.

@sk: This technique has been tested across browsers and is basically copied from the Prototype library.

like image 264
harpo Avatar asked Sep 30 '08 16:09

harpo


People also ask

Why is my variable not defined JavaScript?

This JavaScript exception variable is not defined occurs if there is a non-existent variable that is referenced somewhere. Cause of Error: There is a non-existent variable that is referenced somewhere in the script. That variable has to be declared, or make sure the variable is available in the current script or scope.

What does it mean when a function is not defined?

A function is said to be "undefined" at points outside of its domain – for example, the real-valued function. is undefined for negative. (i.e., it assigns no value to negative arguments). In algebra, some arithmetic operations may not assign a meaning to certain values of its operands (e.g., division by zero).

What does it mean when something is not defined in coding?

It usually means that the 'thing' you're requesting does not exist(or atleast can't be found by the function requesting it). This can be a variable, object or a function.


2 Answers

I had this function not being recognized as defined in latest Firefox for Linux, though Chromium was dealing fine with it.

What happened in my case was that I had a former SCRIPT block, before the block that defined the function with problem, stated in the following way:

<SCRIPT src="mycode.js"/>

(That is, without the closing tag.)

I had to redeclare this block in the following way.

<SCRIPT src="mycode.js"></SCRIPT>

And then what followed worked fine... weird huh?

like image 165
Niloct Avatar answered Oct 13 '22 00:10

Niloct


It shouldn't be possible for this to happen if you're just including the scripts on the page.

The "copyArray" function should always be available when the JavaScript code starts executing no matter if it is declared before or after it -- unless you're loading the JavaScript files in dynamically with a dependency library. There are all sorts of problems with timing if that's the case.

like image 9
David McLaughlin Avatar answered Oct 13 '22 01:10

David McLaughlin