A study record of advanced JavaScript programming (II)

Source: Internet
Author: User
Tags access properties

A study record of advanced JavaScript programming (II)

Referring to the object, from the last summary can be concluded that the basic type has number,boolean,undefined,string,null, and there are many types of reference, and now introduce some reference types I personally feel more confused about the place

(i) Type of object

(1) There are two ways to create an object:

1: Constructor method: var person = new person ();

In this case, although the constructor is also a function, but when we name it, we usually capitalize the first letter of the constructor to keep the difference from other normal functions.

2: Object literal: as follows:

var person = {

Name = ' Sunshine ',

Age = ' 21 '

}

(2) When accessing the properties and methods of an object, there is a notation and square bracket notation, and the only point is that the square bracket notation can use variables to access properties, such as:

Var index = ' age ';

Alert (Person[index);

This is not feasible in point notation, and second, if the object property name contains spaces, you must also use square brackets notation

(ii) Array type

(1) There are two ways of creating an array type

1: Constructor method: eg var color = new Array ();

2: Array literal notation: var color = [' red ', ' purple ', ' black '];

(2) Detection of arrays

Only an array of instanceof operations is detected, and the array is not strictly detected, because once the page contains multiple frames, there are two or more global execution environments, so there will be more than two different versions of the array () constructor, which may cause a series of problems

Therefore, the Array.isarray () method in ES5 can be used to detect

Eg:if (Array.isarray ()) {

}

(3) The one-class function of the array reference type: Push (), pop (), Shift (), unshift (), sort (), reverse (), concat (), slice (), splice (), indexOf (), every (), Some (), map (), filter (), ForEach (), reduce (), reduceright ()

(iii) Date Object

To create a Date object, use the following method:

Var now = new Date ();

When a parameter is not passed, the current date and time are obtained by default, and if you want to create an object based on a specific date and time, you must pass in the number of milliseconds that represents that date, and Es provides two methods, one date.parse () and one DATE.UTC ();

Note that the subscript of the getmonth () function in the Date object is to get the corresponding month by the subscript of the array, for example, to get March, it should be getmonth (2);

(iv) Types of regexp

Creation method: (1) Literal form creation: eg:var patter =/at/g;

(2) Constructor method: Eg:var patter = new RegExp ("/at/g");

(v) Function type

(1) using the function name without parentheses is to access the function pointer, not the calling function

(2) The function in JS is not overloaded

(3) Internal properties of the function: within the function, there are two special objects, arguments and this, where the main purpose of arguments is to save the function arguments, and the arguments object has a callee attribute that points to the function that owns the arguments object , which are used in the following examples:

Function factorial (num) {
if (num<=1) {

Return 1;

}else{

Return Num*arguments.caree (num-1);

}

}

(2) ES5 also specifies a function object caller, which holds references to functions that call the current function in this property,
Eg:function puter () {
    inner () are supported in addition to those not supported in earlier versions of opera;
}
Function inner () {
    alert (Arguments.callee.caller);
}
Outer;
(3) apply () and call ()
use both methods to invoke a function in a specific scope, in effect setting the point of this to the
difference: the Apply () method receives two parameters, one is the scope in which the function is run, and the parameter array
Eg:function sum (num1,num2) {
    return num1+num;
}
Function Callsum (num1,num2) {
    return sum.apply (this,[num1,num2]);
    //or return sum.apply (this,arguments); The difference between the
}
Call () method and apply () is that the parameters are received differently, the first argument is the same as apply (), but the arguments passed to the function must be enumerated individually, so the above should read:
 
  Return Sum.call (THIS,NUM1,NUM2);

  (vi) String type
  (1) This type is constructed by: 1:var str = "Hello world" or var str = new String (' Hello World ');

  (2) String type method: CharAt (), charCodeAt (), concat (), Slice (), substr (), substring (), indexOf (), LastIndexOf (), Trim (), toLowerCase (), and so on


A study record of advanced JavaScript programming (II)

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.