[英]Javascript Manual: `Array.prototype.includes()` vs. `Array.includes()`
Incoming "noob" question:传入的“菜鸟”问题:
Javascript has an includes
method on arrays. Javascript 在 arrays 上有一个
includes
方法。
It looks like:看起来像:
Array.includes()
But when I go to the Javascript Manual to understand this method, the heading on that page (for an appropriate technical reason, I realize) is:但是当我 go 到Javascript 手册来理解这种方法时,该页面上的标题(出于适当的技术原因,我意识到)是:
Array.prototype.includes()
Similar things in the Javascript Manual have caused me to not like the manual at all (and alas, I rely on W3Schools more than the manual). Javascript 手册中的类似内容导致我根本不喜欢该手册(唉,我更依赖 W3Schools 而不是手册)。
However, I really really want to learn to interpret the manual.但是,我真的很想学习解释手册。
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()
时,在文档中包含.prototype
一词的意义是什么Array.prototype.includes()
) ?
(Also, if anyone has suggestions on how I can improve my comprehension of the official Javascript Manual, I'd appreciate suggestions.) (此外,如果有人对我如何提高对 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()
时,在文档中包含.prototype
一词的意义是什么Array.prototype.includes()
) ?
The significance is that actual usage doesn't look like Array.includes()
:重要的是实际用法看起来不像
Array.includes()
:
Array.includes();
That will throw a TypeError: Array.includes is not a function
because Array.includes
doesn't exist .这将引发
TypeError: Array.includes is not a function
因为Array.includes
doesn't exist 。 Accessing a non-existing property evaluates to undefined
, so Array.includes
evaluates to undefined
and thus Array.includes()
is trying to call undefined
as if were a function.访问不存在的属性计算结果为
undefined
,因此Array.includes
计算结果为undefined
,因此Array.includes()
试图调用undefined
,就像 function 一样。
You can see that in action here:你可以在这里看到它的作用:
console.log(Array.includes); undefined();
The includes()
method is defined on the prototype of the Array
global object so that you can call it on instances of Array
: includes()
方法是在Array
全局 object 的原型上定义的,因此您可以在Array
的实例上调用它:
[].includes();
You can see that [].includes
is a function:可以看到
[].includes
是一个 function:
console.log([].includes);
Compare this to Array.from
which is defined on the Array
constructor , not on the Array
prototype :将此与
Array.from
进行比较,后者是在Array
构造函数上定义的,而不是在Array
原型上:
console.log(Array.from);
You can use it like this:你可以像这样使用它:
console.log(Array.from({ length: 10 }, (_, num) => num << 2));
If the documentation said Array.includes()
you would literally type it like this (example):如果文档说
Array.includes()
你会像这样输入它(示例):
Array.includes(1);
Instead it says Array.prototype.includes()
which means it isn't called on the Array
type itself, but on an instance of it.相反,它说
Array.prototype.includes()
这意味着它不是在Array
类型本身上调用的,而是在它的一个实例上调用的。 So in this case you would write:所以在这种情况下,你会写:
const numbers = [1, 2, 3];
numbers.includes(1);
JavaScript is often described as prototype-based language, prototypes is simply how inheritance works in JavaScript. JavaScript 通常被描述为基于原型的语言,原型就是 inheritance 在 JavaScript 中的工作方式。
What does prototype means?原型是什么意思?
Both of us agree that almost everything in JavaScript is an object (I said "Almost" because primitives are not considered objects) cool?我们俩都同意 JavaScript 中的几乎所有内容都是 object (我说“几乎”,因为原语不被视为对象)很酷吗? Okay, now every Object in JS has an internal property called [[Prototype]] and by internal I mean that you can't access it directly the same way you access a JS object's property.
好的,现在 JS 中的每个 Object 都有一个名为 [[Prototype]] 的内部属性,内部我的意思是您不能像访问 JS 对象的属性一样直接访问它。
If we want to know the prototype of an object that we have created we either pass the instance of our object to Object.getPrototypeOf
or through the __proto__
property of our object If we want to know the prototype of an object that we have created we either pass the instance of our object to
Object.getPrototypeOf
or through the __proto__
property of our object
For example:例如:
let myArray = [1,2,3,4];
console.log(myArray.__proto__)
// Expected output: Array []
If you expand the resulting object you get from the little code snippet above you will find the includes method you were asking about and all of the methods available on any array you create in a JS code.如果您扩展生成的 object,您会从上面的小代码片段中找到您所询问的 include 方法以及您在 JS 代码中创建的任何数组上可用的所有方法。 That's because myArray and all arrays in JavaScript are said to share the properties and methods defined on Array.prototype!
那是因为据说 myArray 和 JavaScript 中的所有 arrays 共享 Array.prototype 上定义的属性和方法!
Now, if you look again at the methods of the resulting object which you have from the code snippet above you will notice a method called constructor, defined on the Array.prototype just like includes and the other methods现在,如果您再次查看上面代码片段中生成的 object 的方法,您会注意到一个名为构造函数的方法,它在 Array.prototype 上定义,就像 include 和其他方法一样
That's the function invoked when you create an instance of the JavaScript Array object!这就是在创建 JavaScript Array 对象的实例时调用的 function!
What do we mean by the JavaScript Array object? JavaScript 阵列 object 是什么意思? It's a global JavaScript object that is used in the construction of arrays, it's the Array in Array.prototype.includes() (you may call it a class for convenience buuuuut classes did not exist practically until the release of ES6...before then there was no such thing as class in JS)
It's a global JavaScript object that is used in the construction of arrays, it's the Array in Array.prototype.includes() (you may call it a class for convenience buuuuut classes did not exist practically until the release of ES6...before then JS中没有class这样的东西)
So to keep it simple and wrap it up think of Array as the global object that alll JS arrays are instances of, and think of Array.因此,为了简单起见,将 Array 视为全局 object,所有 JS arrays 都是其实例,并考虑 Array。 proto as it's prototype which wraps the properties and methods that all of its instances share!
proto因为它是封装了所有实例共享的属性和方法的原型!
And regarding the documentation, being able to read the documentation and have a considerable understanding of what you read is actually something good so I believe you're just fine !关于文档,能够阅读文档并对所阅读的内容有相当的了解实际上是一件好事,所以我相信你很好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.