13. Object-oriented and inheritance

Source: Internet
Author: User
Tags hasownproperty

Object-oriented and prototype

Learning Essentials:
1. Learning conditions
2. Create an Object
3. Prototypes
4. Inheritance

ECMAScript has two development modes: 1. Functional (procedural) 2. Object-oriented (OOP). The object-oriented language has a sign that
Class, you can create as many objects as you like with the same properties and methods. But Ecmasxript has no concept of class, so it's
Objects are also different from objects in a class-based language.

I. Conditions of study

In the first lesson of the JavaScript video course, it has been stated that the JavaScript course requires a lot of foundation. Here we discuss in detail:

1.xhtml Basics: JavaScript needs to be used in every aspect.
2. and code base: for example, the project in the xhtml,asp,php course has the JS deduction code process.
3. Object-oriented basis: JS object-oriented is unorthodox and bizarre, must have Orthodox object-oriented foundation.
4. The above three foundations must be based on the master of the project, but the basic knowledge of learning is not strong enough, it is necessary to master the above Foundation in the project.

The above basis can be recommended tutorials: XHTML, ASP, PHP can also choose the best Java tutorials on the market, Java tutorials are object-oriented.

Second, create the object
Create an object, and then create a new property and method for the object,
var box = new Object ();//Create an Object
Box.name = ' Lee ';//Create a Name property and assign a value
Box.age = 23;//Create an Age property and assign a value
Box.run = function () {//Create a run () method and return a value
Return this.name + This.age + ' in motion ... ';//this represents the object under the current scope, which is the object instantiated by the new object
};
This is to be placed under a scope, such as Box.run () {}, which is a method under the box scope, which can be used to represent the box itself
Alert (Box.run ());//Output properties and method values

An object is created above and a property and method is created, and this in the NUM () method represents the box object itself. This is JavaScript.
The most basic method of creating an object, but one drawback is that you want to create a similar object that produces a lot of code.

var box2 = box;//Get the box reference
Box2.name = ' journey ';//Change the Name property directly

Alert (Box2.run ());//Use Box.run () to find that name also changed because they point to the same object

var box2 = new Object ();
Box2.name = ' journey ';
Box2.age = 200;
Box2.run = function () {
Return this.name + this.age + ' running ... ';
};
Alert (Box2.run ());//In order to avoid mixing with box, so as to maintain independence


To solve the problem of several similar object declarations, we can use a method called Factory mode, which is to solve the instantiation object
Generate a lot of repetitive problems.

/* Factory mode */
function CreateObject (name, age) {//functions that are instantiated in a centralized format
var obj = new Object ();
Obj.name = name;
Obj.age = age;
Obj.run = function () {
Return obj.name + obj.age + ' running ... ';
};
Return obj;//object reference to be returned
}
var box1 = CreateObject (' Lee ', 100);//First Instance
var box2 = CreateObject (' Journey ', 200);//Second Instance
Alert (Box1.run ());
Alert (Box2.run ());//Remain Independent

Factory mode solves the problem of repeating instantiation, but there is one more problem, which is to identify the problem, because it is impossible to figure out exactly which instance of the object it is.

Alert (typeof box1);//object
Alert (box1 instanceof Object);//true

A constructor (construct method) can be used in ECMAScript to create a specific object, similar to an object.

/* Constructor function */
function Box (name, age) {//Create an Object
THIS.name = name;//Add a property
This.age = age;
This.run = function () {
Return this.name + this.age + ' running ... ';
};
}

var box1 = new Box (' Lee ', 100);
var box2 = new Box (' Journey ', $);//new box ()
Alert (Box1.run ());
Alert (Box2.run ());
Alert (box1 instanceof Object);//clear identification he belongs to box.


The method of using constructors solves the problem of repeated instantiation and solves the problem of object recognition, but the problem is that there is no new object (),
Why can I instantiate box (), where does this come from?

The methods used for constructors, and those that use Factory mode, differ in the following ways:
1. The constructor method does not show the creation object (new object), but the background auto var obj = new Object ();
2. Assign properties and methods directly to the This object, this represents the background of obj;
3. There is no return statement, no object reference is returned, it is returned automatically by the background.

Constructor methods have some specifications:
1. The function name and instantiation construct name are the same and uppercase, (PS: non-mandatory, but it helps to distinguish between constructors and ordinary functions);
2. Create an object from a constructor, you must use the new operator.

