简体   繁体   English

javascript中不同类型的函数声明

[英]Different type of function declaration in javascript

1)Below is Function declaration with variable name 1)下面是带有变量名的函数声明

var x = function (a, b) {return a * b};

2)Below is another type of function in javascript in angular1 2)下面是angular1中javascript中的另一种函数

  var method = {
            add_category: function(data, success, failure) {
                $upload.upload({
                    url: baseUrl() + 'add_category',
                    data: data
                }).success(success).error(failure);
            },
fileupload: function(data, success, failure) 
            {   

                $upload.upload({
                    url: baseUrl() + 'ImageUpload',
                    data: data
                }).success(success).error(failure);
            }
 return method;
}

What is the difference between on both the code?两者上的代码有什么区别? first method values assign into one variable like this(var x = function (a, b)) (option 1) how second code(option 2) will works with semicolon like this(fileupload: function(data, success, failure))?第一种方法值分配到这样的一个变量中(var x = function (a, b)) (选项 1)第二个代码(选项 2)如何使用这样的分号(文件上传:函数(数据,成功,失败))? how data will assign in fileupload variable?数据将如何在文件上传变量中分配?

In JavaScript, a function is a first class citizen , exactly like a string, a number or a object.在 JavaScript 中,函数是一等公民,就像字符串、数字或对象一样。 Thus it can be assigned to a variable, and used everywhere a variable is expected.因此它可以分配给一个变量,并在任何需要变量的地方使用。

This notation:这个符号:

var obj = {
    item1: 10,
    item2: "string",
    item3: function (a,b) { return a*b; }
}

defines an object with three properties : a number, a string and a function.定义具有三个属性的对象:数字、字符串和函数。 This is equivalent to:这相当于:

var fn = function (a,b) { return a*b; };

var obj = {
    item1: 10,
    item2: "string",
    item3: fn
}

and you call it like this:你这样称呼它:

var prod = obj.item3(2,4); // returns 8

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

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