The Advanced way of JavaScript (iii) type object and array type of reference type

Source: Internet
Author: User
Tags array length

Reference type Object type
function A (num) {if (num>3) {a (--num);  } Console.log (num);   } A (5);//How to create an instance of an object var obj1= new Object (); Console.log (obj1); Obj1.name= "Wu Qiong"; Obj1.age=28;console.log (obj1.name+ "" + Obj1.age);//object literal syntax, somewhat encapsulated feel var obj2 = {name: "Wu Qiong", Age:28};var obj3 = {Age:28,age_wo:30};console.log (obj3["Age_wo"]); Console.log (obj2.name+ "" +obj2.age); function Show (ARG) {var s= ""; if (arg.name! = undefined) {console.log (arg.name);} Else{console.log ("Without this Name");}} Show (OBJ3); show (OBJ1);

  

Each item in an array of type ECMAScript can hold any type of data. Also, the size of the array can be adjusted automatically. Sets the length of the array array.length=2, or less than the array length to move the items in the array, and the length of the array to add the undefined if the length is: array.length; The subscript for the last item of the array is: Array.length-1 array method to detect array ECMAScript3 instanceof
ECMAScript5 Array.isarray () IE9 above support
Accurate array method Detection: 22.1.1 conversion method toString () \ valueOf () \ tolocalstring () \ Join () Stack method: The stack is a LIFO data structure. Push () returns the length of the new array pop () returns the item queue method that was last removed from the array: FIFO. Unshift () returns the length of the new array shift () returns the item reordering method that was removed from the front end of the array: reverse () sort () their return value is an array that has been reordered. Reverse () Reverses the order of the array items, but is not flexible the sort () method defaults from small to large, the principle is to call the ToString () method of each item, and then compare the string action method: Concat () connection array slice () intercept array splice () Delete, insert , replacing an item in an array concat () creates a new array without affecting the original array.
Slice () Creates a new array, does not affect the original array, the parameter is subscript, 1 times from the subscript position to the last, 2 when the second subscript is not included.
Splice () Creates a new array that affects the original array. Returns an item that is deleted from the original array, and returns an empty array if no item is deleted the operation method can be understood as: take some useful items from a well-known array to form a new array, which is convenient for us to operate. Location Method: ie9+ indexOf () lastIndexOf () finds the position of a particular item in the array , there is no return-1 (to determine if an array contains an item) ECMA5 defines 5 iterative methods for arrays: (ie9+) has 2 parameters: the given function and the scope in which the function is run-the value that affects this. The given function has 3 parameter items subscript array 1, every () each item conforms to the given function and returns true
2, some () returns true if one of the specified functions is met
3. Filter () to find the item that conforms to the given function, make up the new array
4. Map () Returns the result of running the given parameter, forming a new array
5, ForEach () does not have a return value, just run the given function for each item in the array ECMA5 reduction method: (ie9+) reduce () Reduceright () applies to the evaluation operation
About arrays//create arrays and access arrays var arr1=new array (); Arr1=[1,2,3];var arr2=[];var arr3=["Wu Qiong", 2, "str", {name: "Wu Qiong", age:28}];arr2[0]= 1;arr1[3]=4;arr1[arr1.length]=5;console.log (ARR1); Console.log (arr1[arr1.length-1]);// The index of the last item is always Length-1console.log (ARR2), Console.log (Arr2[0]); Console.log (ARR3); Console.log (arr3[3].name); if (arr1 instanceof Array) {Console.log ("arr1 is an array");} Else{console.log ("arr1 is not an array");} if (Array.isarray (arr2)) {Console.log ("arr2 is an array");} Else{console.log ("ARR2 is not an array");}; Console.log (Arr1.tostring ()); Console.log (arr1.tolocalestring ()); Console.log (arr1.valueof ()); Console.log (  Arr1.join ("/"));//alert (Arr1.join ()); The background is automatically called arr1.tostring (); Console.log (Arr1.push (6,7, "8", "Pop () this item"));//Returns the length of the new array console.log (ARR1); Console.log (  Arr1.pop ());  Returns the extracted item Console.log (ARR1); var arr4=new Array (); Console.log (Arr4.unshift); Returns the length of the new array 2console.log (ARR4); Console.log (Arr4.shift ()); Console.log (ARR4); Console.log (typeof Arr4.shift ()); Console.log (ARR4); var arr5=[0,5,15,10,20, "A", "B", 30];console.log (Arr5.reverse ()); //Changed the original array console.log (Arr5.sort ()); [0,10,15,20,5] converted to a string comparison of the result Console.log (Arr5.sort (A, b) {return a>b}); var colors=[1,2,3];var colors2  =colors.concat (4,[5,6]); Console.log (COLORS2);    Returns a new array var colors3=colors2.slice (1); var colors4=colors2.slice (1,5); var colors5=colors2.slice (1,colors2.length-1); Console.log (COLORS3); Console.log (COLORS4); Console.log (COLORS5);   var cars =["Mercedes", "BMW", "Audi"];var cars1 =cars.splice (0,1); Remove Mercedes-Benz Console.log (Cars); Console.log (cars1); var cars2 = Cars.splice (0,1, "Mercedes", "Buick");//replace BMW Console.log (cars); Console.log (cars2); var cars3 = Cars.splice (2,0, "Volkswagen"); Insert Volkswagen Console.log (Cars), Console.log (CARS3), var age={name: "Wq"};var age1=[{name: "Wq"},2,{a:2,b:3}];if (! Array.indexof) {Array.prototype.indexOf = function (EL) {for (var i=0,n=this.length; i<n; i+                     +) {if (this[i] = = = EL) {return i;                    }} return-1;                }  }console.log (Age1.indexof ({name: "Wq"}));   -1console.log (Age1.indexof (2)); 1console.log (Arr1.indexof ({a:2,b:3})),//-1var iter=[1,2,3,4,3,2,1];var everyresult = iter.every (function (item, Index,array) {return item > 2;}); Console.log (Everyresult); var someresult = Iter.some (function (item,index,array) {return item > 2;}); Console.log (Someresult); var filterresult = Iter.filter (function (item,index,array) {return item > 2;}); Console.log (Filterresult); var mapresult = Iter.map (function (item,index,array) {return item > 2;});   Console.log (Mapresult); This returns an array of Boolean values, var mapResult1 = Iter.map (function (item,index,array) {return item * 2;}); Console.log (MAPRESULT1); Iter.foreach (function (Item,index,array) {//execute some code here if (array[index]>2) {Console.log ( Item);} else{}); Console.log (ITER), Var sum=iter.reduce (function (Pre,cur,index,array) {return pre + cur;}); Console.log (sum);

  

The Advanced way of JavaScript (iii) type object and array type of reference type

Related Article

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.