Now that you can create an object from a constructor, where does this object come from and where does new object execute? The execution process is as follows:
1. When the constructor is used and the new constructor (), the new Object () is executed in the background;
2. The scope of the constructor is given to the new object (that is, the object created by new object), and this in the body of the function is the delegate new object ()
Out of the object.
3. Execute the code inside the constructor.
4. Returns the new object (returned directly from the background).

For the use of this, this is actually a reference to the current scope object. If this is the Window object represented in the global scope, if
In the constructor body, the object that represents the declaration of the current constructor.

var box = 2;
alert (this.box);//Global, representing Windows


The only difference between constructors and normal functions is that they are called in different ways. Simply, the constructor is also a function, and you must use the new operator to
Called, otherwise it is a normal function.

var box = new Box (' Lee ', 100);
alert (box.run);//Construct mode call

Box (' Lee ', 200);//Normal mode call, invalid

var o = new Object ();
Box.call (O, ' journey ', 200);//Object impersonating call
Alert (Box.run ());

The question of methods (or functions) inside a constructor is explored, first to see if the two instantiated properties or methods are equal.
function Box (name, age) {//Create an Object
THIS.name = name;//Adds an attribute in the constructor body that we call an instance property
This.age = age;
This.run = function () {//instance method
Return this.name + this.age + ' running ... ';
};
}
var box1 = new Box (' Lee ', 100);//transitive consistent, instantiated with address 1
var box2 = new Box (' Lee ', 100);//Ibid., address 2 after instantiation

Alert (Box1.name = = Box2.name);
Alert (Box1.run = = Box2.run);//false, because they compare the reference address
Alert (box1.run () = = Box2.run ());//The values of methods in the body of the constructor are equal

