javascript - Uncaught ReferenceError: keys is not defined - javascript

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.

Related

writing and using custom JS functions in lab.js

I have a question on writing & using custom functions in lab.js studies:
I defined a function in a component at the beginning of the script and I’d like to use it in the following components in my study, but for some reason it doesn’t work.
I can call the function in the component where I defined it, but if I try to call it a few components later, it throws a ReferenceError and says the function is not defined.
Any clues what might be the issue here?
I think I’m not defining my function correctly at the beginning of the experiment but I’m not entirely sure. This is my code for the function:
const extend = function(array1, array2){
var array1, array2;
return Array.from(array1).concat(Array.from(array2));
}
This is how I call the function:
extend([1,2], [3,4,5]);
And this is where you can find the .json for the experiment:
https://github.com/MMarieSchuckart/stackoverflow_demo
Thanks in advance for any ideas/hints!
-Merle
Okay so I tried about 100 things and apparently, it's not enough to declare the function using const or var, you need to use window. to make it global.
The solution looks like this:
window.extend = function(array1, array2){
var array1, array2;
return Array.from(array1).concat(Array.from(array2));
}

Functions as object in Javascript

Hey guys I'm kind of new to JS and found out that functions are also objects.
This means I can add properties to them like this:
let func = function(){};
func.foo = "foo";
console.log(func.foo); // prints foo
However when we now do this:
console.log(func);
It will return (using chrome):
Why does it not show the properties of the object like it usually shows on other type of objects?
Also when we for instance try to console.log(Function) it will return the following output:
What is this native code? What I got from other sources was that it is code written in another programming language(C, C++) that programmed the functionality of this constructor.
Thanks in advance!
Chrome’s console displays functions’ bodies instead of their properties, because that’s usually more useful. It’s much easier to tell when you don’t use an empty function:
And it’ll indeed substitute in [native code] when there is no JavaScript body to be shown.
As #ibrahim mahrir points out, you can use console.dir(func) to get both the default inspection and an expandable list of properties.

No line number specified in console error message. What to do?

In a recent day I faced with this kind of error message, that has no position specified. Debugging becomes much more difficult. What did I do wrong?
When I started re-define JSON.parse functions of my objects, after that appears it first time.
I replaced them to normal functions, but the error message is still strange.
Maybe because I'm writing prototype functions, and I call them before constructing a new object?
Is it because I call a function from console?
I managed to simplify the error:
var o = {g : false};
function f(){
console.log(o.g());
}
If I call it from Chrome and form console, than I will not get any line number.

Error logging associative array properties that are functions using `util.inspect` in node.js

I'm using node.js's util.inspect call to dump a JavaScript associative array to the log. The associative array in question includes member properties that are functions. An example is:
var pendingscreen = {};
pendingscreen['timeoutfunction'] = function(){ sendmsg(); };
pendingscreen['timeout'] = setTimeout(pendingscreen['timeoutfunction'], 1000);
console.log(util.inspect(pendingscreen));
When I run this, I get this error:
TypeError: Function.prototype.toString is not generic
at Client.toString (native)
at String.STRING_ADD_LEFT (native)
at isRegExp (util.js:287:14)
at format (util.js:184:11)
at util.js:216:19
at Array.map (native)
at format (util.js:193:23)
at util.js:216:19
at Array.map (native)
at format (util.js:193:23)
Is there any way to inspect the members of an associative array where some of the members could be functions?
util.inspect should handle this just fine. However there is a bug in the node.js version you are using which can cause this error.
This is fixed in newer version (>=0.4.11).
That actually looks like a bug in Node to me.
Normally it would just print out "[Function]" when you inspect the object.
Edit: I'm not sure this is actually the issue anymore.
This error is triggered by something like this:
(function(){ }).toString.call(null)
Calling the Function 'toString' with something that is not a function. I have no idea how that would happen though.

Javascript + Firebug : "cannot access optimized closure" What does it mean?

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

Categories

Resources