JavaScript Advanced Programming (3rd Edition) Note--chapter5: reference type

Source: Internet
Author: User
Tags access properties

Chapter5 Reference type

The contents of this chapter:

L Use objects

L Create and manipulate arrays

L understand basic JavaScript types

L use basic type and basic packing type

Technically, JavaScript is an object-oriented language, but it does not have the basic structure of classes and interfaces supported by traditional object-oriented languages.

L reference types are sometimes referred to as object definitions because they describe the properties and methods that a class of objects have.

An L object is an instance of a particular reference type. ECMAScript provides a number of native reference types.

5.1 Object type

L How to create an object instance:

    1. The new operator is followed by the object constructor

2. Using object literal notation (a method that developers prefer)

Object literals are shorthand for object definitions to simplify the process of creating objects that contain a large number of properties. When you define an object by object literal, you do not actually call the object constructor.

When you use object literal syntax, the property name can also be used as a string.

When using object literal syntax, if you leave the curly braces blank, you can define an object that contains only the default properties and methods.

L Object literals are also the preferred way to pass a large number of optional parameters to a function.

"In general, named parameters are easy to handle, but are not flexible enough with multiple optional parameters. The best practice is to use named arguments for those required values, and use object literals to encapsulate multiple optional parameters "

L Access Object Properties

    1. Dot notation (point notation is recommended unless you use variables to access attributes)

2. square brackets notation

The main advantage of square brackets syntax: You can access properties by using variables

If the property name contains a character that causes a syntax error, or if the property name uses a keyword or reserved word, you can also use square bracket notation

5.2 Array type

L ECMAScript each item of an array can hold any type of data.

The size of the L ECMAScript array can be dynamically adjusted to automatically grow as the data is added to accommodate the new data.

L ECMAScript Array Creation method:

    1. Array constructor (you can also omit the new operator)

2. Array literal notation

Different number of items can be created because of different browsers. So it's best not to use this syntax.

L Read and set the value, use square brackets and provide the appropriate number index based on 0

L Array Length: not read-only. By setting this property, you can remove an item from the end of the array or add a new item to the item array

Add elements quickly with length

5.2.1 Detecting Arrays

L instanceof operator

Problem: It assumes that there is only one global execution environment

If the page contains more than one frame, there are actually more than two different global execution environments, and there are more than two different versions of the array constructors

L Array.isarray () method

Solve the problem of instanceof operator, ECMAScript5 new

5.2.2 Conversion Method

L toString (): A comma-delimited string that returns the string form of each value in the array

L valueOf (): Returns an array that actually calls the ToString () method of each item of the array

L tolocalestring (): Creates an array of strings that are worth a comma-delimited string. The array is called each item tolocalestring (). "Typically returns the same value as the ToString and ValueOf methods"

Methods different comparisons:

The Tostring,valueof,tolocalestring method for array inheritance, by default, is comma-delimited "

L Join () method: Can receive parameters, that is, a string used as a delimiter

"If you do not pass any value to the join method or pass it to undefined, use a comma as the delimiter." IE7 and earlier versions would incorrectly use the string "undefined" as the delimiter "

"If the value of an item in the array is null or undefined, the value is represented as an empty string in the result returned by the Join,tolocalstring,tostring,valueof method."

5.2.3 Stack Method (LIFO)

The L ECMAScript array provides the push () and Pop () methods to implement a stack-like behavior

Push (): Accepts any number of arguments, adds them to the end of the array, returns the length of the modified array

Pop (): Removes the last item of an array, reduces the length value, and returns the removed value

You can use the Stack method with other array methods

5.2.4 Queue Method (FIFO)

L Use the push () and shift () methods to use arrays like queues

Shift (): Moves the first item in the array and returns the item

L Use the Unshirt () and Pop () methods together to reverse the use of the queue

Unshirt (): In contrast to shirt, add any item to the front of the array and return the length of the new array

5.2.5 Reorder Methods

L reverse (): Reverses the order of array items

L Sort ():

    1. When parameters are not accepted: Array items are sorted in ascending order

"Sort calls the ToString transformation method of each array item, compares the resulting string , determines how the sort" will appear "10" in front of "5"

2. When comparing functions are accepted:

The comparison function receives two parameters, returns 1 if the first parameter should be before the second argument, or 0 if two parameters are equal, or 0 if the first parameter should be located after the second argument

Comparison functions are simpler for array types or their valueof () methods

5.2.6 Operation Method

L Contact (): first create a copy of the current array, add the received parameters to the end of the copy, and return the newly constructed array

L Slice (): Creates a new array based on one or more items of the current array.

    1. Receives two parameters, that is, the start and end position of the item to return (the entry between the start and end items, but not the item that contains the end position)
    2. In the case of only one parameter, returns the item that specifies the position at which the beginning of the current array ends.
    3. If there is a negative number in the argument, the array length is used to determine the corresponding position.

Returns an empty string if the end position is less than the start position

L Splice (): Inserts an item into the middle of the array

Always returns an array that contains the items that were deleted from the original array.

    1. Delete: You can delete any number of items. You only need to specify two parameters: the first item to delete and the number of items to delete.
    2. Insert: You can insert any number of items to the specified location. Just provide 3 parameters: Start position, 0 (number of items to delete) and items to insert
    3. Replace: You can insert any number of items to the specified location and delete any number of items at the same time. Just provide 3 parameters: Start position, number of items to delete, and any number of items to insert

5.2.7 Location Method

L indexOf (): Look backwards from the beginning of the array

L lastIndexOf (): Search forward from the end of the array

Common denominator: Parameters: The subparagraphs (optional) to find the index that represents the starting position of the lookup

Returns the position of the found item in the array, and returns 1 if it is not found

The equality operator (= = =) is used when comparing each item in the first parameter and array

5.2.8 Iterative Methods

L Each method receives two parameters: the function to run on each item and (optionally) the scope object that runs the function-affects the this value.

The functions passed in these methods receive three parameters: the value of the array item, the position of the item in the array, the array object itself

L every (): Runs the given function for each item in the array, and returns True if the function returns true for each item

L filter (): An array of items that run the given function for each item in the array, returning the function that returns true

L ForEach (): Runs the given function for each item in the array, this method does not return a value

L map (): Each item in an array runs the given function, returning an arrays of the results of each function call

L some (): Runs the given function for each item in the array, and returns True if any one of the functions returns True

5.2.9 Merge Method

L iterate over all the items in an algebraic group and then build a final return value.

"Reduce (): From the beginning of the first item of the array; Reduceright (): From the last item in the array, begin the traversal"

L receive two parameters: function called on each item and (optional) as the initial value of the merge base

L The function called on each item receives 4 parameters: the previous value, the current value, the index of the item, and the array object

The return value of this function is automatically passed to the next item as the first argument. The first iteration occurs on the second item of the array, so the first parameter is the first item of the array, and the second is the second item of the array "

JavaScript Advanced Programming (3rd Edition) Note--chapter5: reference type

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.