proto作用
用法
今天在看chalk源码时,发现这么一段话:1
2
3
4
5
6
7
8
9
10
11
12
13
14function build(_styles) {
var builder = function () {
return applyStyle.apply(builder, arguments);
};
builder._styles = _styles;
builder.enabled = this.enabled;
// __proto__ is used because we must return a function, but there is
// no way to create a function with a different prototype.
/* eslint-disable no-proto */
builder.__proto__ = proto;
return builder;
}
用proto是因为要返回一个方法,而且要带上一个不同的原型。
实验
我们用以下方法来做个测试:
1 | var proto1 = defineProps(function chalk1() {}, { |
输出:
1 | ssssss |
参考资料:
create function in javascript with custom prototype
Is it possible to create a function with another prototype than Function.prototype?