The methods (or functions) in the constructor can be replaced with the new function () method, which results in the same effect and proves that they are ultimately judged
is the reference address, uniqueness.
function Box (name, age) {
THIS.name = name;
This.age = age;
This.run = new Function ("return this.name + this.age + ' run ... ');//description is reference type
}

We can guarantee the consistency of the reference address by using the method of binding the same function outside the constructor, but this approach is not necessary, but
Deepen your learning and understanding.

function Box (name, age) {
THIS.name = name;
This.age = age;
This.run = run;//The run on this side cannot be added () because it is to be given as a whole, not a nominal value.
}
function run () {//Call through outside to ensure consistent reference address
Return this.name + this.age + ' running ... ';
}

Although the global function run () is used to solve the problem of ensuring that the reference address is consistent, this approach introduces a new problem in the global
This is the box itself when the object is called. This, when used as a normal function, also represents window.


Third, the prototype

Each function we create has a prototype (prototype) attribute, which is an object whose purpose is to contain a specific type of
Properties and methods that are shared by all instances of the This is logically understood: prototype the prototype of the object created by invoking the constructor.
Object. The benefits of using prototypes allow all object instances to share the properties and methods that it contains. That is, you do not have to define the object in the constructor
Information, you can add this information directly to the prototype.

function Box () {}//declares a constructor that has nothing in the body, and if there is a property called instance, the instance method

Box.prototype.name = ' Lee ';//Add attributes to the prototype, this is called the prototype attribute
Box.prototype.age = 100;
Box.prototype.run = function () {//Add method to the prototype, this is the prototype method
Return this.name + this.age + ' running ... ';
};

Compare the method address in the prototype is consistent;
var box1 = new Box ();
var box2 = new Box ();

alert (box1.name);
Alert (Box1.run ());

If it is an instance method, different instantiation, their method address is not the same, is unique
If it's a prototype method, then their address is shared, and everyone is the same.

Alert (Box.1run = = Box2.run);//true

To learn more about how constructors are declared and how the prototype schema is declared, we can look at the following from the diagram:

constructor function mode
box1--"Box
Namelee
age100
Runfunction

box2--"Box
Namejack
age200
Runfunction


Prototype mode style

box1--"Box
__proto__ ———————————— |
Box Prototype |
box2--"Boxconstructor <--|
__proto__--|name Lee |
|age 100 |
|runfunction |
|_______________________|


In the prototype schema declaration, there are two more properties, both of which are generated automatically when the object is created. __proto__ property is an instance pointing to a prototype object
A pointer to the constructor's prototype property, constructor. With these two properties, you can access the generic in the prototype
Sex and methods.
alert (box1.constructor);//Returns the constructor itself, constructs the property, can get the function of the constructor itself is positioned by the prototype pointer, and then gets the constructor
itself is actually the object instance corresponding to the role of the prototype object

Ps:ie browser in script access __proto__ will not be recognized, Firefox and Google browser and other browsers can be recognized, although can be output, but no
Access to internal information.
alert (box1.__proto__);//This property is a pointer to a prototype prototype object
alert (box1.prototype);//This property is an object that cannot be accessed


Determines whether an object points to a prototype object of the constructor, which can be tested using the isPrototypeOf () method.
Alert (Box.prototype.isPrototypeOf (Box));//Whenever an object is instantiated, it will point to
Another example:
var obj = new Object ();
Alert (Object.prototype.isPrototypeOf (obj));//returns True, as long as the instantiation will point to

function Box () {}//declares a constructor that has nothing in the body, and if there is a property called instance, the instance method

Box.prototype.name = ' Lee ';//Add attributes to the prototype, this is called the prototype attribute
Box.prototype.age = 100;
Box.prototype.run = function () {//Add method to the prototype, this is the prototype method
Return this.name + this.age + ' running ... ';
};

Compare the method address in the prototype is consistent;
var box1 = new Box ();
Box1.name = ' journey ';
alert (box1.name);//Print out the journey, the nearest principle

var box2 = new Box ();


The execution flow of the prototype pattern:
1. First find the properties and methods in the constructor instance, and if so, return immediately.
2. If not in the constructor instance, go to its prototype object, and if so, return.

Although we can access the values saved in the prototype through the object instance, we cannot access the values in the prototype rewrite through the object instance.
var box1 = new Box ();
alert (box1.name);//lee, the value in the prototype
Box1.name = ' journey ';//instance properties, and no override of prototype properties
alert (box1.name);//journey, nearest principle

var box2 = new Box ();//The Instance property is not shared, so box2 cannot access the instance property, it can only return Lee
alert (box2.name);//lee, the value in the prototype, has not been box1 modified

If you want to box1 also can continue to access the value in the prototype, you can remove the properties in the constructor function.

Specific as follows:
function Box () {}//declares a constructor that has nothing in the body, and if there is a property called instance, the instance method

Box.prototype.name = ' Lee ';//Add attributes to the prototype, this is called the prototype attribute
Box.prototype.age = 100;
Box.prototype.run = function () {//Add method to the prototype, this is the prototype method
Return this.name + this.age + ' running ... ';
};
var box1 = new Box ();
Box1.name = ' journey ';
alert (box1.name);
Delete box1.name;//deleting properties from an instance
Delete box.prototype.name;//deleting properties from the prototype
alert (box1.name);//Print out the properties in the prototype

If the judging attribute is in an instance of a constructor, or in a prototype? You can use the hasOwnProperty () function to verify that:

Alert (Box.hasownproperty (' name '));//Returns True in instance, no returns True

function Box () {}

Box.prototype.name = ' Lee ';
Box.prototype.age = 100;
Box.prototype.run = function () {
Return this.name + this.age + ' running ... ';
}
var box1 = new Box ();
Box1.name = ' journey ';
Alert (Box1.hasownproperty (' name '));//return true,hasownproperty is used to determine whether the specified attribute exists in the instance

The In operator returns True when the given property is accessible through the object, regardless of whether the attribute exists in the instance or in the prototype.
Alert (' name ' in box1);//true, exists in the instance or prototype, regardless of whether the instance property or prototype attribute exists, returns True if there is, returns false on both sides


We can detect whether an attribute exists in an instance by using the hasOwnProperty () method, or it can be used to determine whether an attribute exists in an instance or prototype.
The combination of these two methods allows you to determine whether a property exists in the prototype.


function Isproperty (object, property) {//To determine if an attribute exists in the prototype
Return!object.hasownproperty (property) && (object);
}

var box = new box ();
Alert (Isproperty (box, ' name '));//true, if the prototype has


function Box () {}

var box = new box ();
alert (box.prototype);//Use object instance cannot access to prototype
alert (box.__proto__);//Use object instance to access prototype pointer
alert (box.prototype);//Use constructor name (object name) to access prototype

In order for properties and methods to better reflect the effects of encapsulation, and to reduce unnecessary input, prototypes can be created in a literal way:
function person () {};
person.prototype={//uses the literal way to create the prototype object, where {} is the object, and Object,new object is the equivalent of {}
Name: ' Journey ',
AGE:100,
Run:function () {
Return this.name + this.age + ' running ... ';
}
};

function Box () {};
box.prototype={//uses the literal way to create the prototype object, where {} is the object, and Object,new object is the equivalent of {}
Name: ' Journey ',
AGE:200,
Run:function () {
Return this.name + this.age + ' running ... ';
}
};
Using constructors to create prototype objects and to create objects using literals is basically the same as using, but there are some differences in the way that literals are created
Using the constructor property does not point to an instance, but to object, the constructor is created in the opposite way.

var box = new box ();
Alert (box instanceof box);
Alert (box instanceof Object);
Alert (Box constructor = = box);//literal way, return false, otherwise, true
Alert (Box constructor = = Object);//Literal way, return true, otherwise, false

If you want the constructor of the literal way to point to an instance object, you can do this:

Box.prototype = {
constructor:box,//Direct force pointing can be
};

PS: The literal way why does constructor point to object? Because box.prototype={}; This writing is actually creating a new object.
Each time a function is created, the prototype is also returned to create it, and the object automatically acquires the constructor property. So, the new object
Constructor overrides Box's original constructor, so it points to the new object, then the object does not specify a constructor, then
The default is object.

The declaration of the prototype is sequential, so the rewritten prototype will cut off the previous prototype.

function Box () {};

Box.prototype = {//prototype has been rewritten
constructor:box,//force a prototype object to the Box
Name: ' Lee ',
AGE:100,
Fun:function () {
Return this.name + this.age + ' running ... ';
}
}

Overridden a prototype object

box.prototype={
age:200//This will not retain any information about the previous prototype.
Cut off the previous relationship between the original prototype object and the constructor object instance, that is, the prototype object disappears.
}
var box = new box ();
alert (box.name);

Prototype objects can be used not only in the case of custom objects, but also in ECMAScript built-in reference types, and
The reference type itself is also used as a prototype.

var box = [1,3,4,6,7,5];
Alert (Box.sort ());
See if sort is a method in the array prototype object
alert (Array.prototype.sort);//return value, sort is the prototype method of the Array type
alert (String.prototype.substring);//substring is a String-type prototype method

/* Extension of the built-in reference type */
String.prototype.addstring = function () {//Add a method to the string type
Return this + ', was added! ';//this represents a string that is called
};
Alert (' Lee ', addstring ());//Use this method

PS: Although it is particularly convenient to add methods to native built-in reference types, we do not recommend this approach because it can cause
Naming conflicts that are not conducive to code maintenance.

Prototype schema creation object is also its own disadvantage, it omitted the constructor parameter initialization of the process, the disadvantage is that the initialized value is
The same. And the biggest drawback of the prototype is sharing.
All of the properties in the prototype are shared by many instances, and the share is appropriate for the function and can also be used for attributes that contain basic values. But if the property
Contains a reference type, there are certain problems:

function Box () {};
box.prototype= {
Constructor:box,
Name: ' Journey ',
AGE:100,
Family: [' father ', ' mother ', ' sister '],//add an array property
Run:function () {
Return this.name + this.age + this.family;
}
};
var box1 = new Box ();
alert (box1.family);
Box1.family.push (' brother ');//is added in the instance, the prototype is not good to add
alert (box1.family);
var box2 = new Box ();
alert (box2.family);//share the prototype of the reference type after Box1 added

PS: Data sharing causes many developers to abandon the use of prototypes, because each instantiation of the data needs to retain its own features and cannot be shared.

To solve the problem of constructing parameters and sharing, you can combine constructor + prototype mode:
function Box (name,age) {///non-shared use constructors for preserving independence
THIS.name = name;
This.age = age;
this.family = [' father ', ' mother ', ' sister '];
};
Box.prototype = {//shared use prototype mode
constructor:box,//force point to Box
Run:function () {
Return this.name + this.age + this.family;
}
};
var box1 = new Box (' Lee ', 100);
Alert (Box1.run ());
var box2 = new Box (' Journey ', 200);
Alert (Box2.run ());
Box1.family.push (' brother ');
alert (box1.family);
alert (box2.family);//reference type is not used as a prototype so it is not shared

PS: This mixed mode is a good solution to the problem of the sharing of parameters and references. is a good way to create objects.

Prototype mode, regardless of whether you invoke a shared method in the prototype, it initializes the method in the prototype, and when declaring an object, constructs
The function + prototype part makes people feel weird, and it's best to encapsulate the constructors and prototypes together. To solve this problem, we can use
Dynamic prototyping mode.

function Box (name, age) {//encapsulates all information into the function body
THIS.name = name,
This.age = age,
if (typeof this.run! = ' function ') {//determines if this.run exists. Without this judgment, the following prototype will be initialized two times completely unnecessary
Box.prototype.run = function () {
Return this.name + this.age + ' running ... ';
};
}
}
var box1 = new Box (' Lee ', 100);
Alert (Box.run ());
Initialization of the prototype, as long as it is initialized for the first time, it is not necessary to initialize each time the constructor is instantiated.


PS: Using the dynamic prototype mode, it is important to note that you can no longer use the literal way to rewrite the prototype, because it will cut off the connection between the instance and the new prototype.
System.

The above explains the methods of object creation in various ways, and if none of these approaches satisfy the requirements, you can use the first pattern: the parasitic constructor.
function Box (name, age) {//box is the name of the constructor function
var obj = new Object ();
Obj.name = name;
Obj.age = age;
Obj.run = function () {
Return this.name + this.age + ' running ... ';
};
return obj;
}
var box1 = new Box (' Lee ', 100);
Alert (Box1.run ());

var box2 = new Box (' Journey ', 200);
Alert (Box2.run ());

The parasitic constructor, in fact, is the Factory mode + constructor mode. This mode is more generic, but cannot determine object relationships, so you can make
This mode is not recommended when using the previously mentioned pattern.
Under what circumstances is it appropriate to use a parasitic constructor? Suppose you want to create a reference type that has an extra method. Since the previous instructions are not recommended
Direct String.prototype.addstring, can be added by means of parasitic constructs.

function MyString (string) {
var str = new string (string);
str.addstring = function () {
Return this + ', was added! ‘;
}
return str;
}
var box = new MyString (' Lee ');//is much more tedious than adding directly to the reference type
Alert (box.addstring ());


In some safe environments, such as prohibit the use of this and new, here is the constructor does not use this, where the new is in the external instance
The constructor is not used when you use new. This method of creation is called a secure constructor.
function Box (name, age) {
var obj = new Object ();
Obj.run = function () {
Return name + Age + ' running ... ';//Direct printing of parameters can be
};
return obj;
}
var box = box (' Lee ', 100);//Call function directly
Alert (Box.run ());


PS: The safe constructor is similar to parasitic.


Iv. inheritance
Inheritance has 1. prototype chain inheritance 2. Borrowing constructor inheritance (object impersonation Inheritance) 3. Combined inheritance (combined with the first two types) 4. Prototype inheritance
5. Parasitic Inheritance (prototype + Factory mode) 6. Parasitic combination inheritance (temporary relay function + parasitic function)

Inheritance is a relatively central concept in object-oriented. Other Orthodox object-oriented languages implement inheritance in two ways: one is the implementation of an interface.
One is inheritance. ECMAScript only supports inheritance, does not support interface implementations, and implements inheritance in a way that relies on the prototype chain.

function Box () {//box constructs, inherited functions are called supertype (parent class, base class)
this.name = ' Lee ';
}

function Desk () {//desk constructs, inherited functions are called subclasses (subclasses, derived classes)
This.age = 100;
}


Desk.prototype = new Box ();//through the prototype chain inheritance, the superclass instantiation of the object instance, assigned to the sub-type prototype properties can be
New box will give the information in the box construction and the information in the prototype to desk
Desk's prototype, got the information in box's Construction + prototype
var desk = new Desk ();
alert (desk.age);
alert (desk.name);//Get inherited properties

function Table () {//table construct
This.level = ' AAAAA ';
}

Table.prototype= new Desk ();

var table = new Table ();
alert (table.name);//Inherit box and desk

var box = new box ();
alert (box.constructor);//Get prototype construction


If you instantiate table, then there is an age = 100 in the desk instance, and the same attribute is added in the prototype, age = 200, what is the final result?

Desk.prototype.age = 200,//instances and prototypes contain age

PS: The above prototype chain inheritance is also missing a link, that is, object, all constructors are inherited from object, and inherit object is automatically completed
And do not require the programmer to manually inherit them.

What happens to their dependencies after the inherited instance?
Subtype from which it belongs to itself or its super-type
Alert (table instanceof Object);//true
Alert (table instanceof table);//false,desk is the superclass of table
Alert (table instanceof Desk);//true
Alert (table instanceof Box);//true

In JavaScript, the inherited function is called a supertype (parent, base, or other language), and the inherited function is called a subtype (
Subclass, derived class). Inheritance also has previous problems, such as literal rewriting of prototypes that break relationships, use reference-type prototypes, and sub-types
The parameter cannot be passed to the super type.
In order to solve the problem of reference sharing and super type not being able to pass parameters, we adopt a technique called borrowing constructor, or the object impersonating (forged
Object, classic inheritance) to solve both of these problems.
function Box (name,age) {
THIS.name = name;
This.age = age;
this.family = [' Papa ', ' Mom ', ' sister '];//the reference type is not shared in the construct
}
box.prototype.family= ' family ';//The object can only be inherited in the construction, the prototype will not inherit

function Desk (name,age) {
Box.call (this,name,age);//Object impersonating, to the super-type of the parameter, wherein the this represents the desk itself
}

var desk = new Desk (' Journey ', 200);
alert (desk.name);
alert (desk.family);


Although the borrowing of constructors solves the two problems just now, there is no prototype, and reuse is impossible to talk about. So, we need the prototype chain + borrowing constructors
Pattern, this pattern is called combinatorial inheritance.

function Box (age) {
THIS.name = [' Lee ', ' Jack ', ' Hello '];
This.age;
}
The method in the constructor, placed in the construction, each instantiation, will be allocated a memory address, waste, so it is best to put in the prototype, to ensure that the instantiation of only one address
Box.prototype.run = function () {
return this.name + this.age;
};

function Desk (age) {
Box.call (this,age);//Object impersonating
}

Desk.property = new Box ();//prototype chain inheritance

var desk = new Desk (),///Added data, only for desk
Alert (Desk.run ());

There is also an inheritance pattern called prototype inheritance, which uses prototypes to create new objects based on existing objects without having to create
The custom type.

function obj (o) {//pass a literal function, temporary relay function, O indicates an object to be passed into
function f () {//Create a constructor, the F construct is a temporary new object used to store the object passed over
F.prototype = o;//assigns the literal function to the prototype of the constructor, which is the prototype object that assigns the O object instance to the F construct
return new F ();//finally returns the instantiated constructor, which is the object instance that gets the object passed over
}
}

F.prototype = O is actually equivalent to Desk.prototype = new Box ();

var box = {//This is the literal way of declaring, equivalent to var box = new box ();
Name: ' Journey ',
Family: [' elder brother ', ' elder sister ', ' brother ']
};

var box1 = obj (box);//pass, Box1 is equal to new F ()
alert (box1.name);

alert (box1.family);
Box1.family.push (' sister ');
alert (box1.family);

var box2 = obj (box);
alert (box2.family);//The property of the reference type shares the

Parasitic inheritance combines the prototype + factory model to encapsulate the process of creating objects.
function Create (o) {//Encapsulation creation process
var f=obj (o);
F.run = function () {
Return this.arr;//also share references
};
return F;
}

Combined inheritance is the most common inheritance mode of JavaScript, but the combination of inheritance is also a small problem, that is, the super-type will be called during use
Two times: When a subtype is created one time, the other is inside the constructor of the sub-type.

function Box (name) {
THIS.name = name;
This.arr = [' elder brother ', ' elder sister ', ' brother '];
}

Box.prototype.run = function () {
return this.name;
};
function Desk (name,age) {
Box.call (this,name);//Second call box, object impersonating
This.age = age;
}

Desk.prototype = new box ();//Call Box for the first time

The above code is the previous combination of inheritance, then the parasitic combination inheritance, resolved the problem of two calls.

function Create (Box,desk) {
var f = obj (Box.prototype);
F.constructor = desk;
Desk.prototype = f;
}

function Box (name,age) {
THIS.name = name;
Thsi.age = age;
}

Box.prototype.run = function () {
Return this.name + this.age + ' running ... ';
}

function Desk (name, age) {
Box.call (This,name, age);
}

Create (Box, Desk);//This sentence is used instead of desk.prototype = new Box ();

var desk = new Desk (' Lee ', 100);
Alert (Desk.run ());
alert (desk.constructor);

13. Object-oriented and inheritance

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.