JavaScript 函数有不同的语法吗?
可能的重复: JavaScript 中函数表达式与声明之间有什么区别? 这种 JavaScript 语法我到现在为止还没见过,它到底有什么作用?
下面两种写法有什么区别一个函数?我见过两者都使用过,但我不确定哪一个是“正确的”。
function init() {
}
init: function() {
},
那么第二种写法有什么优点呢?
Possible Duplicate:
What is the difference between a function expression vs declaration in JavaScript?
This JavaScript syntax I haven't seen till now, what does it do really?
What is the difference between the following two ways of writing a function? I've seen both used, but I'm not sure which one is 'correct'.
function init() {
}
init: function() {
},
And what are the advantages to writing it the second way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第二种形式只能在对象文字的上下文中使用:
第一种形式作为语句执行。它相当于:
The second one can only be used in the context of an object literal:
The first form is executed as a statement. It is equivalent to:
第一个示例在全局范围内定义了一个函数,可以使用
init()
调用该函数。第二个定义了一个名为init
的对象的属性,它是右侧的函数声明。一般来说,第二个示例提供了一个较小的范围,您可以在其中执行该函数。
第一个示例允许您像这样调用函数:
init();
第二个示例更可能是这样:
The first example defines a function in the global scope which can be called with
init()
. The second defines a property of an object calledinit
that is the function declaration to the right.Generally, the second example provides a smaller scope in which you could execute the function.
First example allows you to call the function like so:
init();
And the second, more likely this:
尝试
在编写对象字面量时始终使用:。
当您尝试处理全局函数时,请将其设置为 var,因此:
请记住,每个函数都有其位置,但我个人喜欢为我正在处理的项目编写命名空间并使用对象文字。
现在,只有在设置对象后,这两个功能才在对象中可用。另一种方法有点草率,现在很少使用,但无论操作顺序如何都可以调用,因此可以在代码中设置它之前或之后调用它......同样,这在很多情况下被放弃。
Try to always use:
when writing an object literal.
When you're trying to process a global function set it as a var instead, so:
Remember, each has its place, but I've personally become fond of writing a namespace for the project I'm working on and using an object literal.
Now both of these will only be available in the object as soon as the object is set. The other method is a little sloppier and less used now, but can be called no matter the order of operations, so it can be called before or after it's set in the code... Again, this is being abandoned in a lot of cases.
函数声明
函数表达式
主要区别与 JavaScript 中的变量提升有关。您可以在这里阅读更多信息: http://www.adequatelygood.com/2010 /2/JavaScript-范围和提升 和 http://javascriptweblog.wordpress.com/2010/07/ 06/function-declarations-vs-function-expressions/
通过您的示例,我相信您也对在
object 中定义
。这是一个示例:匿名函数
感兴趣文字这将像这样使用:
在对象字面量中,使用
key: value
语法定义对象的不同属性,并使用逗号分隔它们。我建议您阅读这个关于 JavaScript 的免费系列 http://learn.appendto.com/lessons,它将为您解答许多此类问题,并为您成为 JS 开发人员奠定坚实的基础。
Function declaration
Function expression
The primary differences have to do with
Variable Hoisting
in JavaScript. You can read more here: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting and http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/By your example, I believe you are also interested in defining
anonymous functions
inobject literals
. Here is an example:This would be used like so:
In object literals different properties of the object are defined with a
key: value
syntax and use commas to separate them.I would recommend going through this free series on JavaScript http://learn.appendto.com/lessons, it will answer a lot of these questions for you and give you a solid base to becoming a JS developer.