JavaScript Advanced Programming 第6-7 Chapter

Source: Internet
Author: User
Tags uppercase letter

The sixth chapter object-oriented programming

1. Understanding objects: A collection of unordered attributes whose properties can contain basic values, objects, or functions

1) attribute type:

Data properties:4Properties --[[Configurable]]Indicates whether you can passDeleteDelete the attribute to redefine the property, delete the attribute's attributes, or modify the property to the accessor property, which is the default value oftrue;[[Enumerable]]Indicates whether you can passfor-inthe Loop returns a property with the default value oftrue;[[writable]]indicates whether the value of the property can be modified, the default value of which istrue;[[Value]]contains the data value of this property, reads the value of the property from this location, and writes the value of the property to the new value at this location; To modify the property's default attributes, you must use theECMAScript5of theObject.defineproperty ()method, receives three parameters--the object where the property resides, the name of the property, and a Descriptor object

Accessor properties: Does not contain data values, contains a pair ofGetterand theSetterfunction that is called when the accessor property is read.Getterfunction, which is responsible for returning a valid value, which is called when the accessor property is written.Setterfunction and pass in the new value, this function is responsible for deciding how to handle the data;4Properties --[[Configurable]]Indicates whether you can passDeleteDelete the attribute to redefine the property, delete the attribute's attributes, or modify the property to the accessor property, which is the default value oftrue;[[Enumerable]]Indicates whether you can passfor-inthe Loop returns a property with the default value oftrue;[[Get]]The function that is called when the property is read, the default value of which isundefined;[[Set]]The function that is called when the property is written, the default value of this attribute isundefined;to modify properties by default, you must use theECMAScript5of theObject.defineproperty ()method, receives three parameters--the object where the property resides, the name of the property, and a Descriptor object

2) define multiple properties:ECMAScript5 defines the object.defineproperties () method for defining multiple properties, receiving two parameters-- The properties of the object whose properties you want to add and modify, and the objects whose properties you want to add and modify,ie9+,firefox4+,safari5+, opera12+,Chrome Works

3) Properties of the Read attribute:ECMAScript5 defines the object.getownpropertydescriptor () method for obtaining a descriptor for a given property, receiving two properties- -the object where the property resides and the name of the property whose descriptor is to be read

2. Create objects

1) Factory mode: Use functions to encapsulate the details of creating objects with specific interfaces

2) Constructor mode:

object is not explicitly created; The property and method are assigned directly to the this object; no return statement

4 Steps to call the constructor- create a new object, assign the scope of the constructor to the new object ( This point to the new object), execute the code in the constructor (add to the new object), return the new object

The difference between constructors and other functions: constructors should start with an uppercase letter, the non-constructor should start with a lowercase letter, the constructor is called by the new operator, and each method of the constructor is re-created on each instance

3) Prototype mode

3. Inheritance:ECMAScript only supports implementation of inheritance

Seventh Chapter function Expression

Function declaration: Functionfunctionname (ARG0,ARG1,ARG2) {// function Body }, which is read before executing code

function expression:var functionname=function (arg0,arg1,arg2) {// function Body }, The function created in this case is called an anonymous function (lambda function)

1. Recursion:

Arguments.callee is a pointer to a function that is executing, so you can use it to implement a recursive call to a function

function factorial (num) {

F (Num <= 1) {

RETURN1;

}else{

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

}

}

In strict mode, Arguments.callee cannot be accessed through scripting

var factorial= (function f (num) {

F (Num <= 1) {

RETURN1;

}else{

Return num*f (num-1);

}

});

2. Closure: A function that has access to a variable in another function scope; a common way to create closures is to create another function inside one function

3, impersonation block-level scope: block-level scope (private scope) of the anonymous function:(function () {// This is a block-level scope }) ();

4. Private variables: Include function parameters, local variables and other functions defined inside the function; Privileged methods: public methods that have access to private variables and private functions;

Define the privileged method in the constructor:

function MyObject () {

private variables and private functions

var privatevariable=10;

Function privatefunction () {

return false;

}

Privileged Methods

This.publicmethod=function () {

privatevariable++;

return Privatefunction ();

};

}

Static private variables: By defining private variables or functions and privileged methods for a custom type in a private scope, the primary difference between this pattern and the privileged method defined in the constructor is that private variables and functions are shared by the instance. The basic pattern is as follows:

(function () {

private variables and private functions

var privatevariable=10;

function Privatefunction () {

return false;

}

constructor Function

Myobject=function () {

};

public / Privileged Methods

Myobject.prototype.publicmethod=function () {

privatevariable++;

return Privatefunction ();

};

})();

Module mode: Create private variables and privileged methods for singleton; Singleton: Refers to an object with only one instance, andJavaScript creates a singleton object in the form of an object literal. The basic pattern is as follows:

var singleton=function () {

private variables and private functions

var privatevariable=10;

function Privatefunction () {

return false;

}

Privileged / public methods and properties

return{

Publicproperty:true;

Publicmethod:function () {

privatevariable++;

return Privatefunction ();

}

};

}();

Enhanced Module mode

var singleton=function () {

private variables and private functions

var privatevariable=10;

function Privatefunction () {

return false;

}

Creating Objects

var object=new customtype ();

Add Privilege / public methods and properties

Object.publicproperty=true;

Publicmethod=function () {

privatevariable++;

return Privatefunction ();

};

return This object

return object;

}();

JavaScript Advanced Programming 第6-7 Chapter

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.