search
HomeWeb Front-endJS TutorialBasic types and reference types in js
Basic types and reference types in jsApr 10, 2018 pm 02:10 PM
javascriptQuotetype

The content shared with you in this article is about the basic types and reference types in js. It has certain reference value. Friends in need can refer to it


Basic types and reference types in js


Reprinted from https://blog.csdn.net/shuidinaozhongyan/article/details/72520842
Basic type: Number, String,Boolean,Null,undefined.

Reference types: Object, Array, Date, RegExp, Function

The difference between null and undefined.

Reference: http://www.ruanyifeng.com/blog/2014/03/undefined-vs-null.html

1. Similarity

1. In JavaScript, assigning a variable to undefined or null, to be honest, there is almost no difference.

var a=null;  
var b=undefined;

2. Undefined and null will be automatically converted to false in the if statement, and the equality operator will even directly report that they are equal.

if (!undefined) 
    console.log('undefined is false');// undefined is falseif (!null) 
    console.log(&#39;null is false&#39;);// null is falseundefined == null// true<span style="font-family:Georgia, serif;color:#111111;"><span style="font-size:16px;letter-spacing:-.12px;word-spacing:2.4px;background-color:rgb(245,245,213);"><strong>
</strong></span></span>

The above code illustrates how similar the behaviors of the two are!

Since the meanings and usage of undefined and null are similar, why do we need to set two such values ​​at the same time? Doesn't this increase the complexity of JavaScript for no reason and trouble beginners? The Dart language, a replacement for the JavaScript language developed by Google, clearly stipulates that there is only null and no undefined!

2.

Recently, when I was reading the new book "Speaking JavaScript", I accidentally discovered the answer to this question!

It turns out that this is related to the history of JavaScript. When JavaScript was born in 1995, initially like Java, only null was set as the value representing "nothing".

According to the tradition of C language, null is designed to be automatically converted to 0.

Number(null)// 05 + null// 5

However, JavaScript designer Brendan Eich feels that this is not enough, for two reasons.

First of all, null is treated as an object just like in Java. However, JavaScript data types are divided into two categories: primitive types (primitive) and composite types (complex). Brendan Eich feels that the value representing "none" is best not an object.

Secondly, the initial version of JavaScript did not include an error handling mechanism. When a data type mismatch occurs, the type is often automatically converted or fails silently. Brendan Eich feels that if null is automatically converted to 0, it will be difficult to find errors.

Therefore, Brendan Eich designed another undefined.

3. Initial design
The initial version of JavaScript is distinguished as follows: null is an object that represents "none" and is 0 when converted to a numerical value; undefined is a representation of " The original value of "None" is NaN when converted to a numerical value.

Number(undefined)// NaN5 + undefined// NaN

4. Current usage
However, the above distinction quickly proved to be unfeasible in practice. Currently, null and undefined are basically synonymous, with only some subtle differences.

null means "no object", that is, there should be no value there. Typical usage is:

(1) As a parameter of a function, it means that the parameter of the function is not an object.

(2) As the end point of the object prototype chain.

Object.getPrototypeOf(Object.prototype)
// null

undefined means "missing value", that is, there should be a value here, but it has not been defined yet. Typical usage is:

(1) When the variable is declared but not assigned a value, it is equal to undefined.

(2) When calling the function, the parameter that should be provided is not provided, and the parameter is equal to undefined.

(3) The object has no assigned attribute, and the value of this attribute is undefined.

(4) When the function does not return a value, it returns undefined by default.

var i;
i // undefinedfunction f(x){console.log(x)}
f() // undefinedvar  o = new Object();
o.p // undefinedvar x = f();
x // undefined

Is Object a reference type?

 Object是一个基础类型,其他所有类型都从Object继承了基本行为。比如原型链中它的原型为null。

What is the difference between reference types and basic types? Which one is on the heap and which one is on the stack?

1. Basic type variables are stored in the stack area (the stack area refers to the stack memory in the memory)

Then its storage structure is as follows:

Basic types and reference types in js

The stack area includes variable identifiers and variable values.

2.

Javascript is different from other languages. It does not allow direct access to the location in the memory, which means that the memory space of the object cannot be directly manipulated. Then we operate What? In fact, it is a reference to the operating object,
so the value of the reference type is accessed by reference.
To be precise, the storage of reference types requires the stack area and the heap area of ​​the memory (the heap area refers to the heap memory in the memory). The stack area memory stores the variable identifier and the pointer to the object in the heap memory.
It can also be said to be the address of the object in the heap memory.
If there are the following objects:

var person1 = {name:&#39;jozo&#39;};var person2 = {name:&#39;xiaom&#39;};var person3 = {name:&#39;xiaoq&#39;};

The situation of these three objects saved in memory is as follows:

Basic types and reference types in js

in js Basic types and reference types

Reprinted from https://blog.csdn.net/shuidinaozhongyan/article/details/72520842
Basic types: Number, String, Boolean, Null, undefined.

Reference types: Object, Array, Date, RegExp, Function

The difference between null and undefined.

Reference: http://www.ruanyifeng.com/blog/2014/03/undefined-vs-null.html

1. Similarity

1.在JavaScript中,将一个变量赋值为undefined或null,老实说,几乎没区别。

var a=null;  
var b=undefined;

2.undefined和null在if语句中,都会被自动转为false,相等运算符甚至直接报告两者相等。

if (!undefined) 
    console.log(&#39;undefined is false&#39;);// undefined is falseif (!null) 
    console.log(&#39;null is false&#39;);// null is falseundefined == null// true<span style="font-family:Georgia, serif;color:#111111;"><span style="font-size:16px;letter-spacing:-.12px;word-spacing:2.4px;background-color:rgb(245,245,213);"><strong>
</strong></span></span>

上面代码说明,两者的行为是何等相似!

既然undefined和null的含义与用法都差不多,为什么要同时设置两个这样的值,这不是无端增加JavaScript的复杂度,令初学者困扰吗?Google公司开发的JavaScript语言的替代品Dart语言,就明确规定只有null,没有undefined!

二、

最近,我在读新书《Speaking JavaScript》时,意外发现了这个问题的答案!

原来,这与JavaScript的历史有关。1995年JavaScript诞生时,最初像Java一样,只设置了null作为表示”无”的值。

根据C语言的传统,null被设计成可以自动转为0。

Number(null)// 05 + null// 5

但是,JavaScript的设计者Brendan Eich,觉得这样做还不够,有两个原因。

首先,null像在Java里一样,被当成一个对象。但是,JavaScript的数据类型分成原始类型(primitive)和合成类型(complex)两大类,Brendan Eich觉得表示”无”的值最好不是对象。

其次,JavaScript的最初版本没有包括错误处理机制,发生数据类型不匹配时,往往是自动转换类型或者默默地失败。Brendan Eich觉得,如果null自动转为0,很不容易发现错误。

因此,Brendan Eich又设计了一个undefined。

三、最初设计
JavaScript的最初版本是这样区分的:null是一个表示”无”的对象,转为数值时为0;undefined是一个表示”无”的原始值,转为数值时为NaN。

Number(undefined)// NaN5 + undefined// NaN

四、目前的用法
但是,上面这样的区分,在实践中很快就被证明不可行。目前,null和undefined基本是同义的,只有一些细微的差别。

null表示”没有对象”,即该处不应该有值。典型用法是:

(1) 作为函数的参数,表示该函数的参数不是对象。

(2) 作为对象原型链的终点。

Object.getPrototypeOf(Object.prototype)
// null

undefined表示”缺少值”,就是此处应该有一个值,但是还没有定义。典型用法是:

(1)变量被声明了,但没有赋值时,就等于undefined。

(2) 调用函数时,应该提供的参数没有提供,该参数等于undefined。

(3)对象没有赋值的属性,该属性的值为undefined。

(4)函数没有返回值时,默认返回undefined。

var i;
i // undefinedfunction f(x){console.log(x)}
f() // undefinedvar  o = new Object();
o.p // undefinedvar x = f();
x // undefined

Object是引用类型嘛?

 Object是一个基础类型,其他所有类型都从Object继承了基本行为。比如原型链中它的原型为null。

引用类型和基本类型有什么区别?哪个是存在堆哪一个是存在栈上面的?

1.基本类型的变量是存放在栈区的(栈区指内存里的栈内存)

那么它的存储结构如下图:

Basic types and reference types in js

栈区包括了 变量的标识符和变量的值。

2.

javascript和其他语言不同,其不允许直接访问内存中的位置,也就是说不能直接操作对象的内存空间,那我们操作啥呢? 实际上,是操作对象的引用,
所以引用类型的值是按引用访问的。
准确地说,引用类型的存储需要内存的栈区和堆区(堆区是指内存里的堆内存)共同完成,栈区内存保存变量标识符和指向堆内存中该对象的指针,
也可以说是该对象在堆内存的地址。
假如有以下几个对象:

var person1 = {name:&#39;jozo&#39;};var person2 = {name:&#39;xiaom&#39;};var person3 = {name:&#39;xiaoq&#39;};

则这三个对象的在内存中保存的情况如下图:

Basic types and reference types in js

相关推荐:

JS引用类型的介绍

The above is the detailed content of Basic types and reference types in js. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function