功能为es6类基础样式

时间:2017-08-18 13:52:31

标签: javascript node.js events ecmascript-6 eventemitter

我是es6和eventEmitter的新人。我已经准备了节点事件基础样式的模块,现在我正在尝试转换为es6 calass样式。这是

// eventStyle.js

const events = require('events');
const util = require('util');

var Customer = function() {
  // console.log(typeof this);
  events.EventEmitter.call(this);
  //    this is only 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 =客户

// 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);

//现在我的基于es6的代码(不适用于我) 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)
  }

  //    this is only 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

有人告诉我错了什么。提前谢谢

2 个答案:

答案 0 :(得分:2)

四个主要问题:

  1. 你错过了第一个' r'在constructor

  2. 在引用对象方法时需要使用this

  3. 您需要在回调中保留this(请参阅How to access the correct this inside a callback?)。在此特定的情况下,您不需要'吨; EventEmitter onthis设置正确(reference)的情况下调用它们。如果没有提供该保证,您可以。

  4. extends events行需要class(更常见的做法是调用导入EventEmitter然后使用extends EventEmitter)。< / p>

  5. 您不能使用此样式通过class events.EventEmitter.call(this);调用超级构造函数。相反,你打电话给super。您必须在使用this之前执行此操作。

  6. 所以最小变化版本是

    class Customer extends events {
    

    constructor

    constructor() { // Added missing 'r' in "constructor"
        super();    // Added
        console.log("cons", this);
        // Removed `events.EventEmitter.call(this);` here
        // On all four of the following, note `this.`
        this.on("newRegistation", this._validate);
        this.on("validated", this._insert);
        this.on("added", this._sendEmail);
        this.on("emailSent", this._registationSuccessful);
    }
    

    ,如果您不希望_方法公开,则不需要将其公开;只需在构造函数中创建它们:

    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);
        }
    
        //    this is only public function
        register(email, password) {
            var newCustomer = {
                email: email,
                password: password
            }
            console.log(this);
            this.emit("newRegistation", newCustomer)
        }
    }
    

    第二种形式确实为每个实例创建了单独的函数对象,但其开销非常小。你需要数以百万计的班级实例才能解决问题。

    您不必单独创建它们,您可以这样做:

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

    ......等等。但是如果你这样做了,那么这个函数就是匿名的,这在堆栈跟踪中没那么有用,如果出了什么问题。 (const _validate = ...形式为have names的那些。)

    关于调用super:如果要传递构造函数参数,可以使用其他表示法来执行此操作:

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

答案 1 :(得分:1)

差不多完成了,只缺少扩展到事件发射器并在构造函数中调用super来初始化超类(如果不这样做,this不存在):

const events = require('events');
const util = require('util');

class Customer extends events {
  constructor(){
  super();
  console.log("cons",this);
  this.on("newRegistation", this._validate)
  this.on("validated", this._insert)
  this.on("added", this._sendEmail)
  this.on("emailSent", this._registationSuccessful)
  }

  //    this is only 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)
  }
}

module.exports =  Customer