2

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

3
  • new javascript ES2016+ Commented Nov 3, 2017 at 10:57
  • 1
    This is ES6 syntax. Look up object destructing. Commented Nov 3, 2017 at 10:58
  • Which part exactly are you unclear on, { editor, html, text }? Commented Nov 3, 2017 at 10:58

1 Answer 1

5

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 }) {
    ···
};
Sign up to request clarification or add additional context in comments.

2 Comments

Understood the concept you were telling here
@Bhargav Yeah that page looks like a good intro into the concept. Thanks for sharing :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.