简体   繁体   中英

How to call a specific function from inside a subfunction in javascript?

How do I call function "a" from "av" without a direct reference "a()"

function a(){
    alert("1");
    this.v=function(){alert("hi")};
}

One way I can think of doing this without repeating a :

function a(){
    let indirect = arguments.callee;
    alert("1");
    indirect.v=function(){ indirect(); };
}

a();
a.v();

But this does not work under strict mode and you must call a before calling av .

You could also do:

function a(){
   alert('1');
   this.v = () => this();
}

a.v = a;
a.v();

This way you don't call a() and it also works under strict mode.

Set its v property outside the function itself:

 function a() { alert("1"); } av = function() { a() alert("hi") }; av()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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