简体   繁体   中英

name:function(parms) - strange way to define a JavaScript function?

I'm looking at the Hello world Cordova application

For some reason, its defining (what looks like) functions like the below

 receivedEvent: function(id) {
       // blah
    },

I've haven't seen this before in JavaScript and can't find anything else doing functions like this. Can somebody explain is this doing something magically?

is it different to

function receivedEvent (id){
//blah
},

What you're seeing here is a method definition in an object or class.

const object = {
  hiThere: function() {
    console.log('hi')
  }
};

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions

There is a more commonly used syntax since ECMAScript 2015 which is the following.

const obj = {
  foo() {
    return 'bar';
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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