27

JavaScript 代码片段是否在某种函数声明下方给出?如果没有,有人可以概述一下它们是什么吗?

some_func = function(value) {
    // some code here
}

show:function(value){
   // some code here
}
4

8 回答 8

72

六种创建函数的方式/上下文:

1) 标准的声明式表示法(有 C 背景的人最熟悉)

function foo() {}

其余的都是函数表达式

2) 作为对象字面量的方法

var obj = {
    foo: function() {}
};

3)作为实例化对象的方法(每次执行时创建new

var Obj = function() {
    this.foo = function() {};
};

4)作为原型的方法(只创建一次,不管执行多少次new

var Obj = function() {};
Obj.prototype.foo = function() {};

5) 作为带有引用的匿名函数(与#1 效果相同)*

var foo = function() {};

6)作为立即执行的匿名函数(完全匿名)

(function() {})();

* 当我看这个陈述时,我考虑了结果。因此,我并不认为这些是匿名的,因为会立即创建对该函数的引用,因此不再是匿名的。但这对大多数人来说都是一样的。

于 2009-12-08T10:55:43.757 回答
25

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.

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

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).

于 2009-12-08T10:37:03.880 回答
4

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.

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);
    }
};
于 2009-12-08T10:36:31.040 回答
2

一种方法:

var some_func = function(value) {  
    // some code here
}

另一种方式:

function some_funct() {
}

还有一种方式:

var some_object={};
some_object["some_func"] = function() {};

或者:

var some_object={};
some_object.some_func = function() {};

换句话说,它们是在 JS 中声明函数的多种方式。


你的第二个例子不正确。

于 2009-12-08T10:35:27.743 回答
1

第一个是分配给变量的函数声明(至少应该是,尽管它首先缺少变量类型声明),第二个可能与对象声明有关。

于 2009-12-08T10:35:59.887 回答
1

它们被称为匿名函数;你可以在这里阅读更多关于它们的信息:

http://www.ejball.com/EdAtWork/2005/03/28/JavaScriptAnonymousFunctions.aspx

于 2009-12-08T10:36:18.280 回答
1

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.

The second example is a function declaration inside an object. it assigns a function as the value of the show property of an object:

var myObj = {
    propString: "abc",
    propFunction: function() { alert('test'); }
};

myObj.propFunction();
于 2009-12-08T10:38:14.873 回答
0

第一个...

some_func = function(value) {  
    // some code here
}

正在声明一个变量,并为其分配了一个匿名函数,相当于...

function some_func (value) {  
    // some code here
}

第二个应该是这样的...

obj = {
    show:function(value){
       // some code here
    }
}
// obj.show(value)

相当于...

//pseudo code
class MyClass {
    function show (value) {
        // some code here
    }
}
obj = new MyClass();    // obj.show(value)

干杯

于 2009-12-08T10:46:34.187 回答