JavaScript 函数声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1866084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript function declaration
提问by Anand Shah
Are the JavaScript code snippets given below some sort of function declaration? If not can someone please give an overview of what they are?
下面给出的 JavaScript 代码片段是某种函数声明吗?如果没有,请有人概述一下它们是什么?
some_func = function(value) {
// some code here
}
and
和
show:function(value){
// some code here
}
采纳答案by Anand Shah
The first one is simply creating an anonymous function and assigning it to a variable some_func. So using some_func()will call the function.
第一个是简单地创建一个匿名函数并将其分配给一个变量some_func。所以 usingsome_func()会调用该函数。
The second one should be part of an object notation
第二个应该是对象符号的一部分
var obj = {
show:function(value){
// some code here
}
};
So, obj.show() will call the function
所以,obj.show() 会调用函数
In both cases, you are creating an anonymous function. But in the first case, you are simply assigning it to a variable. Whereas in the second case you are assigning it as a member of an object (possibly among many others).
在这两种情况下,您都在创建匿名函数。但在第一种情况下,您只是将它分配给一个变量。而在第二种情况下,您将其分配为对象的成员(可能包括许多其他对象)。
回答by Justin Johnson
There are sixways/contexts in which to create functions:
创建函数有六种方式/上下文:
1) Standard declarative notation (most familiar to people with C background)
1) 标准声明式表示法(C 背景的人最熟悉)
function foo() {}
All the rest are function expressions:
其余的都是函数表达式:
2) As a method of an object literal
2) 作为对象字面量的方法
var obj = {
foo: function() {}
};
3) As a method of an instantiated object (created each time newis exectued)
3)作为一个实例化对象的方法(每次执行时创建new)
var Obj = function() {
this.foo = function() {};
};
4) As a method of a prototype (created only once, regardless of how many times newis executed)
4)作为原型的方法(只创建一次,不管new执行多少次)
var Obj = function() {};
Obj.prototype.foo = function() {};
5) As an anonymous function with a reference (same effect as #1) *
5) 作为带引用的匿名函数(与#1 效果相同)*
var foo = function() {};
6) As an immediately executed anonymous function (completely anonymous)
6)作为立即执行的匿名函数(完全匿名)
(function() {})();
* When I look at this statement, I consider the result. As such, I don't really consider these as anonymous, because a reference is immediately created to the function and is therefore no longer anonymous. But it's all the same to most people.
* 当我看到这个陈述时,我会考虑结果。因此,我并不真正认为这些是匿名的,因为会立即创建对该函数的引用,因此不再是匿名的。但这对大多数人来说都是一样的。
回答by MBO
First is local (or global) variable with assigned anonymous function.
首先是分配了匿名函数的局部(或全局)变量。
var some_name = function(val) {};
some_name(42);
Second is property of some object (or function with label in front of it) with assigned anonymous function.
其次是分配了匿名函数的某个对象(或前面带有标签的函数)的属性。
var obj = {
show: function(val) {},
// ...
};
obj.show(42);
Functions are first-class citizens in JavaScript, so you could assign them to variables and call those functions from variable.
函数是 JavaScript 中的一等公民,因此您可以将它们分配给变量并从变量中调用这些函数。
You can even declare function with other name than variable which that function will be assigned to. It is handy when you want to define recursive methods, for example instead of this:
您甚至可以使用其他名称而不是该函数将分配给的变量来声明函数。当你想定义递归方法时它很方便,例如代替这个:
var obj = {
show: function(val) {
if (val > 0) { this.show(val-1); }
print(val);
}
};
you could write:
你可以写:
var obj = {
show: function f(val) {
if (val > 0) { f(val-1); }
print(val);
}
};
回答by jldupont
One way of doing it:
一种方法:
var some_func = function(value) {
// some code here
}
Another way:
其它的办法:
function some_funct() {
}
Yet another way:
还有一种方式:
var some_object={};
some_object["some_func"] = function() {};
or:
或者:
var some_object={};
some_object.some_func = function() {};
In other words, they are many ways to declare a function in JS.
换句话说,它们是在 JS 中声明函数的多种方式。
Your second example is not correct.
你的第二个例子不正确。
回答by yoda
The first one is a function declaration assigned to a variable (at least it should be, despite the fact that it's missing the variable type declaration first), the second one is probably related to a object declaration.
第一个是分配给变量的函数声明(至少应该是,尽管它首先缺少变量类型声明),第二个可能与对象声明有关。
回答by Sarfraz
They are called anonymous functions; you can read more about them here:
它们被称为匿名函数;你可以在这里阅读更多关于它们的信息:
http://www.ejball.com/EdAtWork/2005/03/28/JavaScriptAnonymousFunctions.aspx
http://www.ejball.com/EdAtWork/2005/03/28/JavaScriptAnonymousFunctions.aspx
回答by dxh
The first example creates a global variable (if a local variable of that name doesn't already exist) called some_func, and assigns a function to it, so that some_func()may be invoked.
第一个示例创建一个名为 的全局变量(如果该名称的局部变量尚不存在)some_func,并为其分配一个函数,以便some_func()可以调用它。
The second example is a function declaration inside an object. it assigns a function as the value of the showproperty of an object:
第二个例子是对象内的函数声明。它分配一个函数作为show对象属性的值:
var myObj = {
propString: "abc",
propFunction: function() { alert('test'); }
};
myObj.propFunction();
回答by Ei Maung
The first one...
第一个...
some_func = function(value) {
// some code here
}
is declaring a variable and assigned an anonymous functionto it, which is equivalent to...
正在声明一个变量并为其分配一个匿名函数,这相当于...
function some_func (value) {
// some code here
}
The second one should look like this...
第二个应该是这样的...
obj = {
show:function(value){
// some code here
}
}
// obj.show(value)
and equivalent to...
并相当于...
//pseudo code
class MyClass {
function show (value) {
// some code here
}
}
obj = new MyClass(); // obj.show(value)
Cheers
干杯

