javascript 使用javascript调用嵌套函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7212886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:19:30  来源:igfitidea点击:

Calling nested function using javascript

javascript

提问by satya

I have this code which calls a function test() on body onload

我有这段代码,它在加载时调用一个函数 test()

<body onLoad="test();">

The Test function has 2 more functions drawLayers() ,StopAll().

Test 函数还有 2 个函数 drawLayers() ,StopAll()。

function test() {

  function drawLayers() {
   timers = [];
        timers.push(setTimeout(drawMoon,800));
       timers.push(setTimeout(drawCircle1,2300));
        timers.push(setTimeout(drawCircle2,2700));
    timers.push(setTimeout(drawCircle3,3100));
        timers.push(setTimeout(drawCircle4,3500));
        timers.push(setTimeout(drawCircle5,3900));
        timers.push(setTimeout(drawtext2,4300));
        timers.push(setTimeout(drawtext,4700));
        timers.push(setTimeout(drawtext3,5100));
        timers.push(setTimeout(drawtext4,5500));
        timers.push(setTimeout(drawtext5,5900));
        timers.push(setTimeout(drawtext6,6300));
        timers.push(setTimeout(drawtext7,6700));
        timers.push(setTimeout(drawtext8,7100));
        timers.push(setTimeout(drawtext9,7500));
        timers.push(setTimeout(drawtext10,7900));



    }

 function StopAll() {
     alert('fsdfsdf');
        for (var i = 0; i < timers.length; i++)
             window.clearTimeout(timers[i]);
    }
}

What i want to do is Call the StopAL() function on click of a button, the html code looks like below

我想要做的是单击按钮调用 StopAL() 函数,html 代码如下所示

<a href="javascript:void(0);" onClick="StopAll();">

Its throwing error, "StopAll is not defined"

它的抛出错误,“StopAll 未定义”

How do i call the StopALL() function?

我如何调用 StopALL() 函数?

回答by Darin Dimitrov

The scope of those nested functions is restricted to the testfunction only. You cannot invoke them from the outside. If you need to do that you could externalize it from the testfunction.

这些嵌套函数的范围仅限于test函数。你不能从外部调用它们。如果你需要这样做,你可以从test函数中将它外部化。

回答by hellosmithy

This is a 'closure' problem. The function StopAllis within the scope of the testfunction, and therefore is undefined in the global scope in which you are trying to call it.

这是一个“关闭”问题。该函数StopAll在函数的范围内test,因此在您尝试调用它的全局范围内未定义。

Closures are a tricky subject to grasp initially. There's a good explanation here: How do JavaScript closures work?

闭包最初是一个棘手的主题。这里有一个很好的解释: JavaScript 闭包如何工作?

(by the way StopAllshould really be called stopAllbecause capitalised functions are generally reserved for use with the newkeyword.)

(顺便说一句,StopAll应该真正调用,stopAll因为大写函数通常保留用于new关键字。)

回答by praveenpds

test = function (){

  this.drawLayers = function() {
        this.timers = [];
        this.timers.push(setTimeout(drawMoon,800));       
    }

  this.StopAll = function() {
       alert('fsdfsdf');
        var t = timers.length
        for (var i = 0; i < t; i++)
             window.clearTimeout(this.timers[i]);
    }
}

 var testObj =  new test();
 testObj.StopAll()

回答by Kundan Bharadwaj

function test() {

    function drawLayers() {
        timers = [];
        timers.push(setTimeout(drawMoon,800));
        timers.push(setTimeout(drawCircle1,2300));
        timers.push(setTimeout(drawCircle2,2700));
    }

    var StopAll=function() {
        alert('fsdfsdf');
        for (var i = 0; i < timers.length; i++)
            window.clearTimeout(timers[i]);
    }
    return StopAll;
}
var obj= new test();
//to call StopAll function
obj();

回答by praveenpds

(function test($) {
  function drawLayers() {  
  }
  //expose this to outside world ,public function
  $.StopAll = function() {
    alert('fsdfsdf');
  }
})(window);

StopAll();

回答by Headshota

You'd better not use html attributes to bind event handler, you can do the same with the following code:

最好不要使用 html 属性来绑定事件处理程序,您可以使用以下代码执行相同操作:

    window.onload = function(){
      document.getElementById("myLink").onclick = function(){
        StopAll();    
      }    
    }


  // Your functions

This way you'll ensure your dom is loaded and ready to call event handlers.

通过这种方式,您将确保您的 dom 已加载并准备好调用事件处理程序。

回答by Dhepthi

You can move the function StopAll() outside the test function and call it as specified. If suppose you need to access that function even in the test(), you can do like this

您可以将函数 StopAll() 移到测试函数之外并按指定调用它。如果假设您甚至需要在 test() 中访问该函数,您可以这样做

function test() {

.....
drawLayers();
StopAll() ;

}

function StopAll() {
     alert('fsdfsdf');
        for (var i = 0; i < timers.length; i++)
             window.clearTimeout(timers[i]);
    }

Declaration of function can be given outside and called any where you want

函数声明可以在外部给出并在任何你想要的地方调用