Home > Web Front-end > JS Tutorial > body text

What is the reference data type of javascript

青灯夜游
Release: 2021-11-19 16:55:52
Original
6047 people have browsed it

In JavaScript, a reference data type is a data structure used to organize data and functionality together; it is also often called a class. Values ​​of reference types are objects stored in memory (both in stack memory and heap memory); values ​​of reference types are accessed by reference.

What is the reference data type of javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Data type refers to the type of value that can be stored and manipulated in the program. Each programming language has its supported data types. Different data types are used to store different data, such as text and numerical values. , images, etc.

Data types in JavaScript can be divided into two types:

  • Basic data types (value types): String, Number, Boolean (Boolean), Null, Undefined, Symbol;

  • Reference data types: Object, Array, Function.

JavaScript reference data type

Reference type

In ECMAScript, a reference type is a A data structure used to organize data and functionality together (it is also often called a class).

The value of the reference type is an object stored in memory (saved in both stack memory and heap memory). JavaScript does not allow direct access to locations in memory, so when you operate on an object, you are actually operating on a reference to the object rather than the actual object. Values ​​of reference types are accessed by reference.

Object type

There are two ways to create an Object instance. The first is to use the new operator followed by the Object constructor, for example;

var person = new Object();
 person.name = "Nicholas";
 person.age = 29;
Copy after login

The other way is to use object literal notation. For example:

var person = {
 name : "Nicholas",
 age ; 29
 }
Copy after login

Note: When defining an object through an object literal, the Object constructor is not actually called.

Array type

There are two basic ways to create an array. The first is to use the Array constructor, for example:

var colors = new Array();
Copy after login

The second basic way is to use the array literal representation. An array literal is represented by a pair of square brackets containing array items. Multiple array items are separated by commas, for example:

var colors = ["red","blue","green"];
Copy after login

Detecting array

instanceof operation The operator assumes that there is only one global execution environment. In order to solve this problem, ECMAScript 5 added the Array.isArray() method. The purpose of this method is to ultimately determine whether a value is an array, regardless of the global execution context in which it was created.

Conversion methods

##toLocaleString(), toString() and valueOf() methods
All objects have toLocaeString( ), toString() and valueOf() methods.

Calling the toString() method of an array will return a comma-separated string;
Calling toLocaleString() is the same as toString(), but the string corresponds to the region of the execution environment;
Calling valueOf() What is returned is still an array.

join() method
The join() method accepts only one parameter, a string used as a separator, and returns a string containing all array items.

Stack method (last in, first out)

ECMAScript provides push() and pop() methods specifically for arrays to achieve stack-like behavior. The push() method can accept any number of parameters, add them to the end of the array one by one, and return the modified length of the array. The pop() method removes the last item from the end of the array, decrements the length of the array, and returns the removed item.

Queue method (first in, first out)

shift() can remove the first item in the array and return it, while reducing the length of the array by one. Combining the shift() and push() methods allows you to use arrays like queues.

ECMAScript also provides an unshift() method for arrays. As the name suggests, unshift() does the opposite of shift(): it adds any number of items to the front of an array and returns the length of the new array.

Reordering method

The reverse() method reverses the order of array items.

By default, the sort() method sorts the array items in ascending order - that is, the smallest value is at the front and the largest value is at the end. To implement sorting, the sort() method uses the toString() conversion method of each array item and then compares the resulting strings to determine how to sort. This sorting method is not optimal in many cases, so the sort() method can receive a comparison function as a parameter to specify which value comes before which value.

The return value of the reverse() and sort() methods is the sorted array

操作方法

concat()方法可以基于当前数组中的所有项创建一个新数组。
slice()方法能够基于当前数组中的一或多个项创建一个新数组。slice()方法可以接受一或者两个参数,即要返回项的起始和结束位置。(不会改变原数组)
splice()方法主要是向数组的中部插入值。(删除、插入、替换)该方法改变原数组的值。

位置方法

ECMAScript为数组实例添加了两个位置方法:indexOf()和lastindexOf()这两个方法都返回要查找的项在数组中的位置,或者在没有找到的情况下返回-1。这两个方法都接受两个参数:要查找的项和表示查找起点位置的索引(可选的)。

迭代方法

ECMAScript为数组定义了五个迭代方法:

every():对数组中的每一项运行给定函数,如果该函数对每一项都返回 true,则返回 true。
filter():对数组中的每一项运行给定函数,返回该函数会返回 true 的项组成的数组。
forEach():对数组中的每一项运行给定函数。这个方法没有返回值。
map():对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组。
some():对数组中的每一项运行给定函数,如果该函数对任一项返回 true,则返回 true。

