JavaScript advanced Programming-Reading notes (2)

Source: Internet
Author: User
Tags uppercase letter hasownproperty

The 6th Chapter object-oriented programming

Creating objects

1. The simplest way to create an instance of an object, such as

var person = new Object ();

Person.name = "Greg";

Person.age = 27;

Person.job = "Doctor";

Person.sayname = function () {

alert (this.name);

};

Person. Sayname ();

Cons: Generates a lot of duplicate code

2. Factory mode: Use functions to encapsulate the details of creating objects with specific interfaces, such as

function Createperson (name,age,job) {

var o = new Object ();

O.name = name;

O.age = age;

O.job = job;

O.sayname = function () {

alert (this.name);

};

}

var person1 = Createperson ("Greg", "Doctor");

Person1.  Sayname; "Greg"

Although it solves the problem of creating multiple similar objects, it does not solve the problem of object recognition (that is, how to know the type of an object).

3. Constructor mode

function Person (name,age,job) {

THIS.name = name;

This.age = age;

This.job = job;

This.sayname = function () {

alert (this.name);

};

}

var person1 = new Person ("Greg", "Doctor");

Person1.  Sayname (); "Greg"

Note: As a rule, constructors should always start with an uppercase letter, while non-constructors should start with a lowercase letter.

The main problem with constructors is that each method is recreated on each instance.

4. Prototype Model

Each function we create has a prototype (prototype) attribute, which is an object whose purpose is to contain properties and methods that can be shared by all instances of a particular type.

function Person (name,age,job) {

}

Person. Prototype.name = "Greg";

Person. Prototype.age = 27;

Person. Prototype.job = "Doctor";

Person. Prototype.sayname = function () {

alert (this.name);

};

var person1 = new Person ();

var person2 = new Person ();

Person1.sayname (); "Greg"

Person2.name = "Nicholas";

Alert ("Person1.name"); "Greg"--from the prototype

Alert ("Person2.name"); "Nicholas"-from the instance

When the constructor is called to create a new instance, the inside of the instance contains a pointer (internal property) that points to the constructor's prototype property.

When you add a property to an object instance, this property masks the property of the same name saved in the prototype object; in other words, adding this property will only prevent us from accessing the properties in the prototype, but we will not modify that property. Disregard, you can use the delete operator to delete instance properties.

Delete Person2.name;

Alert ("Person2.name"); "Greg"--from the prototype

Use the hasOwnProperty () method to detect whether a property exists in an instance or exists in a prototype.

Person2.name = "Nicholas";

Alert ("Person2.name"); "Greg"-from the example

Alert (Person1 hasownproperty ("name")); True

There are two ways to use the In operator: use separately and in the for-in loop. When used alone, the in operator returns True when the given property is accessible through the object, whether the attribute exists in an instance or in a prototype.

Alert ("name" in Person1); True

Problem with prototype objects: For attributes that contain reference type values, the shared nature of the prototype will cause some problems.

5. Combining the constructor pattern with the prototype pattern (the most common way to create a custom type)

function Person (name,age,job) {

THIS.name = name;

This.age = age;

This.job = job;

This.friend = ["Shelby", "Court"];

};

Person. Prototype = {

Constructor:person,

Sayname:function () {

alert (this.name);

}

}

var person1 = new Person ("Nicholas", "Engineer");

var person2 = new Person ("Greg", "Doctor");

Person1.friend.push ("Van");

alert (person1.friend); "Shelby, Court, Van."

alert (person2.friend); "Shelby, Court"

Alert (Person1.friend = = = Person2.friend); False

Alert (Person1. Sayname = = = Person2.   Sayname); True

is one of the most widely used and most recognized methods of creating custom types in ECMAScript.

6. Parasitic structure type function mode (used when none of the above modes are applicable)

function Person (name,age,job) {

var o = new Object ();

O.name = name;

O.age = age;

O.job = job;

O.sayname = function () {

alert (this.name);

};

return o;

}

var person = new Person ("Greg", "Doctor");

Person1.  Sayname (); "Greg"

There is no relationship between the returned object and the stereotype property of the constructor or constructor. Therefore, it is recommended that you do not use this mode when other modes are available.

7. Secure constructor Mode

function Person (name,age,job) {

To create an object to return

var o = new Object ();

Private variables and functions can be defined here

Add method

O.sayname = function () {

alert (name);

};

Return object

return o;

}

Best suited for use in some secure environments where this and new are prohibited, or when data is being altered by other applications.

Inherited

Because the function does not have a signature, interface inheritance cannot be implemented in ECMAScript. ECMAScript only supports implementation of inheritance, and its implementation relies primarily on the prototype chain for implementation.

The basic idea of a prototype chain is to have a reference type inherit the properties and methods of another reference type using the prototype.

The most used inheritance pattern is composite inheritance, which uses the prototype chain to inherit shared properties and methods, while inheriting instance properties by borrowing constructors.

7th Chapter anonymous function

Anonymous functions are functions that do not have a name. Any function expression is technically an anonymous function.

Closures are functions that have access to variables of another function scope. A common way to create closures is to create another function inside one function. Because the closure contains the scope of its functions, it consumes more memory than other functions, and it is recommended to consider using closures only when absolutely necessary.

The This object is bound at run time based on the execution environment of the function: in the global function, this equals window, and when the function is called as a method of an object, this is equal to that object. However, the execution environment of an anonymous function is global, so its this object usually points to window.

Any variable defined in a function can be considered a private variable, because these variables cannot be accessed outside of the function.

We refer to common methods that have access to private variables and private functions as privileged methods. There are two ways to create a privileged method on an object:

The first is the method of defining the privilege in the constructor;

The second is to use static private variables to implement privileged methods.

In addition, you can use the module mode, enhanced module mode to implement the single-instance privileged method.

JavaScript Advanced Programming-Reading notes (2)

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.