English 中文(简体)
这对“新陈代谢”。 [复制]
原标题:what does this do in js "xin || {}"? [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
What does var x = x || {} ;

我在js中有这种界限,这只是简单明了,但并不确定最后一部分——<代码>(毒性->>>(<<>>>>>>>。

var xin = (function (name) {return name;}(xin || {}));

我的理解是,毒性是一种物体构造,现在我可以制造有毒物体。 <代码>新谢>,<>。 请允许我向大家介绍,感谢

最佳回答
xin || {}

这与:

xin ? xin : {};

或更长期的形式:

if(xin) {
    return xin;
}
else {
    return {};
}

见有关复制问题。 j 认为:

<代码>>>。

表达

var x = x OR {};

届时应当变得更加明显。

如果xfalsy数值(如null,un specified/code>0,><>>>>,则仅保留现值。 如此之长的版本就象这样。

var x = x ? x : {};
问题回答

毒素>值 如果不是虚假(或未界定或无效)或新物体({><>>>/代码>);

例如:

var name = xin || {};

这一点已转化为:

if(xin)
    var name = xin;
else
    name = {};

在javascript中,你可以使用有条件的操作者( >/code>和<&&)来制作表述。 例如:

typeof f ===  function  && ((arguments.length ==  0 && f()) || f.apply({},arguments)) || g();

这一表述可翻译成以下文字:

if(typeof f ===  function )
    {
        if(arguments.length == 0)
            f();
        else
            f.apply({},arguments);
    }
else
    g();

不管怎么说,这只是为了去掉目的,你竟然使用与有条件的操作者一起建造的大型nes词,因为它非常无法读(很难维持)。

这种表述经常用于初始化,如果已经存在的话,你不想超越某种价值。 一个很好的例子就是在多个js档案中使用一个名称空间。 采用这一技术(var namespace = namespace ±>>;

如果毒素是真实的,它会评估毒性,否则会使用新的物体。

这是:

var xin = (function (name) {return name;}(xin || {}));

这完全相同:

var xin = xin || {};

可以重申:

var xin = xin ? xin : {};

进一步加强我们:

if(!xin) xin = {}

如果没有任何新鲜毒素反对,它就会把新的(或现有的)新毒素带上新功能。

这是人为造名空间的comm。





相关问题
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.