JavaScript Learning Notes Collation reference type _javascript tips

Source: Internet
Author: User
Tags prev string format


Reference types are important content in JavaScript. A reference type is a data structure that is used to organize data and functionality together. It describes the properties and methods of a class of objects. object is an underlying type, array is the type, date is the type, regexp is the regular expression type, and so on.



Embrace JavaScript



The once-well-known JavaScript has multiplied with the popularity of Ajax, and now JavaScript is no longer just a dispensable helper in web development, and even has a "JavaScript engineer" dedicated to its position, If you're just a web developer, you have to understand JavaScript, and at least you can see the word "familiar with JavaScript first" in some of the relevant job requirements. Even I have to tell you that you will be able to develop desktop software with JavaScript, thanks to another development model of Adobe AIR, which uses html+css+javascript to develop air.



1.Object type



1. Create:


 
 


Often applies to storing and transferring data. such as storage:


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


The second way to create: (When you create a property name can also be a string format, that is, you can enclose the property name in quotes.) )


var person = {
Name: "Nicholas",
age:29


2. Remove attribute value: person["name"; or: Person.name;



2.Array type



The same array can hold any type of data (a hodgepodge).



1. Array can be dynamically adjusted (add a data, it itself grows a length, not dead. )。



2. Create:


var stars=new array ();//mode 1
var stars=new array (20);//Mode 2
var stars=new array ("Jay Chou", "JJ Lin", "Stefanie Sun");//Mode 3
var stars=array (20);//Mode 4


3. Dynamic Tuning Example:


var stars=["Jay Chou", "JJ Lin", "Stefanie Sun"];
Stars[1]= "JJ";//Dynamic Change (the JJ into JJ)
stars[3]= "Leather pants Wang"; dynamic growth (plus a length)


4. Detection array: Array.isarray (value);



5. Use Join () to convert the array into a delimited string:


var stars = ["Jay Chou", "Wang Nima", "Zhangquan egg"];
Alert (stars. Join (",")); Jay Chou, Wang Nima, Zhangquan eggs


6. You can use arrays like stacks (pop () out, push () in).



7. You can use arrays like queues. (Combine shift () and push ()):


var stars = new Array (); Create an array
var count = Colors.push ("Jay Chou", "Wang Nima");//push two items
alert (count);//2
count = stars. Pus H ("Zhangquan egg"); Push another item on
alert (count);//3
var item = Colors.shift ();//get the item
alert C12/>alert (colors.length); 2


8. Sort.



1.reverse () flip array Order; (returns the sorted array)



2.sort () sorted from small to large. But they are sorted by string, not by number: (returns the sorted array).


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


To sort by the way you expect, you can add a comparison function to the sort () as an argument:


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


A simplified version of the comparison function (sort is only concerned with returning positive, negative, or 0):


function Compare (value1,value2) {return
value2-value1; 


9. The operation of the array: coupling, slicing, splicing.



1. Connection : Use Concat, Memory: concat-->concatenate: Link, chain.



Example:


var stars = ["Jay Chou", "Wang Nima", "Zhangquan egg"];
var stars = stars. Concat ("Crown Princess", ["Flower Thousand Bone", "Meichangsu"]);
Alert (stars); Jay Chou, Wang Nima, Zhangquan eggs 


2. Slice . Use slice, Memory: Slice translation: slices. Example:


var stars = ["Meichangsu", "Yu Wang", "Jing Wang", "Neon Phoenix", "Liu Fei"];
var stars= stars.slice ();
var stars= stars.slice (,);
Alert (stars); Yu Wang, Jing Wang, Neon Phoenix, Liu Fei (from the first position to start cutting)


3. Splicing . Splice function is powerful. You can delete, insert, replace.



1. Delete any number of items: for example: Splice (0,2), delete the No. 0, 1 (half-closed interval) (return delete item).



2. Insert any number of items at the specified location: for example: Splice (2,0, "Jay Chou", "Wang Nima"), insert Jay Chou, Wang Nima two items from the 2nd position.



3. inserts any number of items at the specified location and deletes any number of items at the same time. For example: Splice (2,1, "Jay Chou", "Wang Nima"), removed from the 2nd position 1 items, and then began inserting Jay Chou, Wang Nima two.



10. Location method: Indexof,lastindexof;



11. Iterative methods: divided into: all qualified to pass, any one pass, filter some slag, one-to-one mapping, iterative query, reduction.



1. All qualified to pass:


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


Returns true if each entry in the previous example is greater than 2.



2. Any one qualified to pass:


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


In the example above, returns True if one is greater than 2.



3. Filter part slag:


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


In the example above, filter out more than 2.



4. One-to-one mapping:


var numbers = [1,2,3,4,5,4,3,2,1];
var mapresult = Numbers.map (function (item, index, array) {return
item * 2;
});


In the example above, multiply each item by 2.



5. Iteration: Using For-each.



6. Reduction: Reduce.


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


The cumulative sum returns, with 5 items reduced to 1 items.



3.REGEXP type



1.var expression=/pattern/flags;



2.flags three : g (Global mode, applied to all strings), I (case-insensive, ignore letter case), M (multiline, multiline mode, one line checked and next line.) )。 Example:


/** matches all instances of ' at ' in the string * *
var pattern1=/at/g;
/** matches the first ' bat ' or ' cat ', which is case-insensitive/
var pattern2 =/[bc]at/i;
/** matches all 3-character combinations at the end of ' at ' and is not case-insensitive


3. all meta characters in the pattern must be escaped, metacharacters: ({[\ ^ $ |)? * +.]}



4.Function Type



1. Each function is an instance of the functions type and has properties and methods, as well as other reference types.



2. Two ways 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 has no overloads.



5.Boolean, Number, String: Basic packing type


var a= "Jay Chou is a superstar";


In the example above, a is a basic type, but a can call the substring method because the background automatically completes the wrapper operation of a and creates an instance of the string type. Boolean,number is similar.



6. Monomer built-in objects, do not need to instantiate, direct use, such as: Math,global.



1. All the functions and variables defined in the global scope are the methods of the global object, such as Parseint,isnan.



The 2.eval () method is also a method of the global object, which is responsible for parsing JavaScript.



The 3.Math object is to save mathematical formulas and related information. It has many methods, such as: min for the minimum value, max for maximum, ceil () up, floor down, round rounded, random take random number.



PS: Reference type understanding: Variable Exchange equals the key of an existing store (variable reference address) copy one to the other boss, when two bosses manage a store at the same time, and the behavior of two bosses can have an impact on the operation of a store.



Reference type Example


function Chainstore ()
{
var store1=['Li-Ning '];
var store2=store1;
Alert (store2[0]); Li-Ning store1[0]= ' Li-Ning China '
Alert (store2[0]); Li-Ning China
}
Chainstore ();
In the above code, Store2 only one assignment, theoretically its value has been fixed, but later by rewriting the value of Store1, found that Store2 value has also changed, which is the characteristics of the reference type, but also we should pay attention to the place
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.