簡體   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