JavaScript Object-oriented

Source: Internet
Author: User

 Object -oriented and object -oriented programming1, process-oriented: All work is now written and used. 2, Object-oriented: is a programming idea, many functions have been written in advance, in use, only need to pay attention to the use of the function, and do not need the specific implementation of the function of the process. JavaScript objects combine related variables and functions into a single whole, which is called an object, a variable in an object is called a property, and a function in a variable is called a method. Objects in JavaScript are similar to dictionaries. Methods for creating Objects1, Monomer<script type= "Text/javascript" >varTom ={name:' Tom ', Age:18, ShowName:function() {alert (' My name is ' + This. Name); }, Showage:function() {alert (' I am this year ' + This. age+ ' Years '); }}</script>2, Factory mode<script type= "Text/javascript" >functionPerson (name,age,job) {varo =NewObject (); O.name=name; O.age=Age ; O.job=job; O.showname=function() {alert (' My name is ' + This. Name);    }; O.showage=function() {alert (' I am this year ' + This. age+ ' Years ');    }; O.showjob=function() {alert (' My job is ' + This. Job);    }; returno;}varTom = person (' Tom ', 18, ' Programmer '); Tom.showname ();</script>2, Constructors<script type= "Text/javascript" >functionPerson (name,age,job) { This. Name =name;  This. Age =Age ;  This. Job =job;  This. ShowName =function() {alert (' My name is ' + This. Name);        };  This. Showage =function() {alert (' I am this year ' + This. age+ ' Years ');        };  This. Showjob =function() {alert (' My job is ' + This. Job);    }; }    varTom =NewPerson (' Tom ', 18, ' Programmer '); varJack =NewPerson (' Jack ', 19, ' Sales '); Alert (Tom.showjob==jack.showjob);</script>3, prototype mode<script type= "Text/javascript" >functionPerson (name,age,job) { This. Name =name;  This. Age =Age ;  This. Job =job; } Person.prototype.showname=function() {alert (' My name is ' + This. Name);    }; Person.prototype.showage=function() {alert (' I am this year ' + This. age+ ' Years ');    }; Person.prototype.showjob=function() {alert (' My job is ' + This. Job);    }; varTom =NewPerson (' Tom ', 18, ' Programmer '); varJack =NewPerson (' Jack ', 19, ' Sales '); Alert (Tom.showjob==jack.showjob);</script>4, inheritance<script type= "Text/javascript" >functionFclass (name,age) { This. Name =name;  This. Age =Age ; } fclass.prototype.showname=function() {alert ( This. Name); } fclass.prototype.showage=function() {alert ( This. Age); }        functionSclass (name,age,job) {Fclass.call ( This, Name,age);  This. Job =job; } Sclass.prototype=NewFclass (); Sclass.prototype.showjob=function() {alert ( This. Job); }        varTom =NewSclass (' Tom ', 19, ' full stack engineer '));        Tom.showname ();        Tom.showage ();    Tom.showjob (); </script>

JavaScript Object-oriented

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.