0

I have just spent far too long trying to find a problem with the code below.

In turned out that because of the context that addRoute was called in, keys() was not returning the keys of the results object. To fix this I had to use Object.keys() despite it working without a problem in the JavaScript console (which I later realised was because of the context).

My question is, why didn't this show in my JavaScript console? It took me quite a while to realise (I have cropped the full code, the actual function is a lot bigger).

Wrong, but no error in the console:

Map.prototype.addRoute = function (results) {
    var sectionsIDs = keys(results);
}

Correct

Map.prototype.addRoute = function (results) {
    var sectionsIDs = Object.keys(results);
}

1 Answer 1

3

Your first function uses the keys console API function.

That "Command Line API Reference" page includes the warning:

Note: This API is only available from within the console itself. You cannot access the Command Line API from scripts on the page.

Thus, it is by design that the keys function only exists for code run directly on the console.

Chrome gives you a small hint about the keys function being a console-only function if you view it in the console:

 > keys  
 function keys(object) { [Command Line API] }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.