“Uncaught SyntaxError: Unexpected token (” but there is no error

限于喜欢 提交于 2019-12-13 02:12:42

问题


I'm authoring a simple userscript that will give the backspace button navigation control like in windows (specifically this is for linux users) for Chromium browser.

This script was working, then I made a few alterations to it (very simple stuff, commenting, tabbing, making it pretty), and now i'm getting this error:

Uncaught SyntaxError: Unexpected token (

on this line

document.head.appendChild(script);

The script is located here - i'm pulling out my hair trying to figure this out.

The script really only applies to chromium as ff gives you a configuration option to enable this functionality.. - Chromium 15.0.874.106 (Developer Build 107270) Ubuntu 11.10

Edit if someone can tell me why this doesn't work that would be great

EmbedCodeOnPage("(function() {" + fn.toString() + "})();"); // fails
EmbedCodeOnPage("(" + fn.toString() + ")()"); // works.

回答1:


because you are just dropping in an anonymous function and not executing it

change line 46 and add ()




回答2:


I believe the actual error is here:

function EmbedFunctionOnPageAndExecute(fn) {
    EmbedCodeOnPage("(function() {" + fn.toString() + "})();");
}

fn.toString is already going to format your function like this:

function () { /* code here */ }

So you're going to end up with this:

(function() { function () { /* code here */ } })();

This is clearly not what you want. You want to execute the inner function.




回答3:


I had the same problem, even with simple function. This might be due to incorrect syntax of the function definition itself, especially switching between java to javascript.

if i declare function with in an object definition such as calculateTax(){//some logic;} and run, i get "SyntaxError: Unexpected token (" - this is due to the fact that function declaration is not correct format/syntax. The error is misleading, however by changing it to calculateTax : function(){//some logic;} resolves the issue. Hope this helps. Thanks.



来源:https://stackoverflow.com/questions/8482681/uncaught-syntaxerror-unexpected-token-but-there-is-no-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!