什么是基元数据
From the documentation we learn that JavaScript has six primitive types: undefined
, boolean
, number
, string
, bigint
, symbol
. If asked in an interview whether strings are primitives or objects, you might confidently answer “primitives”. And you’d be wrong.
从文档中我们了解到JavaScript具有六种原始类型: undefined
, boolean
, number
, string
, bigint
, symbol
。 如果在访谈中被问到字符串是原始元素还是对象,您可能会自信地回答“原始元素”。 而且你会错的。
A primitive data type is a basic type that cannot be broken down to a smaller data type. It is a building block for more complex types. The MDN Web Docs provides the list of the six primitive data types we have seen above but also gives the following definition:
原始数据类型是无法分解为较小数据类型的基本类型。 它是更复杂类型的构建块。 MDN Web文档提供了我们在上面看到的六种原始数据类型的列表,还提供了以下定义:
a primitive is data that is not an object and has no methods.
原语是不是对象且没有方法的数据。
But there are properties and methods on strings. If strings had no properties nor methods, this wouldn’t work:
但是字符串上有属性和方法。 如果字符串没有属性或方法,则将无法正常工作:
"Some string".length; // 11
"Some string".charAt(0); // "S""Some string".indexOf("t"); // 6
包装对象 (Wrapper Objects)
All primitive values exept null
and undefined
have object equivalents that wrap around them. Strings can therefore be represented by too types: the string
primitive type and String
objects. You can create an object from a primitive by using the constructor new String
and you get a primitive out of an object by calling its method valueOf
.
除null
和undefined
所有原始值都有环绕它们的对象等效项。 因此,字符串也可以用太类型来表示: string
原始类型和String
对象。 您可以使用构造函数new String
从基元创建对象,并通过调用对象的valueOf
方法从该对象中获取基元。
const a = "some string"; // "some string"const b = new String("some string"); // String {"some string"}
b.valueOf(); // "some string"
So primitive string
values actually don’t have any method nor property, but String
objects do. But there is no need to manually create a String
object using the constructor (and you shouldn’t) even when wanting to access to some method, JavaScript takes care of this for you under the hood.
因此原始string
值实际上没有任何方法或属性,而String
对象却没有。 但是,即使想要访问某些方法,也不需要使用构造函数手动创建String
对象(并且您不应该这样做),JavaScript会在后台为您解决这一问题。
强迫 (Coercion)
JavaScript is loosely typed and can convert values from one type to another. It does when you add a string
to a number
for example:
JavaScript是松散类型的,可以将值从一种类型转换为另一种类型。 例如,当您将string
添加到number
时,它会执行以下操作:
const c = "1";
const d = 2;c + d; // "12"
or when you compare them using the loose equality operator ==
:
或者当您使用宽松的相等运算符==
比较它们时:
const e = "1";
const f = 1;e == f; // true
Back to our strings, when you call a method of the String
object on a primitive value, JavaScript will implicitely convert it to an object. This leads to you being able to do this without having to think about it:
回到我们的字符串,当您在原始值上调用String
对象的方法时,JavaScript会将其隐式转换为对象。 这使您无需考虑即可执行此操作:
const a = "some string"; // "some string"
a.length; // 11
It also works the other way around, JavaScript will convert objects to primitives:
它也可以以其他方式工作,JavaScript会将对象转换为基元:
const a = "some string"; // "some string"
const b = new String("some string"); // String {"some string"}a + b; // "some stringsome string"
Strings can be primitives or objects, even if they are technically not both at the same time. When you create a string
with a simple assignment, which is the way you should always create your strings, you create a primitive value. In contexts where a method is to be called on a primitive string, for example when you call properties like length
or methods like indexOf
or chatAt
, JavaScript will automatically wrap an object around the primitive, creating a String
object and will call the method on it.
字符串可以是原语或对象,即使从技术上讲它们不是同时存在。 当您通过简单的分配创建string
时(这是您应始终创建字符串的方式),您将创建一个原始值。 在要在原始字符串上调用方法的上下文中,例如,当您调用诸如length
类的属性或诸如indexOf
或chatAt
类的方法时,JavaScript会自动在原始类型周围包装一个对象,创建一个String
对象并在其上调用该方法。
普通英语JavaScript (JavaScript In Plain English)
Enjoyed this article? If so, get more similar content by subscribing to Decoded, our YouTube channel!
喜欢这篇文章吗? 如果是这样,请订阅我们的YouTube频道解码,以获得更多类似的内容!
什么是基元数据