答案是使用this.indexOf 的polyfill 版本不符合spec for String.prototype.includes,这允许this 可以转换为字符串:
如果 searchString 显示为 将此对象转换为字符串的结果的子字符串...
例如,this 到 includes 可以是一个数字:
<< String.prototype.includes.call(1, '1')
>> true
这类似于String.prototype.indexOf,根据spec 也不需要它的this 是一个字符串。
<< String.prototype.indexOf.call(1, '1')
>> 0
如果includes 是按照OP 建议的this.indexOf 实现的:
String.prototype.includes = function(searchString, position) {'use strict';
return this.indexOf(searchString, position) !== -1;
};
然后在规范允许的情况下,使用非字符串 this 调用 includes 会生成运行时错误:
<< String.prototype.includes.call(1, '1')
>> TypeError: undefined is not a function
而 MDN polyfill:
String.prototype.includes = function() {'use strict';
return String.prototype.indexOf.apply(this, arguments) !== -1;
};
工作正常,利用String.prototype.indexOf 的this 也不必是字符串这一事实:
<< String.prototype.includes.call(1, '1')
>> true
所以我认为 MDN polyfill 的编写方式不是为了防止 indexOf 方法在某些特定字符串对象上被覆盖,或者作为避免列出参数的简写,或者由于某些 Crockfordian 偏好prototype.apply 成语,而是为了正确实现规范。