简体   繁体   中英

Converting to ES6 syntax

I'm very new to ES6 and here is my first try. Looks like I only need to update the first line. However, I remember reading that 'this' should not be used. I'm confused. Hopefully you can fix the code for me. Thanks!

// current code

initialize: function initialize(options) {
  this.item = systemChannel.request('iteminfo:request');
  this.model = new someModel({
    someItem: options.someItem
  },
  {
    parse: true
  });
},

Then I'm trying to convert to ES6 syntax // es6 version

initialize(options) {
  this.item = systemChannel.request('iteminfo:request');
  this.model = new someModel({
    someItem: options.someItem
  },
  {
    parse: true
  });
},

Is this correct?

Looks technically valid to me. As for what you intend this to be a reference to, it's hard to say 100% without knowing on what you are declaring initialize , and if anything else is binding functions for you. Can you post a more full example?

Here's an example of how this works on objects with old/new syntaxes. You can run this on the babel repl online, if you want to try for yourself.

let obj = {
  fn() {
    return this;
  }
};

var obj2 = {
  fn: function() {
    return this;
  }
};

console.log(obj.fn() === obj); //=> true
console.log(obj2.fn() === obj2); //=> true

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