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:
you've missed out first 'r' in
constructor
you need use
this
when referring object methodsyou need preservein specific case, don't;this
in callbacks (see how access correctthis
inside callback?).eventemitter
'son
calls themthis
set correctly (reference). if guarantee weren't provided, would.you need
extends events
onclass
line (the more normal thing call importeventemitter
, useextends eventemitter
).you don't use style call super constructor
class
events.eventemitter.call(this);
. instead, callsuper
. must before use ofthis
.
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
Post a Comment