简体   繁体   English

如何js调用function并访问调用者参数

[英]how to js call function and access caller parameters

function caller(id){
    accessParam()
}

function accessParam() {
    // access caller param without passing
    if(id){
        // do something
    }
}

// execute caller function
caller(1001)

Is there a posible way to access without passing paramter accessParam()有没有一种可能的方式来访问而不传递参数accessParam()

Is there a posible way to access without passing paramter accessParam()有没有一种可能的方式来访问而不传递参数accessParam()

No, there isn't.不,没有。 id is a local variable within caller() , and there's no way to access local variables from outside the function. idcaller()中的局部变量,无法从 function 外部访问局部变量。

And even if there were, it would be very poor design, since it would mean that accParam() could only be used from that function.即使有,这也是非常糟糕的设计,因为这意味着accParam()只能从 function 中使用。 The general idea of functions is that they should operate independently.函数的一般思想是它们应该独立运行。

Passing parameters and returning values is the cornerstone of abstraction in programming.传递参数和返回值是编程中抽象的基石。

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

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