Calling a Function defined inside another function in Javascript

前端 未结 6 549
醉酒成梦
醉酒成梦 2020-12-02 13:12

I am calling a function on button click like this:

​

function outer() { 
    alert(\"hi\"         


        
6条回答
  •  不思量自难忘°
    2020-12-02 13:53

    The scoping is correct as you've noted. However, you are not calling the inner function anywhere.

    You can do either:

    function outer() { 
    
        // when you define it this way, the inner function will be accessible only from 
        // inside the outer function
    
        function inner() {
            alert("hi");
        }
        inner(); // call it
    }
    

    Or

    function outer() { 
        this.inner = function() {
            alert("hi");
        }
    }
    
    

提交回复
热议问题