【问题标题】:function to es6 class base stylees6 类基础样式的函数
【发布时间】:2018-01-27 05:48:45
【问题描述】:

我是 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);

//现在我的 eventStyle.js

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

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

【问题讨论】:

  • 你的问题是什么?
  • 也许 Babel 是你需要的。
  • @Sunday:正好相反。
  • 永远不要更改正在运行的系统...为什么需要课程?没有它就不能正常工作吗?
  • “现在我的基于 es6 的代码(不适合我)” 不工作如何?你有什么问题?发生什么了?你期望会发生什么?您看到了什么错误?

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


【解决方案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

【讨论】:

  • 错误:SyntaxError: 'super' 关键字在此处意外
  • 已修复,拼写错误:constuctor => constructor
  • 我不同意,只有扩展事件的类才会调用发出,所以这是调用者客户,始终是调用者客户。我只是做测试,this 仍然是我的实例化对象,不需要绑定
  • @Fefux:(如果你不@ 通知,我不会收到评论通知。)在这个具体的情况下,你是对的; EventEmitter guarantees 表示this 将由on 设置为实例。
  • 感谢@Fefux。你纠正了 TJ 和我,真的很可观
【解决方案2】:

五个四个主要问题:

  1. 您错过了constructor 中的第一个“r”

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

  3. 您需要在回调中保留this(请参阅How to access the correct this inside a callback?)。在这种特定情况下,您不需要; EventEmitteron 使用正确设置的 this (reference) 调用它们。如果不提供该保证,您会提供。

  4. class 行上需要 extends events(更正常的做法是调用导入 EventEmitter,然后使用 extends EventEmitter)。

  5. 您不要使用这种风格来调用具有class events.EventEmitter.call(this); 的超级构造函数。相反,您拨打super。在使用 this 之前,您必须这样做。

所以最小的改动版本是

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:如果你想传递构造函数参数,你可以使用rest notation来做到这一点:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2010-10-28
    相关资源
    最近更新 更多