Javascript es6类基本样式的函数
我是es6和eventEmitter的新手。我已经准备了一个节点事件库样式的模块,现在我正在尝试转换为es6 calass样式。给你 //eventStyle.jsJavascript es6类基本样式的函数,javascript,node.js,events,ecmascript-6,eventemitter,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); // t
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 )
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);
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
module.exports=客户
//eventApp.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 )
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);
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
//现在我的eventStyle.js基于es6的代码(不适用于我)
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 )
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);
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
有人告诉我我弄错了什么。提前感谢即将完成,missing仅扩展到事件发射器并在构造函数中调用super来初始化super类(如果您不这样做,
此
不存在):
差不多完成了,missing只扩展到事件发射器,并在构造函数中调用super来初始化super类(如果您不这样做,
这个
不存在):
五、四个主要问题:
构造函数中的第一个“r”
此
此
(请参阅)。在这种特定的情况下,您不需要EventEmitter
的on
使用正确设置调用它们()。如果不提供担保,你会的
类
行上使用扩展事件
(更正常的做法是调用导入事件发射器
,然后使用扩展事件发射器
)类事件.EventEmitter.call(this)的超级构造函数代码>。相反,您可以调用super
。在使用此
之前,必须先执行此操作
class Customer extends events {
在构造函数中
:
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);
}
但是,如果你不想让\uu
方法公开,就不需要公开它们;只需在构造函数中创建它们:
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\u validate=…
格式中的那些。)
关于调用
super
:如果要传递构造函数参数,可以使用rest表示法:
constructor(...args) {
super(...args);
// ...
}
五、四个主要问题:
构造函数中的第一个“r”
此
此
(请参阅)。在这种特定的情况下,您不需要EventEmitter
的on
使用正确设置调用它们()。如果不提供担保,你会的
类
行上使用扩展事件
(更正常的做法是调用导入事件发射器
,然后使用扩展事件发射器
)类事件.EventEmitter.call(this)的超级构造函数代码>。相反,您可以调用super
。在使用此
之前,必须先执行此操作
class Customer extends events {
在构造函数中
:
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);
}
但是,如果你不想让\uu
方法公开,就不需要公开它们;只需在构造函数中创建它们:
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\u validate=…
格式中的那些。)
关于调用
super
:如果要传递构造函数参数,可以使用rest表示法:
constructor(...args) {
super(...args);
// ...
}
那个么你们的问题是什么?也许巴贝尔就是你们需要的。@Sunday:有点相反。永远不要改变跑步系统。。。你为什么需要上课?没有“现在我的基于es6的代码(不适用于我)”就不能正常工作吗?你有什么问题?会发生什么?你认为会发生什么?你们看到了什么错误?那个你们的问题是什么?也许巴贝尔就是你们所需要的。@Sunday:有点相反。永远不要改变运行系统。。。你为什么需要上课?没有“现在我的基于es6的代码(不适用于我)”就不能正常工作吗?你有什么问题?会发生什么?你认为会发生什么?您看到了什么错误?错误:SyntaxError:“super”关键字此处意外已修复,键入错误:
constructor
=>constructor
我不同意,只有扩展事件的类将调用emit,因此这是调用方客户,始终是调用方客户。我只是做了测试,这个
仍然是我的实例化对象,不需要bind@Fefux:(如果你不@
通知,我就不会收到评论的通知。)在这种情况下,你是对的;此
将由上的设置为实例。谢谢@Fefux。你纠正了TJ&me,非常明显的错误:SyntaxError:“super”关键字在此意外修复,打字错误:constructor
=>constructor
我不同意,只有扩展事件的类才会调用emit,所以这是调用方客户,始终是调用方客户。我只是做了测试,这个
仍然是我的实例化对象,不需要bind@Fefux:(如果您不@
通知,我就不会收到通知