JS Reference type

Source: Internet
Author: User

1. Arrays

-Arrays are very common reference types in ECMAScript. There is a big difference between an array defined by ECMAScript and an array in other languages.

First, an array is also an object.

Characteristics

-arrays are a combination of a set of data

-JS arrays are more Java-like containers with variable lengths and different element types

-the array length is variable at any time and can be modified (length property)

-Common methods:

-push,pop

-shift,unshift

-splice,slice

-concat,join,sort,reverse, etc.

-Location Method: Indexof,lastindexof

-Iterative method: Every filter ForEach some map

-Shrink method: Reduce reduceright

  

<script type= "Text/javascript" charset= "UTF-8" >//var arr1 = new Array ();            vararr = [1,2,3,4,true, ' ABC ',Newdate ()];            alert (arr.length); //Change Array lengthArr.length = 5;  Alert (arr); //1,2,3,4,true called tostring by default, converting the object to a string                                    varARR2 = []; varresult = Arr2.push (1, 2,true);//press Stack, return value is new array length            varobj = Arr2.pop ();//The stack, which removes an element from the end of the array, and the return value is the element content that is removed                        varARR3 = [A.true,Newdate ()]; varRESULT1 = Arr3.shift ();//removes the first element of an array, the return value is the element content that is removed            varRESULT2 = Arr3.unshift (10,false);//inserts an element from the head, the return value is the length of the array                        varARR4 = [1,2,3,4,5]; //Splice The first parameter represents the starting position, the second parameter represents the number of intercepts, and the third parameter represents the new element appended            //splice manipulating the array itselfArr4.splice (1,2,3,4,5);//12345->134545(Arr4.splice);//12345->145Arr4.splice (1);//12345->1                        varARR5 = [1,2,3,4,5]; //Slice does not manipulate the array itselfArr5.slice (2,4);//12345->34, left and right open.                        //methods for manipulating arrays            //The concat method does not manipulate the array itself            varARR6 = [A]; varARR7 = [true, 4,5]; varRESULT3 = Arr6.concat (ARR7);//Array Merge Operations                        //The join method does not manipulate the array itself            varRESILT4 = Arr6.join ('-');//123->1-2-3 to add content between each element                        //The sort method operates on the array itself            //The reverse method operates the array itself            varARR8 = [1,4,3,5,2]; varARR9 = [10,2,4,1,7]; Arr8.reverse ();//sort in reverse order, not by size, but by element position 1,4,3,5,2->2,5,3,4,1Arr9.sort ();//The sequence is sorted by comparison of string types in Java, 10.2.4.1.7->1,10,2,4,7                        //To achieve a natural sort, you need to write your own sorting method.             functionCompare (value1, value2) {if(Value1 <value2) {                    return-1; } Else if(Value1 >value2) {                    return1; } Else {                    return0; }} arr8.sort (Compare); //1,4,3,5,2->1,2,3,4,5                        //Location Method            varArray1 = [1,2,3,4,5,4,3,2,1]; //Compare when using "= = =" To compare, do not automatically convert            varValue = Array1.indexof (4);//returns a result of 3, returning the position of the first occurrence of the parameter            varvalue1 = Array1.indexof (bis);//The first parameter is the starting position, the second argument is the lookup, that is, starting at 4, finding where the first element 4 appears.            varvalue2 = Array1.lastindexof (2);//returns 7, looking forward from behind                        //Iterative Methods            //every: Each element of an array is run with a function, and if all returns True, the last result returns true, and if one returns false, the result is false.             varres = Array1.every (function(item, index, array) {returnItem > 2;                        }); //filter: For each element of the array is also the function of the run, given the function to execute, the filtered results returned            varRes1 = Array1.filter (function(item, index, array) {returnItem > 2;                        }); //foreach: The value of each item in the loop array and executes a methodArray1.foreach (function(item, index, array) {alert (item);                        }); //Map: Each element of an array is run as a function, and after the function has been executed, the new result is returned            varRes2 = Array1.map (function(item, index, array) {returnItem * 3;                        }); //some: A function is run on each element of an array, and if one of the functions returns True, the result is true and the result is False if both are false            varRes3 = Array1.some (function(item, index, array) {returnItem > 10;                        }); //Narrowing Method            //The first parameter is the previous value, the second argument is the current value, the third parameter is the current index position, and the fourth argument is the array itself            varRes4 = Array1.reduce (function(Previous, current, index, array) {returnPrevious + current;//sum            }); //In contrast to the reduce method, the reduce method is traversed from the left, and the Reduceright method is traversed from the right.            varRES5 = Array1.reduceright (function(Previous, current, index, array) {returnPrevious + current;//sum            }); </script>

2.Object

1. So far most of the reference types are instances of type object, and object is also the most used type in ECMAScript. (like Java.lang.Object, the object type is the basis of all its instances)

How the-object type is created, using the

      

// Creation of the Object class            var New Object ();             var New // Not recommended            var obj2 = {};

-Apply for In enumeration loop for object type

2.Object Each instance will have the following properties and methods

-constructor: Holds the function (constructor) used to create the current object

-hasownproperty (PropertyName): Used to detect whether a given property exists in the current object instance (not in the prototype)

-isprototypeof (object): Used to check if the joined object is a prototype of another object

-propertyiselemerable (PropertyName): Used to check whether a given property can use the For-in statement to enumerate

-tolocalestring (): Returns the string representation of an object that corresponds to the region of the execution environment

-tostring (): Returns the string representation of an object

-valueof (): Returns the string, numeric, or Boolean representation of an object

  

<script type= "Text/javascript" charset= "UTF-8" >//Object base class for all classes            //creation of the Object class            varobj =NewObject (); varObj1 =NewObject;//Not recommended            varObj2 = {}; //set some properties for an objectObj.name = ' Zhangsan '; Obj.age= 17; Obj.sex= ' Male '; //The following method of assignment, be sure to add "obj[' birthday ' = ' 1998-03-21 '; Obj.say ()=function() {alert (' Hello World ');            }; //accessing the properties and methods of an objectalert (obj.name);            alert (obj.age);                        Obj.say (); //delete an object property or method delete operator            DeleteObj.age; DeleteObj.say; alert (obj.age); //UndefindObj.say ();//Obj.say is not a function                        //How to traverse a JS object using the for-in statement             for(varIteminchobj)            {alert (item); }//Name,sex,birthday will be output                         for(varIteminchobj)            {alert (Obj[item]); }//zhangsan,male,1998-03-21 is output                                    //Constructor: Saving an object's creation functionalert (obj.constructor);//function Object () {[native code]}            vararr = []; alert (arr.constructor); //function Array () {[native code]}                        //hasOwnProperty (PropertyName): Used to detect whether a given property exists in an objectObj.hasownproperty (' name ');//The return value is a Boolean type                        //isprototypeof (Object): Detecting prototypes                        //propertyenumerable (Propertynmae): Used to check whether a given property can be enumerated by a for in statementObj.propertyenumerable (' name ');//The return value is True</script>

 

JS Reference type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.