简体   繁体   English

JavaScript函数未定义IE11

[英]JavaScript function undefined IE11

I'm trying to import a JavaScript function into my HTML document. 我正在尝试将JavaScript函数导入到HTML文档中。 It work's on Chrome however in IE11 it returning 'aFunction' is undefined. 它可以在Chrome上运行,但是在IE11中返回'aFunction'是未定义的。

I've read numerous help topics but am unable to find a solution yet. 我已经阅读了许多帮助主题,但是还找不到解决方案。

My Code is as follows. 我的代码如下。

Index.html Index.html

<head>...
<script  type="text/javascript" src="test.js"></script>
<script type="text/javascript">
        $(document).ready(function() {
                var myFunction = aFunction();
                //print to console to see if variable has anything in it
                console.log(myFunction);
            });
</script>
...</head>

test.js test.js

function aFunction(){ //some code... }

Any ideas would be much appreciated. 任何想法将不胜感激。

如果要使代码在IE11中运行,则应使用Babel对其进行转换

You made a mistake: you called aFunction instead to assign it to myFunction. 您犯了一个错误:而是调用了aFunction来将其分配给myFunction。 You need to change your code in the following way: 您需要通过以下方式更改代码:

var myFunction = aFunction;
//print to console to see if variable has anything in it
console.log(myFunction);
//now you can call your aFunction using myFunction var
myFunction();

UPDATE: 更新:

You get undefined because aFunction doesn't have a return: in such case, the returned value is 'undefined'. 由于aFunction没有返回值,您会得到未定义:在这种情况下,返回的值是'undefined'。 Using "var myFunction = aFunction();" 使用“ var myFunction = aFunction();” you assign to myFunction the result of aFunction, that is undefined, the value you got on your console. 您将myFunction的结果(未定义)分配给myFunction,该结果是您在控制台上获得的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM