JavaScript基本类型和引用类型(译文)
By S.F.
本文链接 https://www.kyfws.com/news/javascript-primitive-reference-types/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 8 分钟阅读 - 3888 个词 阅读量 0JavaScript基本类型和引用类型(译文)
原文地址:https://www.codeproject.com/Articles/5278758/JavaScript-Primitive-Reference-Types
译文由本站翻译
前言
Builtin types of JavaScript and difference between primitive and reference types JavaScript的内置类型以及原始类型和引用类型之间的区别 This article will discuss the builtin types of JavaScript and the difference between primitive and reference types. 本文将讨论JavaScript的内置类型以及原始类型和引用类型之间的区别.
(Introduction) 介绍
(In my perspective, it is crucial to understand the types of a certain programming language because it is the building block. That’s why in this article, we will see the builtin types of JavaScript and the difference between primitive and reference types.) 在我看来,理解某种编程语言的类型至关重要,因为它是构建模块.因此,在本文中,我们将看到JavaScript的内置类型以及原始类型和引用类型之间的区别. (Moreover, coming from a static-typed language like C# and jumping to JavaScript, I became confused due to its dynamic behavior. The funny thing was I was looking for classes and realized that it has no formal support () 而且,来自诸如C#之类的静态类型语言并跳转到JavaScript,由于其动态行为,我感到困惑.有趣的是,我正在寻找课程,但意识到它没有正式的支持((Classes, as you may know, from languages like Java/C#, don’t technically exist in JavaScript) 您可能知道,Java/C#之类的类在技术上并不存在于JavaScript中(). It was strange at first but I became accustomed. Thus, I decided to create this article to help students or developers alike to share my knowledge and journey learning JavaScript types.) ).起初很奇怪,但我已经习惯了.因此,我决定创建这篇文章,以帮助学生或开发人员分享我的知识并学习JavaScript类型.
(Background) 背景
(When I started with JavaScript, many fantastic developers argue that JavaScript doesn’t have types. Particularly those developers who came from strongly-typed languages like C#, Java, and C++. Back then, I honestly don’t know what to say because I’m not that knowledgeable yet. However; today if you ever encountered or will encounter these kinds of developers, you can point them to the JavaScript language specification. I’ll give the link below and some screenshot that states that the JavaScript language does have types.) 当我开始使用JavaScript时,许多出色的开发人员都认为JavaScript没有类型.特别是那些来自强类型语言(例如C#,Java和C ++)的开发人员.那时,老实说,我还不知道该说些什么,因为我还不那么了解.然而;今天,如果您曾经遇到过或将要遇到这类开发人员,则可以将其指向JavaScript语言规范.我将在下面提供链接和一些屏幕截图,其中指出JavaScript语言确实具有类型. (As of this writing, we have the “ECMAScript 2020 Language Specification”, which can be found) 在撰写本文时,我们拥有" ECMAScript 2020语言规范",该规范可在此处找到. (here) 这里 (. Lastly, let us see what the specification has to say concerning types. Please see the screenshot below:) .最后,让我们看看规范对类型必须说些什么.请查看以下屏幕截图:
(I hope that saves your day. Now, enough of that, let’s jump into the JavaScript built-in types.) 希望这可以节省您的时间.现在,让我们进入JavaScript内置类型.
(JavaScript Data Types) JavaScript数据类型
(The JavaScript language has two kinds of types: primitive types and reference types. Primitive types () JavaScript语言有两种类型:基本类型和引用类型.基本类型((termed as value types too) 也被称为值类型() are stored as simple data types while reference types are stored as objects, which means it holds a reference to a memory’s location.) )存储为简单数据类型,而引用类型存储为对象,这意味着它保存了对内存位置的引用. (When using JavaScript and dealing with its different types, primitive and reference they may seem the same but it isn’t. Correct? You can answer on the comment section below. Moreover, to make the language consistent, it lets you treat primitive types as reference types.) 使用JavaScript并处理其不同类型,原始和引用时,它们看起来可能相同,但事实并非如此.正确?您可以在下面的评论部分进行回答.此外,为了使语言一致,您可以将原始类型视为引用类型. (Here is the list of JavaScript built-in) 这是内置JavaScript的列表**(*types*) 类型**(*:*) :
-
boolean
-
number
-
bigint
-
string
-
null
-
undefined
-
symbol
-
object
(Primitive Types) 基本类型
Types | Description | Remarks |
---|---|---|
Boolean |
true or false |
1 or 0 |
Number |
(Any integer or floating-point numeric value. Any double precision IEEE 754 number.) 任何整数或浮点数值.任何双精度IEEE 754数字. | |
BigInt |
(Large integers even beyond the safe integer limit for) 大整数甚至超出了安全整数限制 Number . |
A BigInt (is created by appending) 通过附加创建 n (to the end of an integer.) 到整数的末尾.(You can check) 你可以检查 here (if it is supported by your browser.) 如果您的浏览器支持它. |
String |
(UTF-16 string. The sequence of characters delimited by either single or double-quotes.) UTF-16字符串.由单引号或双引号分隔的字符序列. | |
Null |
(Special primitive-type that has only one value,) 仅具有一个值的特殊基本类型, null . |
(I called it special because it is, will discuss this in a later section (Why) 我之所以称其为特殊,是因为它将在后面的部分中进行讨论(为什么 null (returns object?).) 返回对象?). |
Undefined |
(A primitive type that has only once value, undefined.) 仅具有一次值(未定义)的原始类型. | The undefined (keyword is the value assigned to a variable that wasn’t initialized.) 关键字是分配给尚未初始化的变量的值. |
Symbol | (A unique and immutable value.) 独特而不变的价值. | (For more information about Symbol, you can read more) 有关Symbol的更多信息,您可以阅读更多 here . |
(Let us see some examples of primitive types below:) 让我们在下面看到一些原始类型的示例:
//boolean
var iLoveJavaScript = true;
//number
var yearsOfCoding = 10;
//bigint
var bigIntSample = 1234567890987654321n;
//string
var programmerName = "Jin Vincent Necesario";
//null
var doesntPointToAny = null;
//undefined
var badCompany; //undefined
(How Primitive Types Behave?) 原始类型的行为如何?
(Primitive types are always assigned, the value is copied.) 始终分配原始类型,然后复制值. (To practically see the behavior, let’s look at the example below:) 要实际查看行为,请看下面的示例:
var name1 = "Jin";
var name2 = name1;
console.log(name1); //Jin
console.log(name2); //Jin
name2 = "Vincent";
console.log(name1); //Jin
console.log(name2); //Vincent
(If you would like to see what’s happening, please see Figure 1 below:) 如果您想了解发生了什么,请参见下面的图1:
(Figure 1) 图1(As you can see, the variable) 如您所见,变量 name1
(and) 和 name2
(are completely separate from each other, and you can change the value in) 彼此完全分开,您可以在中更改值 name2
(without affecting) 不影响 name1
(and vice versa.) 反之亦然.
(Using the typeof Operator) 使用typeof运算符
(The typeof Syntax) 语法类型
//typeof operand
//or
//typeof(operand)
(The) 的 typeof
(operator is the best way to identify primitive types that returns the type in) 运算符是识别返回类型的原始类型的最佳方法 string
(format. The operand is the expression representing the primitive or reference type.) 格式.操作数是表示基本类型或引用类型的表达式.
(Let us see some examples below:) 让我们看下面的一些例子:
//typeof operator without using the open and close parentheses
console.log(typeof true); //boolean
console.log(typeof 10); //number
console.log(typeof bigIntSample); //bigint
console.log(typeof "Jin Vincent Necesario"); //string
console.log(typeof null); //object -> oops! more about this later
console.log(typeof undefined); //undefined
//end typeof operator without using the open and close parentheses
//typeof operator using the open and close parentheses
console.log(typeof(true)); //boolean
console.log(typeof(10)); //number
console.log(typeof(bigIntSample)); //bigint
console.log(typeof("Jin Vincent Necesario")); //string
console.log(typeof(null)); //object -> oops! more about this later
console.log(typeof(undefined)); //undefined
//end typeof operator using the open and close parentheses
(Why null returns object?) 为什么null返回对象?
(OK, you have probably run the code samples above and wondering why the) 好的,您可能已经运行了上面的代码示例,并且想知道为什么 null
(data-type returns) 数据类型返回 object
(. This is the tricky part of the language, even me I was confused at first but no worries will answer that here.) .这是语言的棘手部分,即使是我,我一开始也很困惑,但是在这里没有任何疑问可以回答.
(The truth, it is a bug and it has been acknowledged by) 事实是,这是一个错误,并且已经被(TC39) TC39(, the committee that maintains the JavaScript language. One reason this bug wasn’t fixed because the proposed fix broke a number of existing sites. Thus, the error remained. For more information about it, you can read more) ,是维护JavaScript语言的委员会.由于所建议的修复程序破坏了许多现有站点,因此未修复此错误的原因之一.因此,错误仍然存在.有关更多信息,您可以阅读更多 (here) 这里 (.) .
(Now, that we have an idea of why it behaves that way. What’s the best way to determine if a value is) 现在,我们已经知道了为什么会这样.确定值是否是的最佳方法是什么 null
(? To do this, you can compare the) ?为此,您可以比较 null
(directly against its value. See the example below:) 直接反对其价值.请参阅以下示例:
var myObject1 = null;
console.log(myObject1 === null); //true
(Reference Types) 参考类型
(If you are coming from another language like C#, Java, or C++, reference types are the closest thing to classes. Reference values are instances of reference types. Moreover, reference types do not store the object directly into the variable to which it is assigned. Thus, it holds a reference to the location in memory where the object exists.) 如果您来自其他语言,例如C#,Java或C ++,则引用类型是最接近类的内容.参考值是参考类型的实例.此外,引用类型不会将对象直接存储到为其分配了变量的变量中.因此,它拥有对内存中对象所在位置的引用.
(Make Objects) 制作对象
(There are various ways to create objects in JavaScript. And these are: using) 有多种方法可以用JavaScript创建对象.这些是:使用 new
(operator, object literal, and a constructor function.) 运算符,对象文字和构造函数.
(Let’s see an example below:) 让我们看下面的例子:
var myObject2 = new Object(); //using the new operator
console.log(typeof(myObject2));//object
var myObject3 = {};//using the object literal ({})
console.log(typeof (myObject3));//object
//using the constructor function
function MyUnknownObject(property1, property2) {
this.property1 = property1;
this.property2 = property2;
}
var myObject4 = new MyUnknownObject("JavaSript", 23);
console.log(typeof (myObject4));//object
(How Objects Behave?) 对象的行为方式?
(Let us see an example below:) 让我们看下面的例子:
var _object1 = new Object();
_object1.favoriteLanguage = "JavaScript";
var _object2 = _object1;
console.log(_object1); //output:{favoriteLanguage: "JavaScript"}
console.log(_object2); //output:{favoriteLanguage: "JavaScript"}
_object1.favoriteLanguage = "C#";
console.log(_object1); //output: {favoriteLanguage: "C#"}
console.log(_object2); //output: {favoriteLanguage: "C#"}
(Output) 输出量
(As you can see, we have declared a new) 如您所见,我们已经声明了一个新 object
(using the variable) 使用变量 _object1
(and assigned it to) 并分配给 _object2
(. As a result, both of them are referencing the same object. That’s why any changes to either one of them have the same effect.) .结果,它们两个都引用相同的对象.这就是为什么对其中任何一个进行任何更改都具有相同效果的原因.
(If you would like to see what’s happening, please see Figure 2 below:) 如果您想了解发生了什么,请参见下面的图2:
(Figure 2) 图2#### (Destroy Objects) 销毁对象
(Garbage collection) 垃圾收集(– Frees objects when they are not used anymore (garbage-collected). JavaScript is one of those languages that have this kind of feature built into it. Thus, you don’t really need to worry about manual memory allocations when you use reference types.) –释放不再使用的对象(垃圾回收). JavaScript是其中内置这种功能的那些语言之一.因此,使用引用类型时,您实际上不必担心手动分配内存.
(Moreover, you might have a false impression that you don’t need to deference your objects. Thereby, it’s best to deference objects that you no longer need. In order for the garbage collector to free up that memory space. In order for us to do this is to set the) 此外,您可能会产生错误的印象,即不需要尊重对象.因此,最好参照不再需要的对象.为了使垃圾收集器释放该内存空间.为了让我们做到这一点,是将 object
(variable to) 可变为 null
(.) .
(Difference between Primitive Types and Reference Types) 基本类型和引用类型之间的区别
(The main difference between the two is that primitive types directly contain their values. Meaning, when you assign a primitive value to a variable, the value is copied into that variable. While reference types don’t store the object directly into the variable to which it is assigned. Instead, it holds the reference to the location in memory where the object exists.) 两者之间的主要区别在于基本类型直接包含其值.意思是,当您为变量分配原始值时,该值将被复制到该变量中.虽然引用类型不会将对象直接存储到分配给它的变量中.相反,它保留对内存中对象所在位置的引用.
(Summary) 概要
(In this post, we have tackled the JavaScript primitive and reference types. We have started by listing the builtin types and shown the primitive types and reference types. Moreover, we have seen how to use the) 在本文中,我们解决了JavaScript原语和引用类型.我们首先列出了内置类型,并显示了原始类型和引用类型.此外,我们已经看到了如何使用 typeof
(operator and how to create and destroy an) 运算符,以及如何创建和销毁 object
(. Lastly, we have differentiated the primitive and reference types.) .最后,我们区分了原始类型和引用类型.
(I hope you have enjoyed this article, as I have enjoyed writing it. Stay tuned for more. Many thanks, until next time, happy programming!) 希望您喜欢我喜欢写这篇文章.敬请期待更多.非常感谢,直到下一次,祝您编程愉快!
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
Javascript Dev Beginner Intermediate 新闻 翻译