Reference Type _ JavaScript tips for sorting javascript learning notes

Source: Internet
Author: User
The reference type is a very important part of JavaScript. This article introduces the reference type of JavaScript learning notes, if you are interested in js reference types, learn about them. reference types are very important in JavaScript. A reference type is a data structure used to organize data and functions together. It describes the attributes and methods of a class of objects. Object is a basic type, Array is an Array type, Date is a Date type, RegExp is a regular expression type, and so on.

Embracing JavaScript

Once well-known JavaScript has doubled its value with the popularity of AJAX. Now JavaScript is no longer just a dispensable auxiliary tool in WEB development, but it even has its own position as a "JavaScript engineer ", even if you are just a WEB Background development programmer, you must understand JavaScript. At least you can see the word "familiar with JavaScript first" in some recruitment requirements. I even want to tell you that you can use JavaScript to develop desktop software, thanks to another development mode of Adobe AIR, that is, using HTML + CSS + JavaScript to develop AIR.

1. Object Type

1. Create:

var dog = new Object(); 

It is often used to store and transmit data. For example, storage:

var person = new Object();person.name = "Nicholas";person.age = 29; 

Method 2: (the attribute name can also be in the string format when it is created, that is, the attribute name can be enclosed by quotation marks .)

var person = {name : "Nicholas",age : 29}; 

2. Retrieve the property value: person ["name"]; or: person. name;

2. Array type

The same array can store any type of data (hodgedge ).

1. the array can be dynamically adjusted (if you add more data, it will increase the length by itself, not dead .).

2. Create:

Var stars = new Array (); // method 1var stars = new Array (20); // method 2var stars = new Array ("Jay Chou", "Lin Junjie ", "Sun Yanzi"); // method 3var stars = Array (20); // method 4var stars = ["Jay Chou", "Sun Yanzi", "Lin Junjie"]; // Method 6

3. Dynamic Adjustment Example:

Var stars = ["Jay Chou", "Lin Junjie", "Sun Yanzi"]; stars [1] = "JJ"; // dynamic change (Change Lin Junjie to JJ) stars [3] = "leather pants Wang"; // dynamic growth (with a length added) stars. length = 1; // dynamically forcibly scale down (Lin Junjie, Sun Yanzi, leather pants Wang force remove, length changed to 1)

4. Check Array: Array. isArray (value );

5. Use join () to convert the array into a separator string:

Var stars = ["Jay Chou", "Wang Nima", "Zhang Quan egg"]; alert (stars. join (","); // Jay Chou, Wang Nima, Zhang quanegg alert (stars. join ("-"); // Jay Chou-Wang Nima-Zhang quanegan

6. You can use arrays (pop () and push () Like stacks ).

7. Arrays can be used like queues. (Combined with shift () and push ()):

Var stars = new Array (); // create an arrayvar count = colors. push ("Jay Chou", "Wang Nima"); // push two itemsalert (count); // 2 count = stars. push ("Zhang Quan egg"); // push another item onalert (count); // 3var item = colors. shift (); // get the first itemalert (item); // Jay Chou alert (colors. length); // 2/** the so-called stack-to-queue change refers to reversing the stack and pulling it again */

8. sort.

1. reverse ()Returns the sorted array)

2. sort ()Sort data in ascending order. But it is sorted by string, not by number: (returns the sorted array ).

var values = [, , , , ];values.sort();alert(values); //,,,,

To sort by the way you expected, you can add a comparison function to sort () as a parameter:

function compare(value, value) {if (value < value) {return -;} else if (value > value) {return ;} else {return ;}}var values = [, , , , ];values.sort(compare);alert(values); //,,,, 

Simplified version comparison function (sort only cares about whether the returned result is positive, negative, or 0 ):

function compare(value1,value2){return value2 - value1; } 

9. Operations on Arrays: joining, slicing, and splicing.

1. Join: Use concat, memory: concat --> concatenate: Link, chain.

Example:

Var stars = ["Jay Chou", "Wang Nima", "Zhang quanegg"]; var stars = stars. concat ("Taizi Fei", ["Hua qianggu", "Mei changsu"]); alert (stars); // Jay Chou, Wang Nima, Zhang quanegg alert (stars ); // Jay Chou, Wang Nima, Zhang Quan egg, Taizi Fei, Hua qigu, Mei changsu

2. Slice. Use slice, memory: slice Translation: slice. Example:

