JavaScript Learning Notes (iv) reference types

Source: Internet
Author: User
Tags alphanumeric characters

A reference type is a data structure used to organize data and functionality together.

Object is the most used reference type, you can use new to define an instance, or you can use the object literal .

Here is a summary of how the array type is eaten.

There are several ways to declare an array type:

//Constructor MethodvarMyArray =NewArray ();//declares an empty array, new can be omitted, not recommendedvarMyArray =NewArray (10);//declares an array of 20 empty locationsvarMyArray =NewArray ("Apple", "orange", "banana");//Direct Pass Value//array literal notationvarMyArray = [];//declaring an empty arrayvarMyArray = ["Apple", "orange", "banana"];//array of three elementsvarMyArray = [1, 2,];//not recommended, the browser performance is uncertain

The array can be dynamically modified, including the length property of the array, which can also be modified by default if the array is saved undefined. The following summarizes the various methods commonly used in arrays:

varMyArray = ["Red", "green", "blue"]; Array.isarray (MyArray);//determines whether an array is//instance MethodMyArray;//Direct Output ["Red", "green", "blue"]Myarray.valueof ();//Direct Output ["Red", "green", "blue"]Myarray.tostring ();//output "Red,green,blue"Myarray.join ();//use, concatenate, and output strings for each item in the array.Myarray.join ("| |");//"red| | green| | Blue "//Stack MethodMyarray.push ("Gray");//Append end element, return array lengthMyarray.pop ();//returns the item that was removed//Queue MethodMyarray.push ("Gray");//and the sameMyarray.shift ();//returns the removed item from the front of the arrayMyarray.unshift ("Orange");//adds a new item to the front of the array and returns the array length//sorting methods (sort directly based on the original array)Myarray.reverse ();//invert an arrayMyarray.sort ();//sort the array, note, sort the strings!! Myarray.sort (function(A, B) {returnA-B;});//Sorting for numbers//Operation MethodMyarray.concat ();//Merge Add array element, parameter can be single or array, return new arrayMyarray.slice (S, e);//returns the item labeled [S,e] in the array, without affecting the original array, and if there is a negative number in S or e, the negative number plus the array length is evaluated, and if s>e, returns an empty//The splice method, which returns an array of removed items, and affects the original arrayMyarray.splice (0,2);//starting from 0 items, delete two itemsMyarray.splice (2,0, "Red", "blue");//starting with 2 items, add two newMyarray.splice (2,1, "Red", "blue");//Delete the second item and replace it with the new two items//location method, using congruent comparisonMyarray.indexof ("Blue");//Returns the subscript in the array, unable to find the return-1Myarray.lastindexof ("green");//with the top, starting at the end//Iterative Methods//all accept a function as parameter function (Item,index,array) {}//item, current item; index, current subscript; array, which is a reference to the arrays themselves. Myarray.every ();//returns true for the incoming function, the function returns TrueMyarray.some ();//The function returns True if there is a return true for the incoming functionMyarray.filter ();//for an incoming function, returns an array of all the items whose result is trueMyarray.map ();//for incoming functions, returns an array of each invocation resultMyarray.foreach ();//performs an incoming function in an array, with no result returned//Merge Methodmyarray.reduce (); Myarray.reduceright ();

For the date type, the default new date () returns the current date. Date.parse () accepts a date string that returns the number of milliseconds that the date is returned, and DATE.UTC () accepts the number of milliseconds, but remembers that the month is calculated starting at 0. Date.now () returns the number of milliseconds when this function is called . Also note that getDate () returns the number of days in January (1-31), while Getday () returns the day of the Week (0-6).

About the type of regexp, I am the color pens, Mao did not learn, and so learned to open a separate summary.

About the function type, I think that JS a lot of essence are from functions, so allow me to sum up a well.

The first function is an object, and there are two ways to declare a function. One is to define functions directly through traditional declarative methods, and the other is a function expression that assigns an anonymous function to a variable to hold the function. There is only one difference between the two declarative methods, which is that the former will have a function to declare the Ascension process, and the parser will pre-read the function declaration and be available before any code executes, which does not have this feature. Function name is only a function of reference, because of this feature, JS does not have a function overload, because at the same time define two functions of the same name, the function of the definition of the function is to re-assign value, the previous function is overwritten by the definition, so there is no overloading.

There are two special properties within the function:arguments andthis. There is a callee property in arguments, which is a pointer to the function that owns the arguments. Callee This property is primarily used to de-name the coupling, especially in recursive functions used to reference itself will be stable and secure. The this pointer points to the environment object of the current function, or to the Window object if it is global.

Each function contains two attributes the length and Prototype,length properties represent the number of arguments that the function expects to receive, and prototype represents the prototype object pointer for the function, which is then summarized separately. Each function also contains two non-inherited internal methods: apply () and call (), which are almost identical in that they allow the function to execute in a particular scope, which is equivalent to specifying the function's this value to run. The only difference is that the Apply function receive parameter is in the form of an array and the call function needs to pass in the parameter one. These two functions are primarily used to extend the scope of a function.

For Number,boolean and string three basic types have corresponding basic wrapper types in Es, which is why, although they are basic types, they also have a way to work with the data. However, the background creates a wrapper type for the base type and then destroys the wrapper type after performing the required method operation. Here are some ways to focus on string basic wrapper types. The string type is much like a character array, so many of the array's methods apply under the string type, such as concat, Slice, indexOf, LastIndexOf, and so on. The following summarizes some functions that are unique to string types:

//substring method has no effect on the original stringvarmystr = "Hello World"; Mystr.slice (3, 7);//returns the position of the string from 3-6, "Lo w"Mystr.substring (3, 7);//returns the position of the string from 3-6, "Lo W", with the sliceMystr.substr (3,7);//7 characters from ground 3, "Lo worl"Mystr.slice (3,-4);//Slice encountered a negative value will be added to the string length of the operation, "Lo W"Mystr.substring (3,-4);//Converts all negative numbers to 0 and starts with the smallest number, that is, a similar call to Mystr.substring (0,3); return "Hel"Mystr.substr (3,-4);//substr will be the first negative plus the total length, the second negative to 0, so the equivalent to call the MYSTR.SUBSTR (3,0); return an empty stringMystr.trim ();//used to remove spaces before and after a string, returning a copyMystr.tolowercase ();//Turn lowercaseMystr.touppercase ();//Turn CapitalMystr.localecompare (ANOTHERSTR);//If this string returns 0 the same as ANOTHERSTR, the alphabetical order is followed by 1, before returning-1

With regard to the monolithic built-in objects, global is the globally object of ES, but only a definition, in the Web environment, the window object acts as the global role. There are several methods in the global object that require attention, such as encodeURI (), encodeURIComponent (), and Decoded decodeURI (), decodeURIComponent (), which are encoded for URIs. The encodeURI () encoding is converted to a space of% 20 and the other characters are intact, but encodeuricomponent () replaces all non-alphanumeric characters with the corresponding encoding. So generally for the entire URI with the former, the latter is used for characters appended to the URI. There is also a method called eval ()in Global, which is equivalent to an ES interpreter that passes in the ES code and executes. However, notice that the function declared in eval () cannot declare an elevation.

The single built-in object also has a very important math object, which encapsulates a lot of practical mathematical methods. Not summarized here.

JavaScript Learning Notes (iv) 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.