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