JS Basics-Object-oriented programming

Source: Internet
Author: User

1. Create objects

1) Factory mode

No problem solving object recognition

function Createperson (name,age,job) {  varnew  Object ();   = name;   = Age ;   = job;   function () {    alert (this. Name);  };   return o;} var person1 = Createperson (' Nicholas ', ' software engineer '); var person2 = Createperson (' Greg ', ' Doctor ');

2) Constructor mode

To create an instance of person, using the new operator, go through the steps:

1) Create a new object

2) assigns the scope of the constructor to the new object (so this points to the new object)

3) Execute the code in the constructor (add properties for this new object)

4) return new object

Problem: Each method is recreated on each instance

3) Prototype mode

Each function we create has a prototype property, which is a pointer to an object

The purpose of this object is to include properties and methods that can be shared by all instances of a particular type

Determine if an object is a prototype of another object

Method:

Person.prototype.isPrototypeOf (person1); // ES5 Object.getprototypeof (person1) = = Person.prototype

Whenever a property of an object is read:

1) search starts from the object instance itself

2) search for the prototype object pointed to by the pointer

How to determine whether a property exists in an instance or in a prototype object

Use: hasOwnProperty ()

Prototypes and in operators:

Two ways to use in: use separately and use in for-in loops

Use for: In loop, returns all enumerable properties that can be accessed through an object, including properties that exist in the instance, and instances that exist in the prototype

Object.keys()Used to get all the enumerable property values of the object itself, but not the properties in the prototype, and then returns an array of property names. Note it with the for: In the same way, properties are not guaranteed to be output in the original order of the objects.

Object.getOwnPropertyNames()method returns an array of property names (including non-enumerable properties) of all of the object's own properties, but does not get properties on the prototype chain.

The dynamic nature of the prototype:

Overriding the prototype object cuts the connection between the existing prototype and any previously existing object instances, and they still refer to the original prototype

Problem with prototype object:

All properties in the prototype are shared by many instances, and the problem is prominent for values that contain reference types

4) Combining the constructor pattern with the prototype mode

Constructor patterns are used to define instance properties, and prototype patterns are used to define methods and shared properties

5) Dynamic Prototyping mode

6) Parasitic structural function mode

In addition to using the new operator and using the wrapper function called the constructor, this pattern is exactly the same as the factory pattern

7) Secure structural function mode

2. Inheritance

1) prototype chain

The relationships between constructors, instances, and prototypes:

Each constructor has a prototype object, and the prototype object contains a pointer to the constructor, and the instance contains an internal pointer to the prototype object

Make a prototype object equal to an instance of another type

Results:

The prototype object contains a pointer to another prototype, and another prototype contains a pointer to another constructor, and if another prototype is another type of instance, the layers are progressive, forming the chain of instances and prototypes

Don't forget the default prototype: All reference types inherit object by default, and the default prototype contains an internal pointer to the Object.prototype

Determine the relationship between the prototype and the instance:

Method One: instanceof operator: The test instance and the constructor that appeared in the prototype chain

instanceof // true

Method Two: isPrototypeOf (): Prototype in the prototype chain

Person.prototype.isPrototypeOf (Person1);

The code that adds a method to the prototype must be placed after the statement that replaced the prototype

2) Borrowing constructors

Calling a super-type constructor inside a subtype constructor

Use the Apply and call methods to execute constructors on (future) newly created objects

Pass-through parameters: a great advantage of borrowing constructors is that you can pass parameters to a superclass constructor in a sub-type constructor

Problem: Function reuse does not work

3) Combination Inheritance

Inheritance of prototype properties and methods using prototype chains, implementation of inheritance of instance properties by borrowing constructors

functionsupertype (name) { This. Name =name;  This. colors = ["Red", "Blue", "green"];} SuperType.prototype.sayName=function() {alert ( This. name);}functionSubtype (name,age) {//inherited attributes, and arguments are passedSupertype.call ( This, name);  This. Age =Age ;}//Inheritance MethodSubtype.prototype =Newsupertype (); Subtype.prototype.constructor=subtype; Subtype.prototype.sayAge=function() {alert ( This. age);};varInstance1 =NewSubtype (' Nic ', 29); Instance1.colors.push (' Black '); alert (instance1.colors);//"Red,blue,green,black"Instance1.sayname ();//"Nic"Instance1.sayage ();// invarInstance2 =NewSubtype (' Greg ', 27); alert (instance2.colors);//"Red,blue,green"instance2.sayname (); Instance2.sayage ()

4) prototype-Type inheritance

Inheritance can be implemented without having to pre-define constructors, essentially by performing a shallow copy of a given object, while the copied copy can be further modified.

Prototypes allow you to create new objects based on existing objects without having to create custom types

function Object (o) {  function  F () {}  = o;   return New F ();}

Essentially object () performs a shallow copy of the object passed in

ES5 used Object.create (person)

var person = {  name:' Nic ',  friends:[' sh ', ' Co ', ' VA '}  var anotherperson = object.create (person)

5) Parasitic inheritance

Similar to prototype inheritance, an object is created based on an object or some information, and then the object is enhanced and finally returned. To solve the inefficient problem of multiple invocation of the superclass constructor by the composite inheritance mode, you can use this pattern with the combination inheritance

6) Parasitic combined inheritance

Combining the advantages of parasitic inheritance and combinatorial inheritance is the most efficient way to implement type-based inheritance

JS Basics-Object-oriented programming

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.