This article explains the relevant information about JavaScript data types for your reference. The specific content is as follows
1. Reference Type
A value of a reference type is an instance of a reference type. A reference type is a data structure used to organize data and functions, and is often called a class.
An instance of a specific reference type is an object. The new object is created using the new operator followed by a constructor. The constructor itself is a function and is defined for the purpose of creating a new object.
var person = new Object();
2.Object type
(1) The Object type is an ideal choice for storing and transmitting data in applications.
(2) How to create an Object type instance:
Use new to add the Object constructor.
<script type="text/javascript"> var person = new Object(); person.name = "peter"; alert(person.name); </script>
Use object literal representation method.
var dog = { name:"kity", age:3, eye:bigEyes };This creation method starts with a pair of curly braces, with the variable name first, followed by a colon, and then an attribute value. There can be multiple attributes, but each attribute must be separated by commas, and the last attribute is generally not added with a comma. The attribute name can also be a string. If there is no writing in curly braces, you can only define objects that contain default properties and methods.
Generally speaking, accessing object properties uses dot notation, and using square brackets [] in javascript to access object properties. When using [], the attributes to be accessed should be placed in [] as strings.
alert(person["name"]);
alert(person.name);
The former can access properties through variables.
If the property name contains characters that will cause syntax errors, the property name uses keywords or reserved words, and square brackets can also be used.
Unless you have to use variables to access properties, we recommend using dot notation.
3. Array type
Arrays in javascript are ordered lists, which can save data of any type, which is the biggest difference between them and other language arrays, and the size of its array can be dynamically adjusted.
(1) Basic ways to create arrays:
Using the Array constructor, you can pass values (the size of the array or the content of the array)
var student = new Array(); var student = new Array(10); var student = new Array("peter","merry","bob");(2) Use the representation method of array literals. The array literals are represented by a pair of square brackets containing array items, and multiple arrays are separated by commas.
(3) When reading and setting the value of the array, use square brackets and the corresponding value based on 0 numeric index.
The length of the array is saved in the length attribute, and this attribute value can return a value of 0 or greater. You can delete or add new items from the end of the array by setting the value of the length attribute.
var colors = ["red","blue","green"]; colors.length = 2;//The length of the array becomes 2, green is removed alert(colors[2]);//At this time, the access will return undefined, alert(colors[1]); will return blue
If the Length property is set to a value greater than the array item, each item added will return an undefined value.
var colors = ["red","blue","green"]; colors.length = 5; alert(colors[4]);
Use the length attribute to add items at the end of the array.
var colors = ["red","blue","green"]; alert(colors[colors.length]="black");
(4) The array can contain up to 4294967295 items
(5) Detection array
Using the Array.isArray() method, it can finalize whether the value is an array, regardless of which global execution environment it was created.
if(Array.isArray){ //operates }(6) Conversion method
All objects have toLocaleString(), toString() and valueOf() methods, where calling the toString() method of the array returns a comma-separated string spliced from each value string in the array. valueOf() returns the array.
When the toLocaleString() method is called, an array is created that is worth comma-separated string. Unlike the previous one, in order to obtain the value of each item, the toLocaleString() method of each item is called, rather than the toString() method.
<script type="text/javascript"> var person1 = { toString : function(){ return "peter"; }, toLocaleString :function(){ return "mary"; } }; var person2 = { toString: function(){ return "26"; }, toLocaleString:function(){ return "18"; } }; var person = [person1,person2]; alert(person); alert(person.toString()); alert(person.toLocaleString()); </script>Use the join() method to build this string with different delimiters. The join() method only receives one parameter, a string used as a delimiter, and then returns a string containing all array items. If you do not pass any value to the join() method, or pass undefined, use a comma as the separator. If the value of an item in the array is null or undefined, the value is represented as an empty string in the results returned by the join(), toString(), toLocaleString() and valueOf() methods.
<script type="text/javascript"> var person1 = { toString : function(){ return "peter"; }, toLocaleString :function(){ return "mary"; } }; var person2 = { toString: function(){ return "26"; }, toLocaleString:function(){ return "18"; } }; var person = [person1,person2]; alert(person); alert(person.toString()); alert(person.toLocaleString()); alert(person.join("*")); alert(person.join("*")); alert(person.join("--")); </script>(7) Stack method
The stack is the stack in the data structure. Its characteristic is to enter first and then exit, and all operations only occur at the top of the stack. JavaScript provides push() and pop() methods, which can achieve behavior similar to the stack.
push() can receive any number of parameters and add them one by one to the end of the array, and return the length of the modified array.
The pop() method can remove the last item from the end of the array, reduce the lengthh value of the array, and then return to the removed top.
(8) Queue method
The data structure of a queue is characterized by first-in-first-out. The queue adds items at the end of the list and removes items from the front end of the list.
push() can add an item to the end of the array, shift() can remove the first item in the array, and return the item, and the length of the array is reduced by 1; unshift() can add any item to the front end of the array and return the length of the new array.
(9) Reordering method
reverse() will reverse the order of the array
By default, sort() arranges array items in ascending order (the minimum value is at the front and the maximum value is at the end. To achieve sorting, the sort() method will call the toString() transformation method of each array item, and then compare the resulting string to determine how to sort). sort() can receive a comparison function as a parameter. The comparison function receives two parameters. If the first parameter should be before the second, it returns a negative number, if the two parameters are equal, it returns 0, and if the first parameter should be after the second, it returns a positive number.
The return values of reverse() and sort() methods are both sorted arrays.
(10) Operation method
concat() can create a new array based on all items in the current array.
slice() can create a new array based on one or more items in the current array, slice() can receive one or two parameters to return the start and end positions of the item. When the parameter is one, it returns all items from the specified position of the parameter to the end of the current array; if there are two parameters, it returns the items between the starting and end positions, but does not include the items at the end position.
splice() method: mainly used to insert items into the middle of the array
Delete: You can delete any number of items, only 2 parameters need to be specified, the location of the first item to be deleted and the number of items to be deleted.
Insert: You can insert any number of items into the specified position, just provide three parameters, the start position and 0 and the item to be inserted.
Replacement: You can insert any number of items into the specified position and delete any number of items at the same time. You only need to specify 3 parameters, the starting position, the number of items to be deleted and any number of items to be inserted. The number of items inserted does not have to be equal to the number of items to be deleted.
splice() will always return an array containing items removed from the original array (if no items are deleted, an empty array is returned).
(11) Position method
Both methods can receive two parameters, the item to be searched and the index indicating the location of the starting point of the search, both return the position of the item to be searched in the array, or return -1 if not found. When comparing the first parameter to each item in the array, the convergence operator will be used, and the items to be searched must be strictly equal.
indexOf(): Start from the beginning of the array and look backwards
lastIndexOf(): Starts from the end of the array and looks forward.
(12) Iteration method
JavaScript provides 5 iterative methods for arrays, each receiving two parameters: the function to run on each item and the (optional) scoped object (the value that affects this). The functions passed into these methods receive three parameters: the value of the array item, the position in the array, and the array object itself.
every(): Run a given function on each item in the array. If the function returns true for each item, it returns true.
filter(): Run a given function on each item in the array. Returning an array consisting of items that return true will return the function.
forEach(): Run a given function on each item in the array, this method does not return a value
map(): Run a given function on each item in the array, returning an array composed of the result of each function call
some(): Run a given function on each item in the array. If the function returns true for any item, it returns true.
None of the above methods will modify the included values in the array.
Every() and sum() are both used to query whether the items in the array meet a certain condition
(13) Reduce method
The following two methods iterate over all items in the array and then build a final returned value. Both receive two parameters: one calls the function on each item and (optional) as the initial value to narrow down the base. Pass to these two methods to receive 4 parameters: the previous value, the current value, the index of the item and the array object. Any value returned by this function will be automatically passed to the next item as the first parameter. The first iteration occurs on the second item of the array, so the first parameter is the first item of the array and the second parameter is the second item of the array.
reduce(): Start with the first item of the array, traverse one by one to the end. You can perform operations that can sum all worthwhile in an array.
<script type="text/javascript"> var values=[1,2,3,4,5]; var sum = values.reduce(function(prev,cur,index,array){ return prev + cur; }); alert(sum); </script>reduceRight(): Start from the last item of the array and traverse forward to the first item. You can perform operations that can sum all worthwhile in an array.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.