The most commonly used inheritance pattern combinations in JavaScript inherit _js object-oriented

Source: Internet
Author: User
Copy Code code as follows:

<script type= "Text/javascript" >
To create a base class
function person (name, age) {
THIS.name = name;
This.age = age;
}
To add a function to a base class in a prototype way (so you can take this function)
Person.prototype.showName = function () {
alert (this.name);
}
To create a child class
function Student (name, age, score) {
This.score = score;
Person.call (This,name,age);
}
To assign an instance of a parent class to the prototype of a subclass
Student.prototype = new Person ();
To add a function to a subclass by prototyping it (so you can take this function)
Student.prototype.showScore = function () {
alert (This.score);
}

The following are used
var student = new Student ("Zhangsan", 22, 100);
Student.showname ();
Student.showscore ();

var stu = new Student ("Lisi", 25, 200);
Stu.showname ();
Stu.showscore ();
</script>

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.