JavaScript Play Inheritance (i)

Source: Internet
Author: User
Tags constructor inheritance

have been learning JavaScript recently, intend to write some articles, calculate to do their own learning experience it, can be regarded as learning notes. No system of knowledge points, too basic do not want to write, mainly to write some of their own feel valuable places.

Write the first article today.

I think everyone has their own views on whether JavaScript is an object-oriented language or a language that supports objects. Those JavaScript-faithful fans must have said that JavaScript is an object-oriented language, and that JavaScript in the book "The Return of the JavaScript King" is based on a prototype object oriented approach. I'd like to talk about my personal opinion. Object-oriented three features, inheritance, polymorphism, encapsulation, JavaScript, although not implemented like java,c#, such as object-oriented language faster, but after all, there is some support. Therefore, it is reasonable to say that JavaScript is an object-oriented language, but from the inheritance of this part to talk about a series of inheritance, but each inheritance law can not realize the power of real object-oriented language, therefore, said he object-oriented has a certain far-fetched. In summary, my understanding of JavaScript is more like to call it a simplified object-oriented, or "pseudo" object oriented (this pseudo word has no derogatory meaning).

Today we'll talk about the first feature of object-oriented: inheritance.

What is inheritance? This I do not want nonsense, there is an animal, there is a person, there is a girl, this is one of the simplest, is also a typical inheritance chain.

In C # and other object-oriented objects, it's easy.

class Animal
{    }
class People:Animal
{    }
class Girl:People
{    }

So in JavaScript, there are no classes, no inherited delivery implementations, what do we do?

Object Camouflage (Structural inheritance method)

What is an object disguise? We may be called structural inheritance easier to understand some. As the name implies, is to play inheritance with the constructor. In fact, it is said that the constructor of the parent class as a common method, put into the constructor of the subclass to execute, so that, when the object is constructed, the subclass of course can construct the parent class Method!

Or use the example above, the code is as follows:

function Animal()
{
 this.Run=function(){alert("I can run");};
}
function People(name)
{
//在这里就是传入了父类的构造方法,然后执行父类的构造方法,这个时候就//可以使用父类中的方法了。
 this.father=Animal;
 this.father();
//记得要删除,否则在子类添加于父类相同名称的方法时,会修改到父类。
delete this.Father;
this.name=name;
this.Say=function(){alert("My name is "+this.name);}
}
function Girl(name,age)
{
 this.father=People;
 this.father(name);
 delete this.father;
 this.age=age;
 this.Introduce=function(){alert("My name is "+this.name+".I am "+this.age);};
}

In this way, an inheritance chain is implemented and tested:

var a=new Animal();
a.Run();
var p=new People("Windking");
p.Run();
p.Say();
var g=new Girl("Xuan",22);
g.Run();
g.Say();
g.Introduce();

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.