1、pomelo handler默认支持ES6标准的class
app/servers/logic/remote/testHandler.js(正常调用test函数)
module.exports = function(app) {
return new TestHandler(app);
}
class TestHandler {
constructor(app) {
this.app = app;
}
test(info, msg, next) {
}
}
2、pomelo remote默认不支持ES6标准的class
app/servers/logic/remote/testRemote.js(找不到test函数)
module.exports = function(app) {
return new TestRemote(app);
}
class TestRemote {
constructor(app) {
this.app = app;
}
test(info, msg, next) {
}
}
3、原因
ES5标准中,原型链中的方法,都是可枚举的(enumerable)。
var Point = function (x, y) {
};
Point.prototype.toString = function() {
};
Object.keys(Point.prototype)
// ["toString"]
Object.getOwnPropertyNames(Point.prototype)
// ["constructor","toString"]
ES6标准中,类的内部所有定义的方法,都是不可枚举的(non-enumerable)。
class Point {
constructor(x, y) {
}
toString() {
}
}
Object.keys(Point.prototype)
// []
Object.getOwnPropertyNames(Point.prototype)
// ["constructor","toString"]
4、解决方法
pomelo-rpc/lib/util/proxy.js(修改函数genObjectProxy中遍历属性的方法即可)
var genObjectProxy = function(serviceName, origin, attach, proxyCB) {
//generate proxy for function field
var res = {};
/*
for (var field in origin) {
if (typeof origin[field] === 'function') {
res[field] = genFunctionProxy(serviceName, field, origin, attach, proxyCB);
}
}
*/
//修改Remote不支持ECMAScript6的问题,类的内部所有定义的方法,都是不可枚举的(non-enumerable)
var fieldArr = Object.getOwnPropertyNames(origin.__proto__);
for (let i=0; i<fieldArr.length; ++i) {
let field = fieldArr[i];
if (typeof origin[field] === 'function') {
res[field] = genFunctionProxy(serviceName, field, origin, attach, proxyCB);
}
}
return res;
};
网友评论