JavaScript Learning notes-reference types

Source: Internet
Author: User
Tags instance method prev

A reference type is a data structure, also known as an object definition, similar to the concept of a class.

The object is an instance of a reference type.

JavaScript reference types are: Object, Array, Date, REGEXP, Function

Declare an instance of a reference type using the New keyword.

An Object

Two methods for creating objects.

New Object ()// Objects Declaration  = {}/ /

Object literal is created and does not call the object constructor

Two arrays

1. You can delete or create a new array item by using the Length property

arr = [4;   increased [1,2,3,undefined]arr.length = 2; // Delete [+]
ARR[6] = 6; [1,2,3,undefined,undefined,6]

2. Detecting arrays

Array.isarray ()

3. Conversion method

Calls the ToString () method of the array, returning a comma-delimited string of strings of each value in the array. In fact, to create this string, the ToString () of each item of the array is called.

4. Stack method

The stack is a LIFO data structure, where data insertions and deletions occur at the top of the stack.

Push (), accept any number of arguments, add them one by one to the end of the array, and return the new array length (into the stack)

Pop (), delete the last element of the array, and return the value of the deleted item (out of the stack)

5. Queue method

The queue method is a FIFO data structure.

Shift (), delete the first item of the array and return the value of the item (queue header out)

Push (), inserting several items from the end of the array (queue trailing in)

Or

Unshift (), inserts several items from the head of the array, and returns the new Length (Queue header Entry)

Pop (), delete the last item in the array (the tail of the queue)

6. Reorder Methods

Reverse () and sort () return the sorted array

7. How to Operate

Concat () is used to connect two or more arrays. Finally, a one-dimensional array is returned. It does not change the value of the original array, it returns the copy

Slice () Creates a new array based on one or more items in the current array. Accepts 1 or 2 parameters. The first is the start position of the returned item, and the second is the end position

Splice () a powerful method. can be deleted, inserted, replaced. Accepts 3 parameters. Parameter 1, start position, parameter 2, number of items to delete, Parameter 3, item to insert. Parameters can also be passed as many as you want, followed by inserts into the array sequentially as items to be inserted

8. Location method

IndexOf (), LastIndexOf () accepts 1 parameters and can accept up to 2. Parameter 1, the value to find, and the index at which to find the start position of the parameter 2. Start the search from subscript 0 of the array. LastIndexOf () starts looking forward from the last position. Returns the position of the found value in the array, not found return-1

9. Iterative Methods

Each method receives two parameters. Parameter 1, the function that runs on each item; parameter 2, the scope object that runs the function

Every () each item in the array runs the given function, and if the function returns true for each item, the method returns True

Each item in the filter () array runs the given function, returning the array of items that the function returns True

ForEach () runs the given function for each item of the array, no return value

Map () runs the given function for each item of the array, returning an array that consists of the results of each function call.

Some () runs the given function for each item of the array, and returns True if the function returns true for either item

10. Merge method

Reduce (), Reduceright () both methods receive two parameters. Parameter 1, the function called on each item of the array, parameter 2, optional, as the initial value of the merge base. The function passed to reduce (), reduceright (), accepts 4 parameters. The previous value, the current value, the index of the item, and the array object. Any value returned by this function is automatically passed to the next item in the array as the first argument.

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

Three Date

New Date () the newly created object automatically obtains the current date and time without passing parameters. Timestamp of the parameter receive milliseconds

Date.parse () receives a string representing the date, returning the corresponding number of milliseconds timestamp. Date strings are formatted differently depending on the browser locale. If the passed-in string cannot be resolved, a Nan is returned

The parameters received by DATE.UTC () are year, based on month 0 for January, date of month, 1-31, hour, 0-23, minute, second, millisecond. These parameters are required only for the year and month.

1. Methods of inheritance

Tolocalstring () returns the date and time in a format that is compatible with the locale of the browser

ToString () typically returns a date and time with time zone information, where time is typically expressed in military time.

ValueOf () does not return a string, but instead returns a millisecond representation of the date. You can therefore use comparison operators to compare date values directly

Four REGEXP

Format:

var expression =/pattern/flags;

1.flags is a sign. Are G, I, M respectively.

G: Represents the global pattern. That is, the pattern will match all the strings, not stop immediately when the first match is found.

I: indicates case insensitive.

M: represents multiline mode, which will continue to look for the next line when it reaches the end of a line of text.

Properties of 2.REGEXP Instances

Global: Whether the G mark is set;

IgnoreCase: Whether the I flag is set;

LastIndex: Indicates the character position at which to start searching for the next occurrence, starting from 0;

Multiline: Whether the M flag is set;

Source: A string representation of a pattern

var pattern =/\[bc\]at/I;alert (pattern1.global);    // falsealert (pattern1.ignorecase);   // true     alert ();   // falsealert ();   // 0alert ();   // "\[bc\]at"

3.RegExp instance method

EXEC () receives 1 parameters. A string that applies a pattern to the drug.

    var text = "Father and mother I love You";     var pattern =/I love you/gi;  var matched = pattern.exec (text);

Five function types

Each function is an instance of the type. Functions are also objects.

Declaring functions:

     function Func_name () {}  //The JavaScript parser is the first to read the declaration of functions when the program executes, making it available (accessible) var before executing any code     function() {}; // The parser executes to the line of code where it is located before it is interpreted.     

So the function name is a variable that holds a pointer to a function object, not bound to a function, and is no different from the other variables that contain the pointer to the object.

1. No overloads

Declare two functions with the same name, and the second will overwrite the first one

2. Functions as values

The function name itself is a variable. The function body can therefore be used as a value. It can be passed as a parameter, or it can be used as a return value for other functions.

function callsomefunction (someFunction, someargument) {      return  someFunction (someargument);  }

3. Function Internal Properties

There are two special objects inside the function: arguments and this

The *arguments object holds the parameters inside all incoming functions.

*arguments.callee. This is a pointer to the function that owns the arguments object

function factorial (num) {    if(num <=1) {       return 1;    } Else {       return Num*arguments.callee (num-1);   recursive call       // equivalent:       /        /// This eliminates the tight coupling between the function name and the internal recursive call Function name     }}

*this refers to the variable object of the environment in which the function is executed.

*caller Object Properties. This property holds a reference to the function that invokes the current object. If the current function is called in the global scope, the value is null

function outer () {     inner ();  } function inner () {     alert (arguments.callee.caller);} Outer (); // returns the source code of the outer () function

4. Function properties and methods

Length---The number of functionobject.length function arguments. function has no arguments, it returns 0

Apply ()---function.apply (scope[, function.arguments| | Array])//The parameter passed can be a arguments object, or it can be a parameter array

Call ()---function.call (scope[, Argument1,argument2, ...) The parameters to be passed must be listed as a blow.

Both of these functions are the scope in which to extend the runtime itself. Parameter 1, the scope in which the two functions run (typically objects), and parameter 2, which is passed to the parameter of the function.

Window.color = ' red '; var o = {' Color ': ' Blue '}; function Saycolor () {     alert (color);       // Red Saycolor.call (this//red//red//blue

The advantage of using these two functions is to reduce the coupling between the object and the method.

Six basic packaging types

to Be continued ...

JavaScript Learning notes-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.