Javascript函数声明.冒号在函数声明中

Ben*_*nas 10 javascript

javascript中的函数声明"function test()"和"test:function()"有什么区别?

function test() {
    …
}
Run Code Online (Sandbox Code Playgroud)

VS

test: function() {
    …
}
Run Code Online (Sandbox Code Playgroud)


var functionName = function(){} vs function functionName(){}问题函数被声明为:

function test() {
    …
}
Run Code Online (Sandbox Code Playgroud)

var test = function() {
    …
};
Run Code Online (Sandbox Code Playgroud)

从语法角度来看,我的问题中的函数看起来并不相同.

Tus*_*har 11

function test()是正常的函数声明,您可以使用函数名称直接调用.虽然test: function()是在某个对象中定义的函数,但是必须使用定义它的对象来调用它.

功能声明

function test() {
    alert('In Test');
}

test(); // Calling test
Run Code Online (Sandbox Code Playgroud)

方法

var myObj = {
    test: function() {
        alert('Inside test');
    }
};

myObj.test(); // Calling test
Run Code Online (Sandbox Code Playgroud)


Jer*_*lle 6

考虑这个javascript对象:

{ "name" : "Joe",
  "age" : "23"}
Run Code Online (Sandbox Code Playgroud)

Javascript被弱化,你可以用23(数字)替换"23"(字符串):

{ "name" : "Joe",
  "age" : 23}
Run Code Online (Sandbox Code Playgroud)

没有错误,完美无缺.

实际上,您可以用其他任何东西替换23:布尔值

{ "name" : "Joe",
  "age" : true}
Run Code Online (Sandbox Code Playgroud)

另一个对象

{ "name" : "Joe",
  "age" : {"2014" : 22 , "2015": 23 } }
Run Code Online (Sandbox Code Playgroud)

甚至是一个功能

{ "name" : "Joe",
  "age" : function(){ alert("23");} }
Run Code Online (Sandbox Code Playgroud)

旁注:有些人因为如此宽松而讨厌Javascript.其他人(像我一样)喜欢Javascript也是出于同样的原因,因为这种灵活性就是它的强大功能(而且是异步的).

您可以将该对象命名为"人"并询问他的姓名和年龄:

var person = { "name" : "Joe",
      "age" : function(){ alert("23");} }

console.log( person.name ); // will log "Joe"
person.age(); // "age" is a function, so you need to call it. It will alert 23.
Run Code Online (Sandbox Code Playgroud)

现在您可以创建一个将返回该对象的函数:

function Person() {
    return{
      "name" : "Joe",

      "age" : function(){ alert("23");},

      sayHello : function() {
        alert("Hello");
      },

      sleep : function() {
        alert("I'm sleeping");
      }
    }
};

console.log( Person().name ); // logs "Joe"
Person().age(); // alerts "23"
Person().sayHello(); // alerts "Hello"
Person().sleep(); // alerts "I'm sleeping".
Run Code Online (Sandbox Code Playgroud)

age,sayHello并且sleep是功能,这被称为方法Person的功能.

一个人通常会避免Person()多次调用,new Person而是创建一个代替:

var person = new Person();
person.sayHello(); // alerts "Hello"
person.sleep(); // alerts "I'm sleeping".
Run Code Online (Sandbox Code Playgroud)

此方法允许通过传递参数创建许多人:

function Person(name, age) {
    return{
        "name" : name,

        "age" : function(){ alert(age);},

        sayHello : function() { // sayHello or "sayHello", both work
          alert("Hello, my name is "+ this.name );
        },

        sleep : function() {
          alert("I'm sleeping");
        }
     }
};

var person = new Person("John", 25);
person.sayHello(); // alerts "Hello, my name is John"
person.age(); // alerts "25".
Run Code Online (Sandbox Code Playgroud)

此方法目前替换Javascript 5(EcmaScript 5)缺少的类.但是EcmaScript 6很快就会推出适当的课程.

  • 这是JS课程的一个很好的介绍,但它没有回答OP的问题. (3认同)