Javascript 手册:`Array.prototype.includes()` 与 `Array.includes()`
Javascript Manual: `Array.prototype.includes()` vs. `Array.includes()`
收到"noob"问题:
Javascript 在数组上有一个 includes
方法。
看起来像:
Array.includes()
但是当我转到 Javascript Manual 了解此方法时,该页面上的标题(出于适当的技术原因,我意识到)是:
Array.prototype.includes()
Javascript 手册中的类似内容导致我根本不喜欢该手册(唉,我更依赖 W3Schools 而不是该手册)。
不过,我真的很想学习解读手册。
所以,我的问题是:文档中 Array.prototype.includes()
中包含单词 .prototype
的意义是什么,实际用法如下:Array.includes()
?
(此外,如果有人对我如何提高对官方 Javascript 手册的理解提出建议,我将不胜感激。)
So, my question is: what's the significance of including the word .prototype
in Array.prototype.includes()
in the documentation, when the actual usage looks like: Array.includes()
?
重要的是实际用法并不看起来像Array.includes()
:
Array.includes();
这将抛出一个 TypeError: Array.includes is not a function
because Array.includes
doesn't exist. Accessing a non-existing property evaluates to undefined
,因此 Array.includes
的计算结果为 undefined
,因此 Array.includes()
试图调用 undefined
就好像是一个函数。
您可以在此处看到实际效果:
console.log(Array.includes);
undefined();
The includes()
method is defined on the prototype of the Array
global object 这样您就可以在 Array
:
的 个实例 上调用它
[].includes();
可以看到[].includes
是一个函数:
console.log([].includes);
将此与 Array.from
进行比较,后者是在 Array
构造函数 上定义的,而不是在 Array
原型:
console.log(Array.from);
你可以这样使用它:
console.log(Array.from({ length: 10 }, (_, num) => num << 2));
如果文档说 Array.includes()
你会像这样输入它(示例):
Array.includes(1);
相反它说 Array.prototype.includes()
这意味着它不是在 Array
类型本身上调用,而是在它的一个实例上调用。所以在这种情况下你会写:
const numbers = [1, 2, 3];
numbers.includes(1);
JavaScript 通常被描述为基于原型的语言,原型只是继承在 JavaScript.
中的工作方式
原型是什么意思?
我们都同意 JavaScript 中的几乎所有东西都是对象(我说 "Almost" 因为基元不被认为是对象)酷吗?
好的,现在 JS 中的每个对象都有一个名为 [[Prototype]] 的内部 属性 并且内部我的意思是你不能像访问 JS 对象的 属性.[= 一样直接访问它。 13=]
如果我们想知道我们创建的对象的原型,我们要么将对象的实例传递给 Object.getPrototypeOf
,要么通过对象的 __proto__
属性
例如:
let myArray = [1,2,3,4];
console.log(myArray.__proto__)
// Expected output: Array []
如果您展开从上面的小代码片段中获得的结果对象,您将找到您询问的 includes 方法以及您在 JS 代码中创建的任何数组上可用的所有方法!
这是因为据说 myArray 和 JavaScript 中的所有数组共享 Array.prototype!
中定义的属性和方法
现在,如果您再次查看上面代码片段中生成的对象的方法,您会注意到一个名为构造函数的方法,它在 Array.prototype 上定义,就像 includes 和其他方法
这是创建 JavaScript 数组对象实例时调用的函数!
JavaScript 数组对象是什么意思?
它是一个用于构造数组的全局 JavaScript 对象,它是 Array.prototype.includes() 中的数组(为方便起见,您可以将其称为 class buuuuut class es 在 ES6 发布之前实际上并不存在...在此之前,JS 中没有 class)
因此,为了保持简单并将其包装起来,将 Array 视为所有 JS 数组实例的全局对象,并将 Array.proto 视为包装的原型所有实例共享的属性和方法!
关于文档,能够阅读文档并对所阅读的内容有相当的了解实际上是一件好事,所以我相信你很好!
收到"noob"问题:
Javascript 在数组上有一个 includes
方法。
看起来像:
Array.includes()
但是当我转到 Javascript Manual 了解此方法时,该页面上的标题(出于适当的技术原因,我意识到)是:
Array.prototype.includes()
Javascript 手册中的类似内容导致我根本不喜欢该手册(唉,我更依赖 W3Schools 而不是该手册)。
不过,我真的很想学习解读手册。
所以,我的问题是:文档中 Array.prototype.includes()
中包含单词 .prototype
的意义是什么,实际用法如下:Array.includes()
?
(此外,如果有人对我如何提高对官方 Javascript 手册的理解提出建议,我将不胜感激。)
So, my question is: what's the significance of including the word
.prototype
inArray.prototype.includes()
in the documentation, when the actual usage looks like:Array.includes()
?
重要的是实际用法并不看起来像Array.includes()
:
Array.includes();
这将抛出一个 TypeError: Array.includes is not a function
because Array.includes
doesn't exist. Accessing a non-existing property evaluates to undefined
,因此 Array.includes
的计算结果为 undefined
,因此 Array.includes()
试图调用 undefined
就好像是一个函数。
您可以在此处看到实际效果:
console.log(Array.includes);
undefined();
The includes()
method is defined on the prototype of the Array
global object 这样您就可以在 Array
:
[].includes();
可以看到[].includes
是一个函数:
console.log([].includes);
将此与 Array.from
进行比较,后者是在 Array
构造函数 上定义的,而不是在 Array
原型:
console.log(Array.from);
你可以这样使用它:
console.log(Array.from({ length: 10 }, (_, num) => num << 2));
如果文档说 Array.includes()
你会像这样输入它(示例):
Array.includes(1);
相反它说 Array.prototype.includes()
这意味着它不是在 Array
类型本身上调用,而是在它的一个实例上调用。所以在这种情况下你会写:
const numbers = [1, 2, 3];
numbers.includes(1);
JavaScript 通常被描述为基于原型的语言,原型只是继承在 JavaScript.
中的工作方式原型是什么意思?
我们都同意 JavaScript 中的几乎所有东西都是对象(我说 "Almost" 因为基元不被认为是对象)酷吗? 好的,现在 JS 中的每个对象都有一个名为 [[Prototype]] 的内部 属性 并且内部我的意思是你不能像访问 JS 对象的 属性.[= 一样直接访问它。 13=]
如果我们想知道我们创建的对象的原型,我们要么将对象的实例传递给 Object.getPrototypeOf
,要么通过对象的 __proto__
属性
例如:
let myArray = [1,2,3,4];
console.log(myArray.__proto__)
// Expected output: Array []
如果您展开从上面的小代码片段中获得的结果对象,您将找到您询问的 includes 方法以及您在 JS 代码中创建的任何数组上可用的所有方法! 这是因为据说 myArray 和 JavaScript 中的所有数组共享 Array.prototype!
中定义的属性和方法现在,如果您再次查看上面代码片段中生成的对象的方法,您会注意到一个名为构造函数的方法,它在 Array.prototype 上定义,就像 includes 和其他方法
这是创建 JavaScript 数组对象实例时调用的函数!
JavaScript 数组对象是什么意思? 它是一个用于构造数组的全局 JavaScript 对象,它是 Array.prototype.includes() 中的数组(为方便起见,您可以将其称为 class buuuuut class es 在 ES6 发布之前实际上并不存在...在此之前,JS 中没有 class)
因此,为了保持简单并将其包装起来,将 Array 视为所有 JS 数组实例的全局对象,并将 Array.proto 视为包装的原型所有实例共享的属性和方法!
关于文档,能够阅读文档并对所阅读的内容有相当的了解实际上是一件好事,所以我相信你很好!