Bhargav
Bhargav

Reputation: 95

Can someone explain this code in javascript where a function has formal parameter as an object

In a Vue component's methods I read this code where a function is defined this way

methods : {
   onEditorChange({ editor, html, text }) {
        console.log('editor change!', editor, html, text)
        this.content = html
   }
}

I checked the code and it is working. Can we declare formal parameters to a function like that ? You can find the code snippet in https://github.com/surmon-china/vue-quill-editor

Upvotes: 2

Views: 63

Answers (1)

xate
xate

Reputation: 6379

This is known as Destructuring.

From: http://2ality.com/2015/01/es6-destructuring.html#parameter-handling

In ECMAScript 5, you’d implement selectEntries() as follows:

function selectEntries(options) {
    options = options || {};
    var start = options.start || 0;
    var end = options.end || getDbLength();
    var step = options.step || 1;
    ···
}

In ECMAScript 6, you can use destructuring, which looks like this:

function selectEntries({ start=0, end=-1, step=1 }) {
    ···
};

Upvotes: 5

Related Questions