OOP Programming for JavaScript 1

Source: Internet
Author: User

The first thing to say is that JavaScript can actually be programmed for OOP, and then there are many ways to implement OOP programming in JavaScript, which I've written that I've tested and that is feasible.

Version1

//Parent ClassfunctionPerson (name) { This. Name =name} Person.prototype.say=Saymyname;functionSaymyname () {alert ( This. Name)}//sub-classfunctionEmployee (name) {Person.call ( This, name)}//calling the parent class constructorEmployee.prototype =NewPerson ();//Inherit parent classEmployee.prototype.cardID =function() {alert ("1000")}//define your own methods for subclasses//instantiation ofp =NewEmployee ("Lishujun");p. Say ();p. CardID ();

Version2

//Parent ClassfunctionPerson (name) { This. Name =name} Person.prototype.say=Saymyname;functionSaymyname () {alert ( This. Name)}//sub-classfunctionEmployee (name) {Person.call ( This, name)}//calling the parent class constructorEmployee.prototype =NewPerson ();//Inherit parent classEmployee.prototype.cardID =function(){ This. Say (); Alert ("1000")}//define the subclass's own method, call the parent class method//instantiation ofp =NewEmployee ("Lishujun");p. Say ();p. CardID ();

Version3

//Parent ClassfunctionPerson (name) { This. Name =name} Person.prototype.say=Saymyname;functionSaymyname () {alert ( This. Name)}//sub-classfunctionEmployee (name) {Person.call ( This, name)}//calling the parent class constructorEmployee.prototype =NewPerson ();//Inherit parent classEmployee.prototype.cardID =function() {alert ("1000")}//define your own methods for subclassesEmployee.prototype.say =function() {alert ("Employee say")}//overrides the parent class method, that is: polymorphic //instantiation ofp =NewEmployee ("Lishujun");p. Say ();p. CardID ();

OOP Programming for JavaScript 1

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.