Var stars = ["Mei changsu", "Yu Wang", "Jing Wang", "Fei Liu"]; var stars = stars. slice (); var stars = stars. slice (,); alert (stars); // Yu Wang, Jing Wang, Jing, Fei Liu (switch from the first position) alert (stars); // Yu Wang, jing Wang, Jing (switch from the first position to the first position, indicating semi-closed, not included)

3. Splicing. Splice. Powerful functions. It can be deleted, inserted, or replaced.

1.Delete any number of items: for example, splice (), delete item (semi-closed range) (return to delete items ).

2.Insert any number of items at the specified position, for example, splice (2nd, "Jay Chou", "Wang Nima"). Insert Jay Chou and Wang Nima from the second position.

3.Insert any number of items at the specified position and delete any number of items at the same time. For example, splice (2nd, "Jay Chou", "Wang Nima") deletes one item from the first position, and then inserts Jay Chou and Wang Nima.

10. Location Method: indexOf, lastIndexOf;

11. iteration method:Divided into: all pass, any pass through, filter part of the slag, one-to-one ing, iterative query, reduction.

1. All pass:

var numbers = [1,2,3,4,5,4,3,2,1];var everyResult = numbers.every(function(item, index, array){return (item > 2);});alert(everyResult); //false 

In the preceding example, if each item is greater than 2, true is returned.

2. Any one of them will pass the test:

var numbers = [1,2,3,4,5,4,3,2,1];var someResult = numbers.some(function(item, index, array){return (item > 2);});alert(someResult); //true 

In the preceding example, if one of the values is greater than 2, true is returned.

3. Filter partial slag:

var numbers = [1,2,3,4,5,4,3,2,1];var filterResult = numbers.filter(function(item, index, array){return (item > 2);});alert(filterResult); //[3,4,5,4,3] 

In the preceding example, all the values greater than 2 are filtered out.

4. One-to-one ing:

var numbers = [1,2,3,4,5,4,3,2,1];var mapResult = numbers.map(function(item, index, array){return item * 2;});alert(mapResult); //[2,4,6,8,10,8,6,4,2] 

In the above example, multiply each item by 2.

5. iteration: Use for-each.

6. reduce: reduce.

var values = [1,2,3,4,5];var sum = values.reduce(function(prev, cur, index, array){return prev + cur;});alert(sum);//15

Return the sum of values. Five values are reduced to one.

3. RegExp type

1.Var expression =/pattern/flags;

2. There are three types of flags:: G (global mode, applicable to all strings), I (case-insensive, case-insensitive), m (multiline, multiline mode, one row is verified and then the next line .). Example:

/** Match all 'at' instances in the string */var pattern1 =/at/g;/** match the first 'bat 'or 'cat ', case-insensitive */var pattern2 =/[bc] at/I;/** match all three character combinations ending with 'at', case-insensitive */var pattern3 = /. at/gi;

3.All metacharacters in the mode must be escaped. metacharacters: {[\ ^ $ | )? * +.]}

4. Function Type

1.Each Function is a Function-type instance, and has attributes and methods like other reference types.

2. Two methods to define a function:

Method 1:

function sum(a,b){return a + b; } 

Method 2:

var sum=function(a,b){return a + b;} 

3. The function is not overloaded.

5. Boolean, Number, and String: basic packaging type

var a="Jay Chou is a superstar";var b=a.substring(0,8); 

In the above example, a is a basic type, but a can call the substring method because the backend automatically completes the packaging operation of a and creates an instance of the String type. Boolean and Number are similar.

6. A single built-in object that is directly used without instantiation, such as Math and Global.

1. All functions and variables defined in the Global scope are Global object methods, such as parseInt and isNaN.

2. the eval () method is also the Global Object method, which is responsible for parsing javascript.

3. The Math object stores mathematical formulas and related information. It has many methods, such as min for the minimum value, max for the maximum value, ceil () for the upward integer, floor for the downward integer, round rounding, random for the random number.

Ps: Reference Type understanding: the exchange of variables is equivalent to copying the key (variable reference address) of an existing store to another boss. At this time, the two bosses manage the same store at the same time, the actions of both shopkeepers may affect the operation of a store.

Reference Type example

Function chainStore () {var store1 = ['nike China']; var store2 = store1; alert (store2 [0]); // Nike Chinastore1 [0] = 'nike U. s. a. '; alert (store2 [0]); // Nike U. s. A .} chainStore (); // In the above Code, store2 only performs a value assignment once. Theoretically, its value has been set, but it is rewritten later by the store1 value, it is found that the value of store2 has also changed, which is the feature of the reference type, which is also worth attention.
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.