I'm working on an assignment in which there are 2 functions, each of which are supposed to take document as a parameter. I kept getting 'document is undefined' as an error in the console, so I took out 'document' as a parameter altogether from the two functions, and not only did the error go away, but my overall program is working.
However, now there is an autograder for the assignment that is telling me 'document is not defined' in my two functions. The first line of each function is a variable declaration like the below.
let faultyItems = document.getElementById("faultyItems");
Do I need to define document before calling it? And do I most likely need to put it back in as a parameter? Any advice would be greatly appreciated as I'm new to the DOM - thank you.
I have a utility function that I use 93 times, let's call it function add(a,b){return a + b}
I am getting a TypeError: a is undefined in the console at the moment.
Usually when something similar happens, Firefox provides a trace of the line numbers of the code that calls add below the error message, allowing me to get back to the cause. However, this time there is no further information.
I've considered adding a break point in the add function, however because it gets called so much, it is impractical.
How should one trace the source of a TypeError in a heavily used javascript function?
Edit:
As pointed out by Jonas Wilms, the simplifed example will not produce a TypeError. Consider this instead:
function add(a,b){
if (typeof a != 'string'){
throw Error('my TypeError')
}
return a + b
}
Edit:
I solved this by doing ctrl-F on add( and stepping through each call until I spotted where undefined inputs were. I still have a feeling there's a better way! Or perhaps a way to arrange code so that this doesn't happen.
I've declared two methods.
String.prototype.hazaa = function (shazoo) {
return this + shazoo;
}
Number.prototype.hazaa = function (shazoo) {
return this + shazoo;
}
When I call the former, I get the expected behavior. However, invoking the second one, produces the error below.
Syntax error: Unexpected token ILLEGAL(...)
I have the feeling that it's my C#-ishness that is spooking (I'm thinking extension methods and object oriented calls). The invocation's performed as follows.
"abc".hazaa("shazoo");
12345.hazaa(00000000);
Is there another syntax to invoke the function I've added? Have I not declared the prototype addition the right way?
Yes, I have made the research but I might be missing a relevant point.
The issue is while it is parsing 12345.hazaa(00000000);, it sees hazaa as coming after the decimal point in the number, hence the unexpected token. If you wrap the number in parentheses it is parsed and executed correctly:
(12345).hazaa(00000000);
It will continue to work normally on variables, as the parsing has already happened:
var a = 123;
a.hazaa(0000);
As mentioned by Jaromanda X in the comments, another alternative to allow correct parsing is to use a double-dot syntax:
12345..hazaa(00000000);
I am getting an error when I run the following command in an included script. But if I run the command from the google chrome console, it works properly.
var a = {};
console.log(keys(a));
Error:
Uncaught ReferenceError: keys is not defined
What's going on here? How can I use the keys function in an included script?
console.log(keys(a))
keys() is not function provided by the browser for use in your code. You probably want Object.keys()
a = {};
console.log(Object.keys(a));
Sometimes the console has extra functions exposed to it for ease of use debugging that aren't available in your actual code. keys() sounds like one, and copy('some text') is another.
I'm failing to find a link which lists them, sadly. But I'm quite sure there are more than those 2 functions.
Whenever you get an error like this, try to search for a definition of the function/variable that's been reported as undefined. If it is defined, try looking for a reason this might not be working. Did you know that the keys function is apart of the Object constructor? You can't call it as if it's a free-standing function. Though if you get into the habit of doing this, try making your own function to allow this:
function key( object ) {
return Object.keys( object );
}
Your code should pass given a definition like this.
I just got the following error in a piece of javascript (in Firefox 3.5, with Firebug running)
cannot access optimized closure
I know, superficially, what caused the error. I had a line
options.length()
instead of
options.length
Fixing this bug, made the message go away. But I'm curious. What does this mean? What is an optimized closure? Is optimizing an enclosure something that the javascript interpretter does automatically? What does it do?
I had this issue too when Firebug is running.
It seems to happen sometimes, when an exception is raised (for whatever reason) and when there's a recursive function call somewhere in the call stack. The exception gets re-raised as the mysterious "InternalError: cannot access optimized closure"
Changing the way I define the recursive function, seems to make this issue go away. eg changing from
function foo(bar) {... foo(recursively); ...}
to
var foo = function(bar) {... foo(recursively); ...}
Hope that helps.
It is a bug in Firefox happening with Firebug open:
https://bugzilla.mozilla.org/show_bug.cgi?id=505001
[An earlier answer mentioned this was due to this other bug, which I think is incorrect as that other problem was not related to Firebug.]
Seems like a Firefox bug:
https://bugzilla.mozilla.org/show_bug.cgi?id=496790
A closure is a function with context. If you dynamically create a new function, then you create a closure.
function makeAdder(int num) {
return function(int num2) { return num + num2; }
}
adder = makeAdder(5);
adder(7) // returns (5+7) = 12
adder(2) // returns (5+2) = 7
Here, the closure is the inner function, as returned by makeAdder, along with the '5' that was passed.
The javascript engine might choose to optimize away the function shown above, to make things run faster, never generating or running that code, so it can't be debugged or referenced. Optimizers are supposed to be very careful to ensure there's no impact, so I'd guess this one made a mistake.
http://ludex-marketing.com/blog/2009/12/google-analytics-javascript-error-cannot-access-optimized-closure-in-ga-js/
This can also be caused by a simple race condition. I was just refactoring a 'startup' object that does a few things before the document is ready. As soon as I tried to access a second object defined immediately below the startup object I received this error.
I was under the impression that script execution waited until all of the code was compiled. Clearly that's not the case. Waiting for document ready to call methods on the second object fixed the problem. Also, using this nice 'dump()' function confirms that the second object is only partially defined when the error occurs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
I encountered the same error today. In my case this occurred because I was referencing an object's attribute or function that did not exist or was not available. I'm guessing that since the object was available via a closure that was optimized, firebug could not access metadata on that object and thus the cryptic error message.
This also happened to me today. Firebug error'd at line 2 of this function:
function IsValidDate(objName) {
re = new RegExp('^( +|today|pdate|- *\\d+ *(day(s|)|week(s|))+ *$', 'i');
if (re.test(objName.value)) return 2;
return (chkdate(objName));
}
When I added "var " before the declaration of "re" in line 1, the error went away.
There is an exception being raised somewhere else in your code within the function that has this error. It could be as simple trying to access a variable that doesn't exist.
I think we need to get a Firebug dev in here to answer why it doesn't give a more specific error as to where in the closure that raised the exception to prompt the error.
You pasted options.length(), but it is not what prompted the error. What caused the error is the fact that your bug was inside a closure.
function(){
array.length()
}
that gives the error