English 中文(简体)
运行? 功能 () { 返回假 { 返回假 } } () 的详情
原标题:Detail of operation of !function() { return false; } ()
!function() { return false; } ()

我知道为什么你会写这样的东西, 但我有一个问题 关于它是如何工作的。据我所知,感叹标记有两件事:

  1. It acts on function() { return false; }, changing it into an expression
  2. It also acts on the result of the executed function, so that the whole line evaluates to true

所以,我的问题是:

  1. Is this the correct explanation?
  2. If it is correct, then since () binds more tightly than !, how did the first part (the change of the function itself into an expression) happen? Why doesn t the exclamation mark act on the whole line throughout?
最佳回答

1) 它不“ 改变” 。 当解析器通过“ 功能” 位时, 它就会期望表达, 所以“ 功能” 被解析成函数表达( 可能匿名) 而不是函数声明的一部分 。

2)它“em>is 'em'is '/em> 在整个行线上行动。如果按照jbabey的建议,你看这些位次,你会看到这个函数调来更紧紧,那么否定的操作员就会被评估为

! ((function(){})());

或以类似、或许更易读的版本:

var f = function(){ ... };
! (f());
问题回答

根据 rel=“nofolt”>>operator properpotrator proces , 声明(是 new 函数 的简称)首先发生,函数调用 () 将第二发生,否定 ! 将最后发生。

为清楚起见编辑: 您可以重写此行来完成同样的事情 :

// declare an anonymous function and assign it to the myFunc variable
var myFunc = function () { 
    return false; 
};

// execute the function and store it s return value (false) in returnValue
var returnValue = myFunc();

// negate the return value (true)
var output = !returnValue;




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