Delve into JavaScript advanced programming objects, arrays (stack methods, queue methods, reordering methods, iterative methods) _javascript techniques

Source: Internet
Author: User
Tags javascript array

Inheritance is one of the most talked-about concepts in oo language. Many OO languages support two ways of inheriting: interface inheritance and implementation inheritance. Interface inheritance inherits only the method signature, while implementing inheritance inherits the actual method. As described, interface inheritance cannot be implemented in ECMAScript because the function is not signed. ECMAScript only supports implementation inheritance, and its implementation inheritance relies primarily on the prototype chain.

1. Defining objects using object literals

var person={};

When you create an object in this way, the object constructor is not actually invoked.

Developers prefer the syntax of object literals.

2. Sometimes when you need to pass a large number of optional parameters, you generally use object literals to encapsulate multiple optional parameters.

3. The difference between the point representation of the object attribute and the square bracket notation

(1) Functional: There is no difference between the two

(2) But the square bracket is a bit of a variable that can access the property

For example:

  var person={
  Name: "Nic"
}

Dot notation: Person.name

Square bracket notation: var prop= "name";
Person[prop]

(3) There is another advantage:

If the property name contains a character or keyword that causes a syntax error, the square brackets are not wrong when the reserved word is used

For example: person["the" "Name"]= "OK";

(4) Usually, it is recommended to use dot notation

4. To create an array problem

var colors=[1,2,]//don't do that. This creates an array that contains 2 or 3 items
var opy=[,,,,,]//don't do that. This creates an array that contains 5 or 6 items

This is because IE8 and previous versions have bugs in implementing array literals

Array constructors are not invoked when creating arrays with literal quantities

5. If you set an index for a value that exceeds the number of existing items in the array.

such as: Var color=[1,2,3]

COLOR[3], the array is automatically incremented to the length of the index value plus 1

At this point, the value of color[3] is undefined

6. The length of the array is not only read-only. You can continue to add new items to the end of the array by setting the length of this property.

7. Array conversion to string toString () join ()

Array.tostring ()  ///Returns a comma-delimited string
array.valueof ()  //Return or array
array.join (",")  //CAN

8. Stack method of array push () pop ()

A stack is a data structure in which the most recently added item is first removed (LIFO). The insertion and removal of items in the stack only occurs in one position-the top of the stack.
ECMAScript provides a push () and Pop () method to implement this stack.

The push () method adds one or more elements to the end of the array and returns a new length.

The Pop () method is used to delete and return the last element of the array.

Example:

var arr=[];
var count=arr.push (' A ', ' B ');  count=2
Arr.push (' C ');
var item=arr.pop ();  Remove the last item C item=c and change the length of the array

9. Queue method Shift () Unshift ()

Access rules for queue data are advanced first out
The ECMAScript provides shift () to implement.
The shift () method deletes the first element of an array and returns the value of the first element.
The Unshift () method adds one or more elements to the beginning of the array and returns a new length.

10. Reorder Method Sort () reverse ()

ECMAScript provides the sort () and reverse () to implement.

Sort () calls the ToString () method of each array item to compare the resulting string.

11. Array stitching concat ()

The Concat () method is used to connect two or more arrays.

This method does not change an existing array, but merely returns a copy of the connected array.

The 12.slice () method returns the selected element from an existing array.

13. Location Method: IndexOf () and LastIndexOf ()

14. Iterative method

ECMASCRIPT5 defines the following 5 methods, all of which receive three parameters: the value of an array item, the position of the item in the array, and the array to itself

Every (), filter (), ForEach (), map (), some ()

Example:

var num=[1,2,3,4];
var res=num.every (function (item,index,array) {
  return (item>2)
})  //false  must be greater than 2 for each item before returning true
var res=num.some (function (item,index,array) {
  return (item>2)
})  //true returns True
whenever one is greater than 2 var res=num.filter (function (item,index,array) {return
  (item>2)
})  //[3,4]   
var res= Num.foreach (function (item,index,array) {return
  (item>2)
})  //[1,4,9,16]  

Iterative methods in JavaScript array objects

/* The Iterative method in JavaScript array objects * ECMASCRIPT5 defines 5 iterative methods for arrays. 


 Each method accepts two parameters, the first is the function that is iterated, and the second is the scope object "optional" for the function. 


* The function of the iteration accepts three parameters, the first is the value of the element to be iterated in the array, the second is the position of the element that the array will always iterate over, and the third is the iteration group itself. * 1. Every () runs the given function for each item in the array, and returns True * 2 if the function returns true for each item. 
 Filter () Each item in the array runs the given function, and returns an array of the items that the function returns True. * 3. Each item in the ForEach () array runs the given function, and this method does not return a value of * 4. Map () Each item in the array runs the given function, which returns the array * 5 of the result of each function call.  Some () each item in the array runs the given function, and returns TRUE if the function returns true for any of the items. * * These iterative methods support browsers have, Ie9+,firefox2+,safari3+,opera 9.5+,chrome/var 
num = [1,2,3,4,5,6,7,8,9]; 
  var everyresult = num.every (function (item, index, array) {if (item > 2) {return true; 
} 
}); 
alert (Everyresult); 
  var someresult = num.some (function (item) {if (item > 2) {return true; 
} 
}); 
alert (Someresult); 
  var filterresult = num.filter (function (item) {if (item > 2) {return true; 
} 
}); 
alert (Filterresult); 
  var mapresult = Num.map (function (item) {if (item > 2) {return true; 
} 
}); 
alert (Mapresult); VaR Foreachresult = Num.foreach (function (item) {if (item > 2) {return true; 
} 
});  alert (Foreachresult);

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.