Learn more about the--object, Array of JavaScript reference types

Source: Internet
Author: User
Tags array sort javascript array

1. Type of Object

Objects are instances of a particular reference type, and new objects are created in two ways:

I, using the new operator to call the constructor to create.

1 var New Object (); 2 person.name = "Zhangsan"; 3 person.age = 20;

II. Using object literal notation to create objects simplifies the creation of objects that contain a large number of attributes.

1 var person = {2     name = ' Zhangsan ',3Age     =    4  };

var person = {} <==> var person = new Object (); Creates an object that contains only the default properties and methods. Object constructors are not called when objects are created by object literals.

Object literals are also the preferred way to pass a large number of optional parameters to a function, usually with named parameters, and a large number of optional parameters are encapsulated with object literals. here the display method does not have a named parameter and can be obtained through the arguments parameter object to the passed-in parameter list. Arguments[1] gets this anonymous object. The properties of an object can be accessed by an object. property or Object ["property"],person.name or person["name"].

function display () {    alert (arguments[0]);    Console.log (typeof arguments[1].age);}    Display (123, {name: "Zhangsan", age:20});//Output 20

2. Array type

Array of Ecamscript each item can hold any type of data, that is, if the first item holds the string, the second item can hold the number ... The size of the array can be dynamically adjusted, and as the data is added automatically to accommodate the new data, the length of the Java array is constant, the array is a bit powerful ... this feature of the JavaScript array feels a bit like a list collection in Java. It's written to think about when to put an array or a collection in Java, without being irrelevant.

There are two ways to create an array:

1 // 1.2varnew  Array (); 3 var New Array ("Red", "blue"); // you can omit the new operator and if you see it, don't be surprised 4 5 // 2. Creating an array with array literals 6 var colors = ["Red", "Blue"];

The length property of the array: is not read-only and can be changed by modifying the lenght property of the array (adding elements, deleting elements);

var colors = ["Red", "blue"= 3; Console.log (colors[//undefined

At this point the length of the array becomes 3, and the last statement will output undefined;

It is also convenient to add new items at the end of the array by using the Length property. This is more interesting ... We all know that the subscript of the array starts with 0 and the maximum subscript is lenght-1.

1 var colors = ["Red", "Blue"]; 2 Colors[length] = "BLACK";

Here's a powerful way to start with arrays. (Not all)

1, Stack method: The data structure of the stack is LIFO
Push (): Adds the element to the end of the array, returns the array length, pops (): Moves the last element at the end of the divisor and returns the removed element.

2. Queue method: FIFO
Shift (): Moves the first item in the array and returns the item, Unshift (): Adds the element to the first item of the arrays.

3, Reorder method: Sort (), reverse (), I think the two methods are useful for everyone to see the name should know. Here's a look at the sort method.

1 var values = [1, 2, 5, ten, +]; 2 alert (Values.sort ()); // 1,10,15,2,5

  The result of the return is a bit out of our expectation, because even if the array sort is all numbers, the comparison is the size of the string, that is, the comparison of what code again ... So how do you solve the normal sort of numbers? The sort method can receive a comparer method, and when this returns a negative number, it puts the value1 in front, returns 0 for two, and returns a positive value to the back of the value1.

1 var values = [1, 2, 5, ten, +]; 2 alert (Values.sort (Compare));  // 1,2,5,10,15 3 function Compare (value1, value2) {4     return value1- value2; 5 }

4. Operation method

Concat (): The incoming array can be spelled to the end of the original array
Slice (): A string-like interception method substring
Splice (): A very powerful method that is primarily used to insert items into an array. You can also delete items and replace items.
Delete Splice (the first position to delete, the number of items to delete),
Insert Splice (insert position, 0, insert content);
Replace splice (position, number of replacements, replacement content);

5. Position method

IndexOf (): Find lastIndexOf from the beginning (): Find at the end
The comparison uses the congruent operator (= = =), so the items to be searched must be strictly equal. PS: Do you really understand the = = in JavaScript?

  6. Iterative method

Every (), filter (): Returns an array that matches the condition, which is useful when you want to filter items that meet certain criteria. foreach (), map (): Returns an array of specific processing, some ().

1 var New Array (1, 2, 3, 4, 5, ten); 2 var newarray = Num.filter (function(item, index, array) {return item > 2;}); 3 alert (NewArray); // 3, 4, 5, ten

Over. If you feel good, please help me with some suggestions, give me some motivation. There are also mistakes in the place to welcome everyone to point out the common learning progress. Thank you for browsing!

Learn more about the--object, Array of JavaScript reference types

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.