Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the reference data types of javascript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the reference data types of javascript". In the daily operation, I believe that many people have doubts about the reference data types of javascript. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what are the reference data types of javascript?" Next, please follow the editor to study!

In javascript, a reference data type is a data structure that organizes data and functions together; it is also often referred to as a class. The value of the reference type is an object stored in memory (both in stack memory and heap memory); the value of the reference type is accessed by reference.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Data types refer to the types of values that can be stored and manipulated in a program. each programming language has its own supported data types, and different data types are used to store different data, such as text, values, images, and so on.

Data types in JavaScript can be divided into two types:

Basic data types (value types): string (String), number (Number), Boolean (Boolean), empty (Null), undefined (Undefined), Symbol

Reference data types: object (Object), array (Array), function (Function).

JavaScript reference data type reference type

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

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

Object Typ

There are two ways to create an instance of Object. The first is to use the new operator followed by the Object constructor, such as

Var person = new Object (); person.name = "Nicholas"; person.age = 29

Another way is to use the literal representation of objects. For example:

Var person = {name: "Nicholas", age; 29}

Note: when an object is defined literally, the Object constructor is not actually called.

Array Typ

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

Var colors = new Array ()

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

Var colors = ["red", "blue", "green"]; detection array

The instanceof operator assumes only one global execution environment. To solve only one problem, ECMAScript 5 added the Array.isArray () method. The goal of this method is to finally determine whether a value is an array, regardless of which global execution environment it was created in.

Conversion methods toLocaleString (), toString () and valueOf ()

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

Calling the toString () method of the array returns a comma-separated string

Calling toLocaleString () is the same as toString (), but the string corresponds to the locale of the execution environment

The call to valueOf () returns the same array.

Join () method

The join () method takes only one parameter, the string used as the delimiter, and then returns the string containing all the array items.

Stack method (LIFO)

ECMAScript specifically provides push () and pop () methods for arrays to implement stack-like behavior. The push () method can take any number of parameters, add them to the end of the array one by one, and return the length of the modified array. The pop () method reduces the length of the array from the last item at the end of the array, and then 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. Using the shift () and push () methods together, you can use arrays just like queues.

ECMAScript also provides a unshift () method for arrays. As the name implies, unshift () is the opposite of shift (): it can add any item to the front of the array and return the length of the new array.

Reordering method

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

By default, the sort () method sorts the array items in ascending order-- that is, the smallest value comes first and the largest value comes last. To achieve sorting, the sort () method uses the toString () transformation method for each array item, and then compares the resulting strings to determine how to sort them. This sort is not the best option in many cases, so the sort () method can take a comparison function as an argument to determine which value precedes which value.

The return values of the reverse () and sort () methods are sorted arrays

Operation method

The concat () method creates a new array based on all the items in the current array.

The slice () method can create a new array based on one or more items in the current array. The slice () method can accept one or two parameters, that is, the start and end positions of the item to be returned. (will not change the original array)

The splice () method mainly inserts values into the middle of the array. (delete, insert, replace) this method changes the value of the original array.

Location method

ECMAScript adds two location methods to the array instance: both indexOf () and lastindexOf () return the position of the item to be found in the array, or-1 if it is not found. Both methods accept two parameters: the item to find and the index that represents the starting point of the lookup (optional).

Iterative method

ECMAScript defines five iterative methods for arrays:

Every (): runs the given function on each item in the array, and returns true if the function returns true for each item.

Filter (): runs the given function on each item in the array and returns an array of items that will return true.

ForEach (): runs the given function on each item in the array. This method does not return a value.

Map (): runs the given function on each item in the array and returns an array of results from each function call.

Some (): runs the given function on each item in the array and returns true if the function returns true for any item.

Merging method

ECMAScript 5 also adds two new methods for merging arrays: reduce () and reduceRight (). Both methods iterate over all the items in the array and then build a final returned value.

You can use the reduce () and reduceRight () methods to perform operations that sum all the values in an array, such as:

Var values = [1, 2, 3, 4, 5]

Var sum = values.reduce (function (prev, cur, index, array) {

Return prev + cur

});

Alert (sum); / / 15

Date Typ

To create a date object, use the new operator and the Date constructor:

Var now = new Date ()

When the Date constructor is called without passing parameters, the newly created object automatically gets the current date and time. To accept a string parameter that represents a date, ECMAScript provides two methods: Date.parse () and Date.UTC ().

ECMAScript adds the Date.now () method, which returns the number of milliseconds representing the date and time when the method was called.

Method of inheritance

The Date type also overrides the toLocaleString (), toString (), and valueOf () methods.

The toLocaleString () method returns the date and time in a format appropriate to the browser, while the toString () method usually returns the date and time with time zone information. As for the valueOf () method, it does not return a string at all, but a millisecond representation of the date.

Function Typ

A function is actually an object, and the function name is actually a pointer to a function object and will not be bound to a function. Each function is an instance of the Function type and has properties and methods like other reference types.

Functions are usually defined using function declaration syntax: (function declaration promotion)

Function sum (sum1,sum2) {return sum1 + sum2;}

There is another way to define a function using a function expression:

Var sum = function (sum1,sum2) {return sum1 + sum2;}

Note: to access the function pointer without executing the function, you must remove the parentheses after the function name.

Internal properties of function

Inside the function, there are two special objects: arguments and this. Arguments is a class array object that contains all the parameters passed in the function. The object has a property that has a pointer to the function that owns the arguments object. This refers to the environment object that the function data is used to execute. (when a function is called in the global scope of a web page, the this object refers to window.)

Function properties and method properties

Each function contains two properties: length and prototype.

The length attribute represents the number of named parameters that the function wants to receive.

For reference types in ECMAScript, prototype is the real place to hold all their instance methods. The prototype property is not enumerable, so using for-in is not valid.

Method

Each function contains two non-inherited methods: apply () and call ()

The purpose of both methods is to call a function in a specific scope, which is actually tantamount to setting the value of the this object in the function body. Examples of action: 1. Pass parameter 2. Extend the scope on which the function runs

EAMAScript also defines a method: bind ()

This method creates an instance of the function whose th value is bound to the value passed to the bind () function.

The toLocaleString (), toString (), and valueOf () methods inherited by each function always return the code of the function.

Object

An object is an instance of a specific reference type. The new object is created using the new operator followed by a constructor.

New

The purpose of the new operator is to create an object instance. This object can be user-defined or built-in by some system with a constructor. If the constructor after the new expression returns something other than JavaScript's built-in reference object (Object,String, etc.) new creates an anonymous object and returns it; if it is a built-in reference object or the original type, it overwrites the anonymous object. (when there is no return, it is actually return primitive type undefined)

Constructor function

The constructor itself is a function, except that the function is defined for the purpose of creating a new object.

The difference between object and Object in JavaScript object

When you use typeof to detect a data type, it returns object as long as the variable you check is an object, or null.

Object

Object is an important object in JavaScript, and other objects are based on it, including the functions you create.

The difference between typeof operator and instanceof operator

The typeof operator can be used to determine which basic type a value is, and the instanceof operator can be used to determine which reference type a value is.

Function and Function

ECMAScript's Function is actually a fully functional object. While the keyword function is used to create constructors for all objects or use keywords to define classes and objects for ordinary functions, var a=new function () {} actually uses constructor methods to create an instance of an anonymous object, not an instance of the system's built-in object Function. So an instanceof Function returns false,typeof and returns "object".

When does typeof return "function"?

Function a () {} / / undefined typeof a / / "function" at this point, the study on "what are the reference data types of javascript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report