JavaScript Advanced reference type (i) JavaScript Advanced Programming (Third edition)

Source: Internet
Author: User
Tags array length array sort instance method

A reference type is a data structure. It is also called a class. It is sometimes referred to as the definition of an object .

Objectis an instance of a particular reference type. I. Type of objectExpression context: Refers to the ability to return a value statement context (statement context): Represents the beginning of a block of statements (example: if statement) Construction Mode:Method for creating an instance of type object: 1. Normal method new Object (); 2. Object literal method var person = {Name: "Zjh", Age: "22"} (expression context) PS: The second method, generally only in consideration of the object property name of the readable Use (object constructors are not actually used when constructing objects using the literal literals) ways to Access object properties:1. Dot notation Person.name (this method is generally used) 2. person["Name"--the property name must be in string form (a bit: 1. Property Name 2 can be accessed through a variable. Property names can include keyword spaces reserved words) two, array typeThe difference between an array in JS and other languages is that it can put all kinds of data in the same set of numbers, and its length is dynamic and does not need to be defined at construction time. Construction Mode:1.new keyword: var colors = new Array (); Or var colors = new Array (' Red ', ' blue '), orvar colors = new Array (88); --Array length = PS: You can omit the New keyword2. Array literal: var colors = []; Orvar colors = [' Red ', ' Blue ']; (PS: The array constructor is not called when literals are used as with objects) ways to access data within an array:Index method: Colors[0]-ps:1 from zero. Returns the corresponding value if the index is less than the array length. Otherwise, the undefined 2 is returned. If the array length is 3, if you set it to 1 then you will remove the two data from the back. Conversely if the length is set very large, then there is no corresponding value of the location is undefined methods for detecting arrays:1. if (instanceof array) {}2. Array.isarray (Array) inherited method (Transform) Method: (Tolocalstring (), toString (), ValueOf ())When using one of the above three methods, the string of the array is returned (each item of the array calls the respective three methods) (delimited) PS: valueof () is called implicitly when calling the (), if a value in the array is null or unde The fined returns an empty string. Stack method: (LIFO Last-in-first-out last-in, first-out data structure)The array also implements this Stack。 Two methods are provided: Push (): Accepts any number of arguments, returns the length of the array after the push-in parameter. Pop (): POPs (returns) the last data into the stack, with the array length-1. Queue method: (FIFO first-in-first-out first-out)Arrays can also simulate Queue。 In fact, the push () method is also added from the end of the queue. There is also a method: Shift (): The ability to fetch (return) the first data in the queue and eject it at the same time queue Length-1. PS: There is also a unshift () method that acts like push (), but operates the data in front of the queue so it and pop () can be combined to simulate a reverse queue that pulls data from the end of the data" Reorder Methods:There are two main methods: reverse (): Reverse sorting in the order of the array sort (sorting method): Special note: Where Nums is a numeric type, there are quotes and no quotation marks. Use of Comparec can be avoided. Operation Method:Arrays provide several ways to copy and intercept arrays. (both copy a copy of the current array and return the value after the operation on the copy) Concat (): The parameter can be normal data or an array slice ():  The first argument is the starting sequence, the second argument is the end sequence (not writable) splice (): The middle of the array can bemakeDelete, insert, replace
Location Method:Provides two parameters that provide the parameter name and a lookup start index, returns the data sequence found, returns -1indexof (), andlastIndexOf ()--looking forward from the end:

Loop Iteration Method:A total of five iterative methods are provided: (You need to provide a function with three parameters: the value of the array item, the position of the item in the array, the array object itself) every ():runs the given function for each item in the array,the operation of each item returns TRUE and eventually returns TrueSome ():runs the given function for each item in the array,returns True if one of the items returns TrueFilter (): Each item in an array runs the given function, and returns a list of items that are true of the function that is composed of foreach ():run the given function for each item in the array, no return valueMap ():runs the given function for each item in the array, returning the arrays that comprise the result of each function callExample:filter ()
Map () To reduce the method:Provides two methods that iterate through each item in the array, and then build a value that is ultimately returned. You need to provide them with a function that receives four parameters (the previous value, the current value, the index of the current value, the array object), it should be noted that any value returned by this function will be the first parameter of the next item, so the first iteration takes place on the second item, the first one as the first parameter of the first iteration.reduce (): Starting with the first itemReduceright (): Starting with the last item third, date typeThe date type of JS is built on the basis of Java's Java.util.Date class. It also uses UTC (1970 milliseconds to start) Construction Mode:var now = new Date (), not written in parentheses, defaults to the time of the moment. You can also specify a time in parentheses, formatted by two methods: Date.parse () and DATE.UTC ()//Automatic call Date.parse ()///The first two parameters must be written, the month is based on 0PS: Take This Moment time provides a method Date.now () If the browser does not support can var star = + new Date (); methods of Inheritance:The date type also inherits the toLocaleString (), toString (), ValueOf () methods but has different output formats in different browsers, and the use value is not high. ValueOf () takes a number of milliseconds to compare the size. methods for formatting dates: Date/Time component method: iv. Types of RegExpRegular expressions Construction Mode:Per syntax: var regExp =/pattern/flags;p Attern-I can be any simple or complex regular expression ... flags---there are three modes (and a regular expression can be followed by multiple modes) G: Global mode    , it is not found that the first match is stopped and will always match to the end.   I: case-insensitive mode. M: Multiline mode, after one line matches, go to the next line. PS: Escape characters include: using the RegExp constructor: (two parameters, the string pattern to match, an optional identity string). It is important to note that using literals and using constructors to get a regular expression is not the same as a literal constructed regular expression literal that always shares a regexp instance. The instance property is not reset, so in the loop, the call again fails. RegExp The properties of the instance:Global: Whether Gignorecase is set: Whether Imultiline is set: Msource is set: The string representation of a regular expression lastindex: Indicates where to start searching for the next occurrence RegExp The instance method:EXEC (): Returns the array containing the first match information test (): Returns whether the matching Boolean property: The position of the index match, the string in which input is matched; The first item in the array is a string that matches the entire pattern. The remainder is the capturing group string that matches the string for the entire pattern (if not, there is only one item) PS: for exec () and test (), if the regular expression has G mode, the next time the expression is called again, the match is started from the last match, and if the G mode is not used, Call the regular expression again and match from the beginning. inherited methods: (Tolocalstring (), toString (), ValueOf ())The first two returns the literal of the regular expression valueof () returns the regular expression itself RegExp Properties of the constructor:The RegExp constructor provides properties that apply to all regular expressions within the scope and vary based on the last regular expression operation performed. PS: Short attribute names are accessed by square brackets. limitations of the model:



From for notes (Wiz)



JavaScript Advanced reference type (i) JavaScript Advanced Programming (Third edition)

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.