Javascript “反对(这个)”的目的是什么?
我正在研究数组'aJavascript “反对(这个)”的目的是什么?,javascript,node.js,Javascript,Node.js,我正在研究数组'afindpolyfill在MDN上的实现: 复制并粘贴到下面: // 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 ? ToO
find
polyfill在MDN上的实现:
复制并粘贴到下面:
// 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 new 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 new 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;
}
});
}
//https://tc39.github.io/ecma262/#sec-array.prototype.find
if(!Array.prototype.find){
Object.defineProperty(Array.prototype,“find”{
值:函数(谓词){
//1.设O为?ToObject(该值)。
if(this==null){
抛出新的TypeError(“'this'为null或未定义”);
}
var o=对象(此);
//2.设len为?ToLength(?Get(O,“length”))。
var len=o.length>>>0;
//3.如果IsCallable(谓词)为false,则抛出TypeError异常。
if(类型谓词!=='function'){
抛出新类型错误('谓词必须是函数');
}
//4.如果提供了thisArg,则T为thisArg;否则T为未定义。
var thisArg=参数[1];
//5.设k为0。
var k=0;
//6.当k
为什么我们需要做var o=Object(这个)代码>
对象(此)
实现了什么
谢谢大家的讨论。这是一个有趣的问题。。。谢谢你的帖子
老实说,Object(this)
让我有点惊讶,因为在this
可能是一个原始值的情况下,JavaScript似乎会强制任何对象(使用包装器)
如果我们尝试使用Function.prototype.bind()
更改此,JavaScript总是返回一个对象(函数就是一个对象):
在JavaScript中,我们知道当本机对象不是用作构造函数而是用作常规函数时,它们可能对类型转换很有用<代码>数字
、字符串
和布尔值
非常方便:
var num=1337,
str='',
布尔=真;
console.log(编号(str)、类型编号(str));
控制台日志(编号(bool)、编号类型(bool));
log(字符串(num)、字符串类型(num));
log(字符串(bool)、字符串类型(bool));
log(Boolean(num),typeof Boolean(num))
log(Boolean(str),typeof Boolean(str))代码>在严格模式下,原语此
不会被强制到对象
因此,使用对象(this)
显式强制对象是必要的
下面是一个更详细的示例:
const array=array.prototype;
Object.defineProperty(数组,'foo1',{value(){
返回this.length>>>0;}});
defineProperty(数组,'foo2',{value(){
“严格使用”;
返回this.length>>>0;}});
log(Array.prototype.foo1.call(未定义));
log(Array.prototype.foo2.call(未定义))
这样做的目的是处理此
为原语的情况。将其转换为对象。同意。包括null之类的值@这里已经处理了,null
和未定义的值:如果(this==null)抛出…
。o=Object(this)
部分只有在this
是一条字符串时才有意思,我一直在想this
是一个原语,而Object(this)
也将其转换为对象,但是this
实际上可能是一个原语吗?一个原语怎么称呼polyfill呢?即使可以,它的长度不是未定义的吗
,而代码的其余部分仍然有效吗1.length
抛出语法错误,但var num=1;num.length
不会抛出错误。@Thomas同意。我是在作更一般性的评论。问题并没有具体说明它是否只适用于此特定代码上下文中的用法,还是更一般的用法。谢谢您的详细回答。我自己也不知道他们到底为什么要使用对象(这个)
,这让我大惑不解。严格模式似乎是原因。我会接受这个答案,因为你的解释就像一个教程,一步一步地解释为什么这个谜题是一个谜题,非常详细,未来的读者很容易理解。注意未来的读者,除了这个,请阅读@torazaburo的答案。非常感谢你的解释。
var foo = function () {
console.log(this, typeof this);
}.bind('foo');
var bar = function () {
console.log(this, typeof this);
}.bind(1337);
var baz = function () {
console.log(this, typeof this);
}.bind(false);
var qux = function () {
console.log(this, typeof this);
}.bind(NaN);
var quux = function () {
console.log(this, typeof this);
}.bind(undefined);
var corge = function () {
console.log(this, typeof this);
}.bind(null);
var grault = function () {
console.log(this, typeof this);
}.bind([]);
var garply = function () {
console.log(this, typeof this);
}.bind({});
var waldo = function () {
console.log(this, typeof this);
}.bind(/regex/);
var fred = function () {
console.log(this, typeof this);
}.bind(function () {});
foo(); // String { 0: "f", 1: "o", 2: "o" } object
bar(); // Number { 1337 } object
baz(); // Boolean { false } object
qux(); // Number { NaN } object
quux(); // Window object
corge(); // Window object
grault(); // Array [ ] object
garply(); // Object { } object
waldo(); // /regex/ object
fred(); // function fred<() function
Array.prototype.foo = function () {
console.log(this, typeof this);
};
['foo'].foo(); // Array [ "foo" ] object
Array.prototype.foo.call('bar'); // String { 0: "b", 1: "a", 2: "r"} object
Array.prototype.foo.call(42); // Number { 42 } object
Array.prototype.foo.call(true); // Boolean { true } object
Array.prototype.foo.call(NaN); // Number { NaN } object
Array.prototype.foo.call(undefined); // Window object
Array.prototype.foo.call(null); // Window object
Array.prototype.foo.call({}); // Object { } object
Array.prototype.foo.call(/regex/); // /regex/ object
Array.prototype.foo.call(function () {}); // function () function