简体   繁体   English

Javascript闭包(使用参数名称在同一函数中定义对象)

[英]Javascript closure (using parameter name to define an object in the same function)

I have the below code. 我有下面的代码。 Need to know how "a" is used as a function parameter, and then inside the same function it again used as object "a" to call another function.And what is mean by "a || {}" at the end of code. 需要知道如何将“ a”用作函数参数,然后在同一函数内再次将其用作对象“ a”来调用另一个函数。代码末尾的“ a || {}”是什么意思。

E.martin= function (a) {
a = mergein({ api_url: "/somefolder/",
          json_parameter: false,
          channel_id: null,
          after_response_hook: null},
          a || {});
//Here 'a' is a function arg
E.martin= function (a) {

//Here 'a' is overwritten by the returned value from mergein
a = mergein({ api_url: "/somefolder/",
      json_parameter: false,
      channel_id: null,
      after_response_hook: null},

      //Here 'a' is send to the function, if it's not null/false.
      //if 'a' is null/false an empty object will be created and sent instead.
      a || {});

mergein does probably add a function to the arg a . mergein可能确实向arg a添加了一个函数。

I can answer the a || 我可以回答|| {}; {}; section 部分

This is a way to check if "a" already exists. 这是一种检查“ a”是否已经存在的方法。 If it does then use it, if it doesnt the create it as a new object. 如果使用,则使用它;如果不使用,则将其创建为新对象。

EDIT : 编辑

To answer your actual question (I originally thought you were having issues with the code), the a || {} 为了回答您的实际问题(我本来以为您在代码中遇到了问题),请a || {} a || {} part of the code says "either use 'a' or if 'a' is not defined then use a new empty object ({})". a || {}部分代码表示“要么使用'a',要么如果未定义'a',则使用新的空对象({})”。

ADVICE : 建议

I would advise you return the a in your E.martin method as objects in JavaScript are not hard referenced. 我建议您在E.martin方法中返回a,因为JavaScript中的对象不是硬引用。 If you don't return the result, you'll likely lose the original object that you sent to the method. 如果不返回结果,则可能会丢失发送给该方法的原始对象。

Let's say mergein is a method that concatenates two objects: 假设mergein是连接两个对象的方法:

function mergein(new_obj, old_obj){

    for(var i in new_obj){

        old_obj[i] = new_obj[i];   

    }

    return old_obj;

}

If we have your original method, we will lose our original object keys/values when we get our result back: 如果我们拥有您的原始方法,则在返回结果时,我们将丢失原始的对象键/值:

E.martin = function (a) {

    a = mergein({ api_url: "/somefolder/",
          json_parameter: false,
          channel_id: null,
          after_response_hook: null},
          a || {});

}

var b = {foo:'bar'};

var result = martin(b);

console.log(result['foo']); // error

If we return our a object, we'll get back our original object with the added keys/values: 如果返回a对象,我们将使用添加的键/值来返回原始对象:

E.martin = function (a) {

    return mergein({ api_url: "/somefolder/",
          json_parameter: false,
          channel_id: null,
          after_response_hook: null},
          a || {});

}

var b = {foo:'bar'};

var result = martin(b);

console.log(result['foo']); // 'bar'

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

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