归并方法

ECMAScript 5 还新增了两个归并数组的方法:reduce()和 reduceRight()。这两个方法都会迭代数组的所有项,然后构建一个最终返回的值。
使用 reduce()和reduceRight()方法可以执行求数组中所有值之和的操作,比如:
var values = [1,2,3,4,5];
var sum = values.reduce(function(prev, cur, index, array){
return prev + cur;
});
alert(sum); //15

Date类型

创建日期对象,使用new操作符和Date构造函数即可:
var now = new Date();
在调用Date构造函数而不传递参数的情况下,新创建的对象自动获得当前日期和时间。 为了可以接受表示日期的字符串参数,ECMAScript提供了两个方法:Date.parse()和Date.UTC()。
ECMAScript添加了Date.now()方法,返回表示调用这个方法时的日期和时间的毫秒数。

继承的方法

Date类型也重写了toLocaleString()、toString()和valueOf()方法。
toLocaleString()方法会按照与浏览器相适应的格式返回日期与时间,而toString()方法则通常返回带有时区信息的日期和时间。至于valueOf()方法,则根本不返回字符串,而是返回日期的毫秒表示。

Function类型

函数实际上是对象,函数名实际上也是一个指向函数对象的指针,不会与某个函数绑定。每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法。
函数通常是使用函数声明语法定义的:(函数声明提升)

function sum (sum1,sum2) {
 return sum1 + sum2;
 }
Copy after login

还有一种方式,使用函数表达式定义函数:

var sum = function(sum1,sum2) {
 return sum1 +sum2 ;
 };
Copy after login

注意:要访问函数指针而不执行函数的话,必须去掉函数名后面的圆括号。

函数内部属性

在函数内部,有两个特殊的对象:arguments和this。arguments是一个类数组对象,包含着传入函数中的所有参数。该对象有一个属性,该属性有一个指针,指向拥有这个arguments对象的函数。this引用的是函数据以执行的环境对象。(当在网页的全局作用域中调用函数时,this对象引用的就是window)

函数属性和方法

属性

每个函数都包含两个属性:length和prototype。
length属性表示函数希望接收的命名参数的个数。
对于ECMAScript中的引用类型而言,prototype是保存它们所有实例方法的真正所在。prototype属性是不可枚举的,所以使用for-in无效。

方法

每个函数都包含两个非继承而来的方法:apply()和call()
这两个方法的用途都是在特定的作用域中调用函数,实际上等于设置函数体内this对象的值。作用实例:1.传递参数 2.扩充函数赖以运行的作用域
EAMAScript还定义了一个方法:bind()
这个方法会创建一个函数的实例,其this值会被绑定到传给bind()函数的值。
每个函数继承的toLocaleString()、toString()和valueOf()方法始终都返回函数的代码。

对象

对象是某个特定引用类型的实例。新对象是使用new操作符后跟一个构造函数来创建的。

new

new运算符的作用是创建一个对象实例。这个对象可以是用户自定义的,也可以是带构造函数的一些系统自带的对象。如果 new 表达式之后的构造函数返回的不是JavaScript内置的引用对象(Object,String等)new会创建一个匿名对象并返回;如果是内置引用对象或者原始类型就会覆盖匿名对象。(无 return 时其实为 return 原始类型 undefined)

构造函数

构造函数本身就是一个函数,只不过该函数是出于创建新对象的目的而定义的。

JavaScript中object和Object的区别

object

使用typeof检测数据类型的时候,只要检查的变量是一个对象,或者是null,那么它就会返回object。

Object

Object 是 JavaScript 中一个重要的对象,其它对象都是基于它的,包括你创建的函数。

typeof 操作符和 instanceof 操作符的区别

确定一个值是哪种基本类型可以使用 typeof操作符,而确定一个值是哪种引用类型可以使用instanceof操作符。

function和Function

ECMAScript 的Function实际上就是一个功能完整的对象。而function这个关键字是用来创建所有对象的构造函数或者使用关键字来定义普通函数的类和对象,var a=new function(){}实际上是用构造函数的方法创建了一个匿名对象的实例,而并不是系统内置对象Function的实例。所以a instanceof Function返回false,typeof返回"object"。

什么时候typeof返回"function"呢?

function a (){ }
 //undefined
 typeof a
 //"function"
Copy after login

【推荐学习:javascript高级教程

The above is the detailed content of What is the reference data type of javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!