简体   繁体   English

JavaScript函数声明

[英]JavaScript function declaration

Are the JavaScript code snippets given below some sort of function declaration? 下面给出了某些函数声明的JavaScript代码片段吗? If not can someone please give an overview of what they are? 如果不是,有人可以概述一下它们是什么吗?

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

and

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

There are six ways/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 new is exectued) 3)作为实例化对象的方法(每次new被执行时创建)

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

4) As a method of a prototype (created only once, regardless of how many times new is 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. 但对大多数人来说都是一样的。

The first one is simply creating an anonymous function and assigning it to a variable some_func . 第一个是简单地创建一个匿名函数并将其分配给变量some_func So using some_func() will call the function. 所以使用some_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). 而在第二种情况下,您将其指定为对象的成员(可能在许多其他对象中)。

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);
    }
};

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. 你的第二个例子不正确。

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

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

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 show property of an object: 它将一个函数指定为对象的show属性的值:

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

myObj.propFunction();

The first one... 第一个...

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

is declaring a variable and assigned an anonymous function to 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 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM