1, the constructor mode---"Alert results are false
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Document</title> <Script> functionPerson (name,age,job) { This. Name=name; This. Age=Age ; This. Job=job; This. ShowName= function() {alert ( This. Name); } This. Showage= function() {alert ( This. Age); } This. Showjob= function() {alert ( This. Job); } } varTom= NewPerson ('Tom', -,'Programmer'); varJack= NewPerson ('Jack', +,'Sales'); Alert (Tom.showjob==jack.showjob); //The result is false </Script></Head><Body> </Body></HTML>
2. prototype mode---->alert result is true
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Document</title> <Script> 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 ( This. Age); } Person.prototype.showjob= function() {alert ( This. Job); } varTom= NewPerson ('Tom', -,'Programmer'); varJack= NewPerson ('Jack', +,'Sales'); Alert (Tom.showjob==jack.showjob);//result is true </Script></Head><Body> </Body></HTML>
Two types of object-oriented constructs (constructors) in JavaScript and (the difference between prototype schemas)