在Array.prototype.find's implementation中,为什么我们需要使用Object(this)将this转换为对象?
// https://tc39.github.io/ecma262/#sec-array.prototype.find
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
value: function(predicate) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw TypeError('predicate must be a function');
}
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];
// 5. Let k be 0.
var k = 0;
// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
// e. Increase k by 1.
k++;
}
// 7. Return undefined.
return undefined;
},
configurable: true,
writable: true
});
}发布于 2021-04-04 21:44:35
大多数情况下,是因为规范中这么写的。
但它为什么要这么做呢?因为稍后,它将访问值的属性(如.length和[k]),该值必须是该值的对象。当然,polyfill中使用的成员访问语法已经为我们做了这件事,但在规范文本中,Get算法需要一个对象,到对象的转换应该只发生一次,而不是在每个成员访问时(尽管这将是不可区分的)。
为什么我们要将任意值转换为对象?因为可以使用原始值调用方法,就像this参数一样--这很少见,但也是可能的。正如说明书所说的
注意:
find函数有意是泛型的;它不要求其this值必须是Array对象。因此,它可以被转移到其他类型的对象中作为方法使用。
举个例子:
String.prototype.find = Array.prototype.find;
console.log("test".find(x => x > 'f' && x < 't'))https://stackoverflow.com/questions/66941001
复制相似问题