" in a var statement mean?

Possible Duplicate:
null coalescing operator for javascript?
What does “options = options || {}” mean in Javascript?

Can someone explain me this expression? I stumbled accros the javascript line of code and I wondered what it means.

var node = element.node || element[element.length - 1].node;

node get's used like this below:

if (node.nextSibling) {
            node.parentNode.insertBefore(this.node, node.nextSibling);
        } else {
            node.parentNode[appendChild](this.node);
        }

At first i though node should be a boolean or something but it's not. Am I correct if i think that the meaning is: node is element.node but if the node attribute is undefined node is the last element in the array of element?


Your understanding is along the right lines; be aware that even if element.node is defined, but is a falsey value ( 0 , false etc.) that element[element.length - 1].node will be assigned to node instead.


Simple answer: it means OR :)

Reference: http://www.w3schools.com/js/js_operators.asp


这意味着,如果element.node的值在布尔表达式中表示为True,那么node将是element.node ,否则它将是element[element.length - 1].node

链接地址: http://www.djcxy.com/p/73360.html

上一篇: 如果变量未定义,则使用另一个Javascript

下一篇: “在var语句中是什么意思?