javascript - function to es6 class base style -


i'm newie in es6 , eventemitter both. i've prepare module in node event base style , i'm trying transform es6 calass style. here is

// eventstyle.js

const events = require('events'); const util = require('util');  var customer = function() {   // console.log(typeof this);   events.eventemitter.call(this);   //    public function   this.register = function(email, password) {   var newcustomer = {email:email, password:password}   this.emit("newregistation", newcustomer)   }    var _validate = function(customer) {   if(customer.password=='password')   this.emit("validated", customer)   else   this.emit("registationfailed", customer)   }    var _insert = function(customer) {   this.emit("added", customer)   }    var _sendemail = function(customer) {   this.emit("emailsent", customer)   }    var _registationsuccessful = function(customer) {   this.emit("registationsuccessful", customer)   }    this.on("newregistation", _validate)   this.on("validated", _insert)   this.on("added", _sendemail)   this.on("emailsent", _registationsuccessful) }  util.inherits(customer, events.eventemitter ) 

module.exports = customer

//eventapp.js

const customer = require('./eventstyle'); customer = new customer(); // console.log(customer); customer.on("registationsuccessful", ()=>{   console.log("well done"); })  customer.on("registationfailed", ()=>{   console.log("sorry error"); }) console.log(typeof customer.register); settimeout(()=>customer.register(), 1000); 

//now es6 based code (not working me ) of eventstyle.js

const events = require('events'); const util = require('util');  class customer {   constuctor(){   console.log("cons",this);   events.eventemitter.call(this);   this.on("newregistation", _validate)   this.on("validated", _insert)   this.on("added", _sendemail)   this.on("emailsent", _registationsuccessful)   }    //    public function   register(email, password) {   var newcustomer = {email:email, password:password}   console.log(this);   this.emit("newregistation", newcustomer)   }    _validate(customer) {   if(customer.password=='password')   this.emit("validated", customer)   else   this.emit("registationfailed", customer)   }    _insert(customer) {   this.emit("added", customer)   }    _sendemail(customer) {   this.emit("emailsent", customer)   }    _registationsuccessful(customer) {   this.emit("registationsuccessful", customer)   } } util.inherits(customer, events.eventemitter )  module.exports =  customer 

somebody tell i'm mistaking. in advance

five 4 main issues:

  1. you've missed out first 'r' in constructor

  2. you need use this when referring object methods

  3. you need preserve this in callbacks (see how access correct this inside callback?). in specific case, don't; eventemitter's on calls them this set correctly (reference). if guarantee weren't provided, would.

  4. you need extends events on class line (the more normal thing call import eventemitter , use extends eventemitter).

  5. you don't use style call super constructor class events.eventemitter.call(this);. instead, call super. must before use of this.

so minimal changes version is

class customer extends events { 

and in constructor:

constructor() { // added missing 'r' in "constructor"     super();    // added     console.log("cons", this);     // removed `events.eventemitter.call(this);` here     // on 4 of following, note `this.`     this.on("newregistation", this._validate);     this.on("validated", this._insert);     this.on("added", this._sendemail);     this.on("emailsent", this._registationsuccessful); } 

but, if don't want _ methods public, there's no need them public; create them in constructor:

class customer {     constructor() {         super();         console.log("cons", this);          const _validate = customer => {             if (customer.password == 'password')                 this.emit("validated", customer);             else                 this.emit("registationfailed", customer);         };          const _insert = customer => {             this.emit("added", customer);         };          const _sendemail = customer => {             this.emit("emailsent", customer);         };          const _registationsuccessful = customer => {             this.emit("registationsuccessful", customer);         };          this.on("newregistation", _validate);         this.on("validated", _insert);         this.on("added", _sendemail);         this.on("emailsent", _registationsuccessful);     }      //    public function     register(email, password) {         var newcustomer = {             email: email,             password: password         }         console.log(this);         this.emit("newregistation", newcustomer)     } } 

that second form create separate function objects each instance, overhead of quite small. you'd need millions of instances of class matter.

you don't have create them separately, do:

this.on("newregistration", customer => {     if (customer.password == 'password')         this.emit("validated", customer);     else         this.emit("registationfailed", customer); }); 

...and on. if did, function anonymous, less helpful in stack traces , such if goes wrong. (whereas ones in const _validate = ... form have names.)


about calling super: if want pass along constructor parameters, can using rest notation:

constructor(...args) {     super(...args);     // ... } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -