JavaScript传递范围到另一个函数 [英] JavaScript pass scope to another function

查看:89
本文介绍了JavaScript传递范围到另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可能以某种方式将函数的作用域传递给另一个?



例如,

  function a(){
var x = 5;
var obj = {..};
b(< my-scope>);
}

函数b(){
//访问x或obj ....
}

我宁愿直接访问变量,即不使用 this.a this.obj ,但直接使用 x obj p>

解决方案

真正访问函数 a 的唯一方法是在 a 中声明 b ,所以它形成一个闭包,允许隐式访问 a 的变量。



这里有一些选项。



直接访问


  1. 声明 b code> a 。

      function a(){
    var x = 5,
    obj = {};
    function b(){
    //访问x或obj ...
    }
    b();
    }

    a();


  2. 如果您不想要 b a 里面,那么你可以在更大的容器范围内使用它们:

      function container(){
    var x,obj;
    function a(){
    x = 5;
    obj = {..};
    b();
    }
    function b(){
    //访问x或obj ...
    }
    }

    container.a ;


能够直接在 b 中使用 a 的变量,而无需一些额外的代码来移动。



间接访问 如果您对一些帮助和/ p>


  1. 您可以将变量作为参数传递,但除了对象的属性外,不会有写访问:

      function a(){
    var x = 5,
    obj = {};
    b(x,obj);
    }

    函数b(x,obj){
    //访问x或obj ...
    //这里改变x不会改变x ,但你可以修改obj的属性
    }

    a();

    作为变体,您可以通过将更新的值传递回 a 如下:

      // in a:
    var ret = b x,obj);
    x = ret.x;
    obj = ret.obj;

    // in b:
    return {x:x,obj:obj};


  2. 您可以传送 b 对象与可以访问 a 的私有变量的getter和setter:

      function a(){
    var x = 5,
    obj = {..},
    translator = {
    getX:function(){return x;},
    setX:function(value){x = value;},
    getObj:function(){return obj;},
    setObj:function(value){obj = value;}
    };
    b(translator);
    }

    函数b(t){
    var x = t.getX(),
    obj = t.getObj

    //使用x或obj ...
    t.setX(x);
    t.setObj(obj);

    //或者你可以直接修改obj的属性:
    obj.key = value;
    }

    a();

    getters和setter可以是public的,分配给 this 对象 a ,但是这种方式只有在 a 中明确给出时才可访问。 / p>


  3. 您可以将变量放在对象中并传递对象:

      function a(){
    var v = {
    x:5,
    obj:{}
    };
    b(v);
    }

    函数b(v){
    //访问vx或v.obj ...
    //或将新的局部x和obj变量设置到这些并使用它们。
    }

    a();作为变体,您可以在调用时构造对象:


      function a(){
    var x = 5,
    obj = {};
    b({x:x,obj:obj});
    }

    函数b(v){
    //访问vx或v.obj ...
    //或者将新的局部x和obj变量设置到这些并使用它们。
    }

    a();



Is it possible to somehow pass the scope of a function to another?

For example,

function a(){
   var x = 5;
   var obj = {..};
   b(<my-scope>);
}

function b(){
   //access x or obj....
}

I would rather access the variables directly, i.e., not using anything like this.a or this.obj, but just use x or obj directly.

解决方案

The only way to truly get access to function a's private scope is to declare b inside of a so it forms a closure that allows implicit access to a's variables.

Here are some options for you.

Direct Access

  1. Declare b inside of a.

    function a() {
       var x = 5,
          obj = {};
       function b(){
          // access x or obj...
       }
       b();
    }
    
    a();
    

  2. If you don't want b inside of a, then you could have them both inside a larger container scope:

    function container() {
       var x, obj;
       function a(){
          x = 5;
          obj = {..};
          b();
       }
       function b(){
          // access x or obj...
       }
    }
    
    container.a();
    

These are the only ways you're going to be able to use a's variables directly in b without some extra code to move things around. If you are content with a little bit of "help" and/or indirection, here are a few more ideas.

Indirect Access

  1. You can just pass the variables as parameters, but won't have write access except to properties of objects:

    function a() {
       var x = 5,
          obj = {};
       b(x, obj);
    }
    
    function b(x, obj){
       // access x or obj...
       // changing x here won't change x in a, but you can modify properties of obj
    }
    
    a();
    

    As a variation on this you could get write access by passing updated values back to a like so:

    // in a:
    var ret = b(x, obj);
    x = ret.x;
    obj = ret.obj;
    
    // in b:
    return {x : x, obj : obj};
    

  2. You could pass b an object with getters and setters that can access a's private variables:

    function a(){
       var x = 5,
          obj = {..},
          translator = {
             getX : function() {return x;},
             setX : function(value) {x = value;},
             getObj : function() {return obj;},
             setObj : function(value) {obj = value;}
          };
       b(translator);
    }
    
    function b(t){
       var x = t.getX(),
          obj = t.getObj();
    
       // use x or obj...
       t.setX(x);
       t.setObj(obj);
    
       // or you can just directly modify obj's properties:
       obj.key = value;
    }
    
    a();
    

    The getters and setters could be public, assigned to the this object of a, but this way they are only accessible if explicitly given out from within a.

  3. And you could put your variables in an object and pass the object around:

    function a(){
       var v = {
          x : 5,
          obj : {}
       };
       b(v);
    }
    
    function b(v){
       // access v.x or v.obj...
       // or set new local x and obj variables to these and use them.
    }
    
    a();
    

    As a variation you can construct the object at call time instead:

    function a(){
       var x = 5,
          obj = {};
       b({x : x, obj: obj});
    }
    
    function b(v){
       // access v.x or v.obj...
       // or set new local x and obj variables to these and use them.
    }
    
    a();
    

这篇关于JavaScript传递范围到另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