繁体   English   中英

如何有条件地向javascript对象文字添加属性

[英]How to conditionally add properties to a javascript object literal

我正在尝试执行以下操作以满足代码生成器的要求(Sencha Cmd是特定的)。

这就是我需要做的事情。 关键因素是函数体必须以对象文字的返回结束。 由于构建器的限制,我无法返回变量。 因此,如果参数'includeB'为true,如何在伪代码点处添加属性'b',但如果为false,则不添加属性AT ALL。 即b == undefined或b == null是不允许的。

也许这是不可能的。

function create(includeB) {
        // Can have code here but the final thing MUST be a return of the literal.
        // ...
    return {
        a : 1
        // pseudo code:
        // if (includeB==true) then create a property called b 
        // and assign a value of 2 to it. 
        // Must be done right here within this object literal
    }
}

var obj = create(false);
// obj must have property 'a' ONLY

var obj = create(true);
// obj must have properties 'a' and 'b'

感谢您的阅读和考虑,

穆雷

如果可以使用ES6,请使用spread属性

function create(includeB) {
    return {
        a : 1,
        ...(includeB ? { b: 2 } : {}),
    };
}

您已经显示了构造函数的用例,而不是使用对象文字:

function CustomObject(includeB) {
    this.a = 1;
    if (includeB) {
        this.b = 2;
    }
}

//has `a` only
var obj1 = new CustomObject(false);

//has `a` and `b`
var obj2 = new CustomObject(true);

重新阅读您的问题后,您似乎在修改功能时获得了有限的访问权限。 如果我正确理解您的问题,您只能更改脚本的有限部分:

function create(includeB) {
    // modifications may be done here

    // the rest may not change
    return {
        a : 1
    }
}

var obj = create(false);
// obj must have property 'a' ONLY

var obj = create(true);
// obj must have properties 'a' and 'b'

如果是这种情况,那么你可以简单地跳过函数的后面部分:

function create(includeB) {
    if (includeB) {
        return {
            a: 1,
            b: 2
        };
    }
    return {
        a: 1
    };
}

您不能将布尔逻辑放在javascript文字定义中。 因此,如果您的构建器要求返回的对象只能被定义为javascript文字,那么您无法以这种方式有条件地定义属性。


如果你可以在你的函数中创建一个对象,使用逻辑修改该对象然后返回该对象,那么这很容易。

function create(includeB) {
    var x = {
        a: 1
    };
    if (includeB) {
        x.b = 2;
    }
    return x;
}

您的另一个选择是包装create函数并在create函数之外执行。

function myCreate(includeB) {
    var x = create(includeB)
    if (includeB) {
        x.b = 2;
    }
    return x;
}

或者,您甚至可以透明地包装create函数,因此调用者仍然使用create() ,但它的行为已被更改。

var oldCreate = create;
create = function(includeB) {
    var x = oldCreate(includeB);
    if (includeB) {
        x.b = 2;
    }
    return x;
}

这应该按照你想要的方式工作:

const obj = {
    a: 1,
    b: includeB ? 2 : undefined
}

我最近不得不这样做,发现你可以在对象的定义中使用自调用函数(如果使用ES6)。 这类似于已接受的答案,但对于需要在不先定义构造函数的情况下执行此操作的其他人可能会有用。

例如:

let obj = (() => {
  let props = { a: 1 };
  if ( 1 ) props.b = 2;
  return props;
})();

制作对象:{a:1,b:2}

它对于更复杂的物体更方便,保持结构连续:

let obj = {
  a: 1,
  b: (() => {
    let props = { b1: 1 };
    if ( 1 ) props.b2 = 2;
    return props;
    })(),
  c: 3
}

使对象:

{
  a: 1,
  b: {
    b1: 1,
    b2: 2
  },
  c: 3
}

您可以稍后定义它:

var hasA = create(); // has hasA.a

var hasBoth = create();
hasBoth.b = 2; //now has both

或者,在create使用您的参数:

function create (includeB) {
  var obj = { 
    a : 1
  };
  if (includeB) {
     obj.b = 2;
  }
  return obj;
}

这个怎么样:

function create(includeB) {
    return includeB && { a:1, b:2 } || { a:1 };
}

includeB为true时,create函数将返回{a:1, b:2} 如果includeB为false,它将返回or之后的任何内容 - 在本例中为{a:1}对象。

create(true)返回{ a:1, b:2 }

create(false)返回{ a:1 }

下面应该工作。 我希望这有帮助。

function create(includeB){

var object = {
    a: 1
};

if (includeB)
    object.b = 2;

return object;

}

如果您想使用声明来满足相同的要求一次没有太多膨胀,您还可以简单地执行以下操作:

var created = function(includeB) {
    var returnObj = { a : 1 };

    if(includeB) { returnObj.b = 2; }

    return returnObj;
}}(); //automatically runs and assigns returnObj to created

暂无
暂无

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

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