Home Web Front-end JS Tutorial Interpretation of examples of creating objects and inheritance in JavaScript_javascript skills

Interpretation of examples of creating objects and inheritance in JavaScript_javascript skills

May 16, 2016 pm 05:00 PM
Create object inherit

Object creation:

When a function object is created, the function object generated by the Function constructor will run code similar to this:

Copy code The code is as follows:

this.prototype={constructor:this};

Assume function F
F constructs the object using new method When the object's constructor is set to this F.prototype.constructor
If the function modifies the prototype of the function before creating the object, it will affect the constructor attribute of the created object

For example:
Copy code The code is as follows:

function F(){};
F.prototype={constructor: '1111'};
var o=new F();//o.constructor==='1111' true

Principle of inheritance:

Inheritance in JavaScript It is a mechanism that uses the prototype chain. Each function instance shares the data defined in the prototype attribute of the constructor. To make one class inherit another, the parent function instance needs to be assigned to the prototype attribute of the child function. And every time a new instance object is created, the object's private property __proto__ will be automatically connected to the prototype of the constructor.

instanceof is to look up the private prototype attribute chain of the instance object to determine whether it is an instance of the specified object

Specific example:
Copy code The code is as follows:

//instanceof implements
function Myinstanceof(obj,type)
{
var proto=obj.__proto__;
while(proto)
{
if(proto ===type.prototype)break;
proto=proto.__proto__;
}
return proto!=null;
}


function View(){}
function TreeView(){}
TreeView.prototype=new View();//TreeView.prototype.__proto__=TreeView.prototype automatic completion
TreeView.prototype.constructor=TreeView;//Correction constructor
var view=new TreeView();//view.__proto__=TreeView.prototype automatically completed
alert(view instanceof View); //true When view.__proto__.__proto__ is found,
alert(view instanceof TreeView); //true is found. When view.__proto__ is found,
alert(Myinstanceof(view,View)); //true
alert(Myinstanceof(view,TreeView)); //true
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem that activex components cannot create objects How to solve the problem that activex components cannot create objects Jan 24, 2024 pm 02:48 PM

How to solve the problem that activex components cannot create objects

Detailed explanation of C++ function inheritance: How to use 'base class pointer' and 'derived class pointer' in inheritance? Detailed explanation of C++ function inheritance: How to use 'base class pointer' and 'derived class pointer' in inheritance? May 01, 2024 pm 10:27 PM

Detailed explanation of C++ function inheritance: How to use 'base class pointer' and 'derived class pointer' in inheritance?

How do inheritance and polymorphism affect class coupling in C++? How do inheritance and polymorphism affect class coupling in C++? Jun 05, 2024 pm 02:33 PM

How do inheritance and polymorphism affect class coupling in C++?

Solve PHP error: problems encountered when inheriting parent class Solve PHP error: problems encountered when inheriting parent class Aug 17, 2023 pm 01:33 PM

Solve PHP error: problems encountered when inheriting parent class

Detailed explanation of C++ function inheritance: How to debug errors in inheritance? Detailed explanation of C++ function inheritance: How to debug errors in inheritance? May 02, 2024 am 09:54 AM

Detailed explanation of C++ function inheritance: How to debug errors in inheritance?

Multiple inheritance in PHP Multiple inheritance in PHP Aug 23, 2023 pm 05:53 PM

Multiple inheritance in PHP

Calculate interest on fixed deposits (FDs) and fixed deposits (RDs) using inherited Java program Calculate interest on fixed deposits (FDs) and fixed deposits (RDs) using inherited Java program Aug 20, 2023 pm 10:49 PM

Calculate interest on fixed deposits (FDs) and fixed deposits (RDs) using inherited Java program

How to use polymorphism and inheritance in PHP to deal with data types How to use polymorphism and inheritance in PHP to deal with data types Jul 15, 2023 pm 07:41 PM

How to use polymorphism and inheritance in PHP to deal with data types

See all articles