我正在研究一个节点模块,我希望继续使用es6类语法来保持样式的一致性,但我发现这种模式无法重现:
const proto = module.exports = function(options) {
man.opts = options || {};
function man(sentence) {
man.say(sentence);
}
man.__proto__ = proto;
man.age = 29;
man.say = function(sentence) {
console.log(sentence);
};
return man;
};
这个函数的奇怪之处在于,我可以将它称为标准构造函数,并使用他的方法和道具获取一个人,但我也可以将man作为函数调用,并得到与调用他的方法"say“相同的结果。基本上,man('text')产生与man.say(‘text’)相同的效果;我如何使用es6类语法重新创建这个模式?
发布于 2016-11-10 13:46:35
基本上,
man('text')
产生了与man.say('text')
相同的效果
最好不要使用那种模式。
如何使用es6类语法重新创建此模式?
您可以使用类似于Function
的方法
export default class {
constructor(options) {
const man = sentence => this.say(sentence);
Object.setPrototypeOf(man, new.target.prototype);
man.opts = options || {};
man.age = 29;
return man;
}
say(sentence) {
console.log(sentence);
}
}
https://stackoverflow.com/questions/40536935
复制相似问题