JavaScript 函数有不同的语法吗?

发布于 2025-01-03 15:05:07 字数 392 浏览 0 评论 0原文

可能的重复: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧梦荧光笔 2025-01-10 15:05:08

第二种形式只能在对象文字的上下文中使用:

var myNamespace = {
    func1: function () {
        // do something
    },

    func2: function () {
        // do something else
    },

    someValue = 5
};

第一种形式作为语句执行。它相当于:

var init = function () {
    // do something
};

The second one can only be used in the context of an object literal:

var myNamespace = {
    func1: function () {
        // do something
    },

    func2: function () {
        // do something else
    },

    someValue = 5
};

The first form is executed as a statement. It is equivalent to:

var init = function () {
    // do something
};
残花月 2025-01-10 15:05:08

第一个示例在全局范围内定义了一个函数,可以使用 init() 调用该函数。第二个定义了一个名为 init 的对象的属性,它是右侧的函数声明。

一般来说,第二个示例提供了一个较小的范围,您可以在其中执行该函数。

第一个示例允许您像这样调用函数:

init();

第二个示例更可能是这样:

var thing = function() {
    init: function() { }
};

thing.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 called init 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 thing = function() {
    init: function() { }
};

thing.init();
红尘作伴 2025-01-10 15:05:08

尝试

init: function (){

}

在编写对象字面量时始终使用:。

当您尝试处理全局函数时,请将其设置为 var,因此:

var init = function(){

}

请记住,每个函数都有其位置,但我个人喜欢为我正在处理的项目编写命名空间并使用对象文字。

现在,只有在设置对象后,这两个功能才在对象中可用。另一种方法有点草率,现在很少使用,但无论操作顺序如何都可以调用,因此可以在代码中设置它之前或之后调用它......同样,这在很多情况下被放弃。

function init(){

}

Try to always use:

init: function (){

}

when writing an object literal.

When you're trying to process a global function set it as a var instead, so:

var init = function(){

}

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.

function init(){

}
洒一地阳光 2025-01-10 15:05:07

函数声明

function init() {

}

函数表达式

var init = function() {

};

主要区别与 JavaScript 中的变量提升有关。您可以在这里阅读更多信息: http://www.adequatelygood.com/2010 /2/JavaScript-范围和提升http://javascriptweblog.wordpress.com/2010/07/ 06/function-declarations-vs-function-expressions/

通过您的示例,我相信您也对在 object 中定义匿名函数感兴趣文字。这是一个示例:

//obj is the object name
var obj = {
    //init is the property name or key and the anonymous function is the value
    init: function() {
    },
    anotherProp: 'some value'
};

这将像这样使用:

obj.init();
alert(obj.anotherPorp);

在对象字面量中,使用 key: value 语法定义对象的不同属性,并使用逗号分隔它们。

我建议您阅读这个关于 JavaScript 的免费系列 http://learn.appendto.com/lessons,它将为您解答许多此类问题,并为您成为 JS 开发人员奠定坚实的基础。

Function declaration

function init() {

}

Function expression

var init = function() {

};

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 in object literals. Here is an example:

//obj is the object name
var obj = {
    //init is the property name or key and the anonymous function is the value
    init: function() {
    },
    anotherProp: 'some value'
};

This would be used like so:

obj.init();
alert(obj.anotherPorp);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文