javascript - ternary operation inside object creation - Stack Overflow

I have a traditional conditional statement below, and I was wondering if I could translate this into an

I have a traditional conditional statement below, and I was wondering if I could translate this into an inline conditional(ternary?) statement.

var type;

if type === 1 {

    $('#form1').append(new mySuperObject({ model: myModelB}).render().$el);

} else {


    $('#form1').append(new mySuperObject({ model: new myModelA({ type: myType.Gas }) }).render().$el);

}

Would this be the proper format?

$('#form1').append(new mySuperObject({ model: ((type === 1) ? myModelB : new myModelA({ type: myType.Gas) }) }).render().$el);

When I try it, I get the error below in Firebug console:

TypeError: this.model.get(...) is undefined

Thanks

I have a traditional conditional statement below, and I was wondering if I could translate this into an inline conditional(ternary?) statement.

var type;

if type === 1 {

    $('#form1').append(new mySuperObject({ model: myModelB}).render().$el);

} else {


    $('#form1').append(new mySuperObject({ model: new myModelA({ type: myType.Gas }) }).render().$el);

}

Would this be the proper format?

$('#form1').append(new mySuperObject({ model: ((type === 1) ? myModelB : new myModelA({ type: myType.Gas) }) }).render().$el);

When I try it, I get the error below in Firebug console:

TypeError: this.model.get(...) is undefined

Thanks

Share edited Jan 10, 2014 at 18:24 kei 20.5k2 gold badges37 silver badges64 bronze badges asked Jan 10, 2014 at 18:17 SkyeBoniwellSkyeBoniwell 7,13215 gold badges100 silver badges218 bronze badges 5
  • 4 Ternary operators are better used in one-line expressions; if..else looks better in this case. – elclanrs Commented Jan 10, 2014 at 18:18
  • 1 The ternary is illegible. The only thing you need in the conditional is the creation of the new mySuperObject, too. – Dave Newton Commented Jan 10, 2014 at 18:19
  • 3 Also, your syntax is wrong. Check here jshint. – elclanrs Commented Jan 10, 2014 at 18:20
  • Where are the parentheses that go around the if conditional? – crush Commented Jan 10, 2014 at 18:21
  • 1 new myModelA({ type: myType.Gas) }) should be new myModelA({ type: myType.Gas }) ) – Blazemonger Commented Jan 10, 2014 at 18:21
Add a ment  | 

4 Answers 4

Reset to default 4

I think you just have a parenthesis out of place. Compare the two lines below. The first is the orginal.

$('#form1').append(new mySuperObject({ model: ((type === 1) ? myModelB : new myModelA({ type: myType.Gas) }) }).render().$el);
$('#form1').append(new mySuperObject({ model: ((type === 1) ? myModelB : new myModelA({ type: myType.Gas })) }).render().$el);

To make it easier to read though, you could go with:

var model = (type === 1) ? myModelB : new myModelA({ type: myType.Gas });
$('#form1').append(new mySuperObject({ model: model }).render().$el);

You have some syntax errors in there..

for readability you could use

var objToUse = (type === 1) ? myModelB : new myModelA({ type: myType.Gas});
$('#form1').append(new mySuperObject({ model: objToUse }).render().$el);

Don't jam all of that stuff together like that. To avoid the duplication of code, you can do something like this:

var type;
var model;

if(type === 1) {

    model = myModelB;

} else {

    model = new myModelA({ type: myType.Gas });

}

$('#form1').append(new mySuperObject({ "model": model}).render().$el);

The following:

if (type === 1) {
    $('#form1').append(new mySuperObject({ model: myModelB}).render().$el);
} else {
    $('#form1').append(new mySuperObject({ model: new myModelA({ type: myType.Gas }) }).render().$el);
}

is the same as:

 var model;
 if (type === 1) {
     model = myModelB;
 }
 else {
     model = new myModelA({type: myType.Gas});
 }
 $("#form1").append(new mySuperObject({model: model})).render().$el;

Which is the same as:

 var model = (type === 1) 
             ? myModelB 
             : new myModelA({type: myType.Gas});

 $("#form1").append(new mySuperObject({model: model})).render().$el;

And you can jam it together if you really want:

 $("#form1").append(new mySuperObject({model: (type === 1) ? myModelB : new myModelA({type: myType.Gas})})).render().$el;

I do remend the second or third variant vs. jamming it all together in one line, though. The functional programmer in me likes the third over the second, actually, because you only have one assignment and don't have to declare the variable separately from assigning it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744913931a4600718.html

相关推荐

  • javascript - ternary operation inside object creation - Stack Overflow

    I have a traditional conditional statement below, and I was wondering if I could translate this into an

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信