English 中文(简体)
在联合材料中设立职能[复制]
原标题:Creating a function in JS [duplicate]
  • 时间:2011-04-21 21:21:13
  •  标签:
  • javascript
This question already has answers here:
Closed 12 years ago.

Possible Duplicate:
Javascript: var functionName = function() {} vs function functionName() {}

我很想知道,在js创造职能时,这是最好的预兆。

function x() {
    ...
}

页: 1

var x = function() {
    ...
}

是否存在差别,或者这些差别完全相同。

最佳回答
x(); // I work
function x() {
    ...
}
y(); // I fail
var y = function() {
    ...
}

第一项是职能声明。 在你宣布之前,你可以行使职能。

第二是将职能分配到一个变量。 这意味着你可以做任何事情。

您可将其编成foo[0]foo.bar.bazfoo.get(”baz”)[0]

问题回答

I prefer the first form because it gets defined before variables are defined.

So, you could invoke x in a later scope because the interpreter already defined that function even though it may be declared later on in your code.

这将更简单一些法典:

x(); //logs "hi"

//...
function x() { 
   console.log("hi"); 
}

v

x(); //fails

var x = function() { 
   console.log("hi");
};

这些人并非完全一样,他们提到了前方的分歧,这里的微妙之处——功能名称:

function x(){}
x.name; // "x"

var x = function(){};
x.name; // ""

在第二种情况下,你可以把职能作为参数,将其储存在阵列中。

第一项职能是申报<>,后一项职能是表示/>。 阅读更多here,而我试图在阅读关于差异和所涉问题时发现一个非常深入的第一条。

Edit: Ah, here we go. Grab a cup of tea and settle in for an in-depth discussion.





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