关于作用域的问题,函数非全局函数的影响?

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script>
function fn1(data) {//比如把fn1函数放入window.load=function(){}的函数里面为什么就不能执行的?是什么原因导致的?就是fn1不是全局函数的话为什么出错了?
    var oUl1 = document.getElementById('ul1');
    var html = '';
    for (var i=0; i<data.length; i++) {
        html += '<li>'+data[i]+'</li>';
    }
    oUl1.innerHTML = html;
}
function fn2(data) {
    var oUl2 = document.getElementById('ul2');
    var html = '';
    for (var i=0; i<data.length; i++) {
        html += '<li>'+data[i]+'</li>';
    }
    oUl2.innerHTML = html;
}
function fn3(data) {
    var oUl3 = document.getElementById('ul3');
    var html = '';
    for (var i=0; i<data.length; i++) {
        html += '<li>'+data[i]+'</li>';
    }
    oUl3.innerHTML = html;
}
</script>
<script>
window.onload = function() {
    
    var oBtn1 = document.getElementById('btn1');
    
    var oBtn2 = document.getElementById('btn2');
    
    oBtn1.onclick = function() {
        
        var oScript = document.createElement('script');
        oScript.src = 'getData.php?callback=fn1';
        document.body.appendChild(oScript);
        
    }
    
    var oBtn2 = document.getElementById('btn2');
    
    oBtn2.onclick = function() {
        
        var oScript = document.createElement('script');
        oScript.src = 'getData.php?t=str&callback=fn2';
        document.body.appendChild(oScript);
        
    }
    
    var oBtn3 = document.getElementById('btn3');
    
    oBtn3.onclick = function() {
        
        var oScript = document.createElement('script');
        oScript.src = 'getData.php?callback=fn3';
        document.body.appendChild(oScript);
        
    }
    
}
</script>
</head>

<body>
    <input type="button" id="btn1" value="加载数字" />
    <ul id="ul1"></ul>
    <input type="button" id="btn2" value="加载字母" />
    <ul id="ul2"></ul>
    <input type="button" id="btn3" value="加载字母" />
    <ul id="ul3"></ul>
</body>
</html>

-----------------PHP文件-----------------

<?php
$t = isset($_GET['t']) ? $_GET['t'] : 'num';
$callback = isset($_GET['callback']) ? $_GET['callback'] : 'fn1';

$arr1 = array('111111','22222222','33333333','4444444','555555555555555555555');
$arr2 = array('aaaaaaaaaaaa','bbbbbbbb','cccccccccccc','ddddddddd','eeeeeeeeeeee');

if ($t == 'num') {
    $data = json_encode($arr1);
} else {
    $data = json_encode($arr2);
}

echo $callback.'('.$data.');';
阅读 1.9k
1 个回答

简化下问题就是如下代码段:
fn2函数因为在onload函数中定义,此作用域在onload函数内,出了这个函数就不能被访问到
fn1函数定义在全局作用域下,可以被全局访问

    <script>
        function fn1(){
            console.log("fn1");
        }
    </script>
    <script>
        window.onload=function(){
            console.log("~~onload~~");
            fn1();

            function fn2(){
                console.log("fn2")
            }
        }
    </script>
    <script>
        fn2();//Uncaught ReferenceError: fn2 is not defined
    </script>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题