English 中文(简体)
返回的门槛?
原标题:Ternary to return undefined?

我只想在确定变数的情况下把财产列入物体。 我根本不想要财产。 我甚至不希望这等于一片空洞。 我这样想:

someFunc({
    bing: "bing",
    bang: (myVar) ? myVar : undefined,
    boom: "boom"
    }, "yay");

如果对<密码>myVar未作界定,我就希望这样作:

someFunc({
    bing: "bing",
    boom: "boom"
    }, "yay");

我这样做了吗?

最佳回答

a

var ex1 = {foo: undefined};

为此:

var ex2 = {};

因此,我如何这样做:

var args = {
    bing:  bing ,
    boom:  boom 
};

if (typeof myVar !==  undefined ) {
    args.bang = myVar;
}

someFunc(args,  yay );
问题回答

i)

var config = {
    bing: "bing",
    boom: "boom"
};

if (typeof myVar !==  undefined ) config.bang = myVar;

someFunc(config,  yay );

你们必须仔细了解 j的真理和 f。 如果用我的话来说,我的话只是说,如果界定我的Var,那么它就能够运作,如果我的Var被定义为假的。

使用休息器。

var obj = {
    bing: "bing",
    ...(myVar ? { bang: myVar } : {}),
    boom: "boom"
}
var obj = {
    bing: "bing",
    boom: "boom"
}

myVar != null && obj.bang = myVar;

someFunc(obj, "yay");

上述法典做了工作。 我不认为你可以直接在职能呼吁范围内这样做。

这样做的最佳途径是

var args = {
    bing: "bing",
    bang: myVar,
    boom: "boom"
}

if (myVar === undefined) {
    delete args.myVar;
}

someFunc(args, "yay");

显然,如果存在的话,最好删除。

问题在于处理像它真的是t 那样的东西,最安全的做法是利用本地的将财产从物体中删除。

你们可以用固定时间来打折扣。 我再次建议这样做。

var args = {
    bing: 4,
    bang: (myVar === undefined) ? (function() {
        setTimeout(function() {
            delete args.bang;
        }, 0);
    })() : myVar
}, "yay");




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.