开发者

javascript ternary operator inside variable

okay im working with a friend and he sent me js file which included a variable that included the ternary operator. I cant figure out how to change it to if..else. can you help please?

also i noticed ".length" didnt have the normal "()" after it, is there a reason why?

    var nextRadioTab =开发者_开发问答 activeRadioTab.next().length ? activeRadioTab.next() : $('#contentslider div:eq(0)');


Does this work?

if (activeRadioTab.next().length) {
    var nextRadioTab = activeRadioTab.next();
} else {
    var nextRadioTab =  $('#contentslider div:eq(0)');
}

In JavaScript, objects are more-or-less just a list of names pointing to values. Each name-value pair is called a "property".

These values themselves can be any type of value, including a function. If the value of a property is a function, we call that a "method".

Say you want an object to track the x and y coordinates of a point.

var point = { x: 10, y: 20 };

In this case we can just use simple values, because we don't need any behaviour more advanced than getting a value (alert(point.x)) or setting one (point.x = 10).

jQuery is designed to let your code work on different browsers; different browsers behave differently in lots of situations, so jQuery can't just let you set

element.text = "hello world"

because depending on the type of object element is, it will need to modify different properties on different browsers. For this reason, jQuery makes you use methods for things like this:

element.text("hello world")

The .length attribute of a jQuery object is simple; it's controlled by jQuery itself and doesn't need to do any special things in different browsers. For this reason, you just use it directly. If they needed more complicated behaviour, they would use a function/method instead:

var myObject = { length: 2 }; // myObject.length
var myObject = { length: function() { return 2; } }; // myObject.length()


var nextRadioTab;

if (activeRadioTab.next().length)
  nextRadioTab = activeRadioTab.next();
else
  nextRadioTab = $('#contentslider div:eq(0)');

length is a property of whatever next() returns, which is most likely the same type of object as activeRadioTab.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