JS Reference type

Source: Internet
Author: User
Tags sorts

Object type:

Most reference types in JS are instances of type object. There are two ways to create an instance of an object: the first is to use the new operator followed by the object constructor.

var obj = new Object ();
Obj.name = ' long ';

The second type is the object literal.

var obj = {     name:' Long ',
' La La ': 111
};

Access to object properties, generally prefer the '. ' operator, when the property name is a variable or property name does not conform to the ECMAScript identifier specification, can be accessed with ' [] ', if not a variable needs to enclose the property name in quotation marks.

Array type:

Each item in the JS array can hold values of any type.

There are two ways of creating an array: The first is to use the array constructor.

var New    Array (); Not recommended
var arr = Array (); Not recommended

The second type is the literal amount.

var arr = ["Red", "Blue", "green"];

The length property of the array can be read and written.

Detecting arrays:

In the absence of a frame, the array is instrumented:

if instanceof Array) {    //dosomething      }

ES5 new Array.isarray () method. Regardless of which array is created in the global execution environment, it is possible to detect whether an array is in the end. Supported browsers: ie9+, Firefox 4+, Safari 5+, Opera 10.5+, Chrome.

if (Array.isarray (value)) {    //dosomething  }
Conversion method:

An array call to the ToString () method returns a comma-delimited string that is stitched together as a string of each value in the array. Value () returns the array. The join () method splits the array into strings with the specified delimiter.

var arr = ["Red", "green", "Blue"];arr.tostring ();    // "Red,green,blue"arr.valueof ();        // ["Red", "green", "blue"]
Arr.join (' | '); "Red|green|blue"
Console.log (arr); ["Red", "green", "blue"]

None of these methods will change the original array.

Stack method:

Push () Adds an entry at the end of the array, returning the length after the value has been modified. Pop () deletes the last item of the array and returns the deleted item. Each of the two methods changes the original array.

var arr =["Red", "green", "Blue"; var length = Arr.push ("yellow");      // 4Console.log (arr);                     // ["Red", "green", "blue", "yellow"] var Move = Arr.pop ();                 // "Yellow"console.log (arr);                     // ["Red", "green", "blue"]
Queue method:

Shift () deletes the first item of the array and returns the item. Unshift () Adds an entry at the beginning of the array and returns the length of the new array.

Reorder methods:

The reverse () method reverses the array, sort () sorts the array in a small-to-large way (sorts each item as a string). All two methods change the original array and return the changed array.

var arr = [1,2,3,4,11,20];arr.reverse ();                        // [20,11,4,3,2,1]arr.sort ();                          // [1,11,2,3,4,20]            

Sort () can also accept a comparison function as a parameter. The function accepts two parameters: the array compares two items each time, if the function returns-1, the two arguments do not change position, and if 1 is returned, the two arguments are reversed.

var arr = [1,2,3,4,11,20,5];arr.sort (function(prev,next) {     Return prev > next? 1:-1;})                           // [1,2,3,4,5,11,20]

The sort () method can also be used to sort the array objects according to different attributes:

var arr = [    {     "     Long",     "+"     },      {     "Tom",          "1"     },      {     "air",     "2018"},]          
function ReSort (arr,type) {
Return Arr.sort (function (prev,next) {
return Prev[type] > Next[type]? 1:-1;
})
}
ReSort (arr, ' name '); [{name: ' Air ' ...},{name: ' Long ' ...},{name: ' Tom ' ...}]
ReSort (arr, ' age '); [{name: ' Tom ' ...},{name: ' Long ' ...},{name: ' Air ' ...}]
ReSort (arr, ' time '); [{name: ' Tom ' ...},{name: ' Long ' ...},{name: ' Air ' ...}]
Operation Method:

Concat () stitching array. Does not change the original array. Returns the array after stitching.

var colors = ["Red"];
var colors2 = Colors.concat ("Yellow", ["Green", "Black"]);
Console.log (colors); ["Red"]
Console.log (COLORS2); ["Red", "yellow", "green", "black"]

Slice () Creates a new array based on one or more items in the array, accepting one or two arguments, one parameter, and the new array representing the start and end positions of the array, from the parameter position to the end of the array, and two parameters respectively. Returns the new array created without changing the original array.

var colors = ["Red", "Black", "green", "yellow");
Colors.slice (1); ["Black", "green", "yellow"]
Colors.slice (1,3); ["Black", "green"]

Splice () is the most powerful array method. Main purpose inserts an item into the middle of the array.

Delete any item, two parameters, first parameter: To delete the start position of the item, the second parameter: the length of the deleted item. If there is only one parameter, it is removed from the start position to the end.

Insert any item: three parameters, first: Start position, second: to delete the length of the item, the third: the item to be inserted, if you want to insert multiple items, you can accept the four-and-a 、、、.. Any number of items.

Replace: Ibid.

Splice () changes the original array, returns an array of deleted items, and returns an empty array if no deletions are made.

Location Method:

Es5:indexof (), lastIndexOf () did not find return-1. Use the congruent operator when searching.

Iterative methods:

ES5:

Every (): Returns TRUE if each entry returns true;

Filter (): Returns the item that returns true as an array of elements;

ForEach (): no result returned;

Map (): A function that returns the result of each invocation;

Some (): any one of the items is true, then returns true;

Merge method:

Es5:reduce (), Reduceright ()

Two methods iterate over each item of the algebraic group. Then build a value that is ultimately returned. Accepts two parameters: a function that is called on each item and the initial value that is the base of the merge. The function that passes in the method accepts four parameters: the previous value, the current value, the index of the item, and the array object. Any value of the function is automatically passed to the next item as the first argument, and the first iteration occurs in the second item.

var values = [1,2,3,4,5];
var sum = values.reduce (function (Prev,cur,index,array) {
return prev + cur;
})
Console.log (sum); 15
var sum2 = values.reduce (function (Prev,cur,index,array) {
return prev + cur;
},10)
Console.log (SUM2); 25
Date Type:
var New Date ();

The constructor can accept a parameter, date format ("Yyyy-mm-dd Hh:mm:ss"), which returns the date object for that time. Date () is actually the default call to the Date.parse () method or the DATE.UTC () method when the parameter is accepted.

ECMAScript does not define which date format should be supported, so the general wording is acceptable.

var New Date ("One by One");    Mon-Nov 00:00:00 gmt+0800 (China Standard Time) called the Date.parse ()varnew Date (2017,11,13); Wed Dec 00:00:00 gmt+0800 (China Standard Time) call is DATE.UTC ()

The DATE.UTC () method is based on the month of 0.

ES5 added the Date.now () method, which returns the number of milliseconds in the current time. You can actually turn the data object into milliseconds by using the + operator.

var start = +new  Date (); Console.log (start);            1510567596510 number  type
Inheritance method:

The date type also overrides the toLocaleString (), toString (), ValueOf () methods. ValueOf () returns the number of milliseconds for the date object. You can compare sizes directly with date objects.

Date formatting methods:

....

Methods for date, time components:

f12--Open Console-input: date.prototype--Enter to see the Date method supported by the browser.

RegExp Type:

You can create a regular expression by using the following method:

var reg =/pattern/flags;

The pattern section can be any simple or complex regular expression.

Flags:g (Global), I (case insensitive), m (for multiple lines).

The metacharacters in the pattern must be escaped: () {}[]\^$|? *+.

Another way to create a regular expression is to use the RegExp constructor. Accepts two parameters, one is the string pattern to match, and the other is the flag character. You can create a regular with a literal, which you can create with a constructor function. When a variable is contained in a pattern, it is created with a constructor function. Some strings in the constructor pattern require double escaping.

var  str = ' at '; var reg = RegExp (str,g);
RegExp Strength Attributes:

Useless

RegExp instance method:

EXEC (): specifically designed for capturing groups. Accepts a parameter, which is the string to match, returns an array containing the first match information, and returns null if no match is found. The returned array instance contains two additional attributes: Index and Input,index: The position of the match in the string, and input represents the string to which the regular expression is applied. The first item in the array is a string that matches the entire pattern, and the other item is a string that matches the capturing group in the pattern. If there is no capturing group, the array has only one item.

var text = "Mom and dad and baby";
var pattern =/mom (and dad (and baby)?)? /gi;
var matches = pattern.exec (text);
Console.log (Matches.index); 0
Console.log (Matches.input); "Mom and Dad and baby"
Console.log (Matches[0]); "Mom and Dad and baby"
Console.log (Matches[1]); "And Dad and Baby"
Console.log (matches[2]); "And Baby"

EXEC () Even if G is set in the pattern, it will return only one match at a time, and if it is called multiple times, each call will continue to be found in the string.

Test () accepts a string parameter. Returns TRUE or False

RegExp Constructor Properties:

$, $ 、.. $_, $+, $* 、....

Function Type:

  

Let's get here today--.

JS Reference type

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.