A reference type is a data structure used to organize data and functions together. This article mainly introduces the reference type of JavaScript tutorials.
Reference Type
A reference type is a data structure used to organize data and functions together. It is also often called a class, but this name is not appropriate. Although ECMAScript is technically an object-oriented language, it does not have basic structures such as classes and interfaces supported by traditional object-oriented languages. Reference types are sometimes called object definitions because they describe the attributes and methods of a class of objects.
As mentioned above, the value of the reference type is an object. In ECMAScript, the reference type is a data structure used to organize data and functions together, the object is an instance of a specific reference type.
var a=new Object();
The above declares an instance with an Object reference type and stores the instance in variable a. That is to say, this variable does not actually contain the instance itself, the pointer to this instance.
For the Object type, the Common Object literal representation is used to create an instance. The advantage of var a = {name: "Nick", age: 20} is encapsulation. The object is accessed using DOT or square brackets. A. name is equivalent to a ["name"]. Note that "name" is represented by a string.
For the Array type, you can use the Array literal representation.
For the Array type, you can use length to change the length of the Array. (Add or remove items from the end of the array)
The method for detecting arrays is the Array. isArray (value) method.
Conversion Method: toString () is converted to a string separated. ValueOf (), returns an array. ToLocaleString () can be implemented in the following example.
var p1={toString:function(){return "guo";},toLocaleString:function(){return "yuzhe";}}var p2={toString:function(){return "song";},toLocaleString:function(){return "hap";}}var p=[p1,p2];alert(p); //guo,songalert(p.toLocaleString()); //yuzhe,hap
It can be seen that alert calls the toString () method before the output, and the join () method is used to return the result as a string with the specified symbol. Its default (No parameter is set) is ", ".
Stack method: add an item at the end of push () and return the length of the array. Pop () deletes an item at the end and returns the deleted item.
Queue method: shift () overflows the first entry of the array, and this item is returned. Unshift () adds an entry in the first segment and returns the length of the array.
Re-sorting method (the returned value is an array ):
The reverse order of reverse. A [length-1] = a [0]
The default sort () sort method in ascending order is in ASCII order, rather than the number size we think.
function compare(no1,no2){if(no1
no2){return 1;}else{return 0;} }var a=[1,2,3,4,6,5];a.sort(compare);alert(a)
To produce a descending order, you only need to reverse the if statement.
Operation Method:
Concat () creates a copy that does not affect the original array and adds accepted parameters to the end of the array.
Slice () creates a copy and accepts one or two parameters (the start and end positions of the returned item, excluding the end position). If there is only one parameter, returns all items from the specified position to the end. If the parameter is negative, the result is length + arguments. If the end position is smaller than the start position, an empty array is returned.
Splice (): 1. Delete Method -- specify two parameters, the location of the first item to be deleted and the number of items to be deleted.
2. Insert Method -- specify three parameters, starting position, 0 (number of objects to be deleted), and the items to be inserted.
3. replacement method -- specify three parameters, starting position, number of deletions, and items to be inserted.
The insert/replace position is the starting position.
Location Method:
IndexOf () returns the following table of the array of items to be searched. If not,-1 is returned. Parameter: the item to be searched and (optional) The index (subscript) at the start position of the search ).
LastIndexOf () is the reverse order of indexOf.
Iteration Method:
Two parameters: the function to be run and the (optional) scope. The function in these methods must have three parameters (item (value of the array item ), index (location of the item), array (array object itself )).
Every () is used to run the given function for each item in the array. If every item returns true, true is returned.
An array composed of true items is returned when the filter () is returned.
ForEach () runs the given function for each item without returning a value.
Map () returns an array composed of the execution results of each function.
Some () returns true if one of them is true.
《script》var a=[1,2,3,4];var b=a.every(function(item,index,array){return item>2;});alert(b); //false《script》
Merge method:
Reduce () starts to traverse from the first item of the number, and reduceRight () starts to traverse from the last item of the array.
Use reduce () to calculate all and
《script》var a=[1,2,3,4];var b=a.reduce(function(prve,cur,index,array){return prve+cur;});alert(b); //10
For the first execution, prev is 1, cur is 2, prev is 3, and cur is 3.
The above section describes the reference type of the JavaScript getting started tutorial. I hope it will be helpful to you!