<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD"><HTML> <Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /> <title>Untitled Document</title> <Scripttype=text/javascriptCharSet=utf-8> //Customizing the prototype object alters the constructor of the prototype object. //Concept of Prototypes: All properties and methods in the prototype object are shared by objects instantiated by all constructors functionPerson () {} Person.prototype={constructor:person, Name:'Z3', Age: -, Job:'Programmer', friends: ['John Doe','Harry'], Sayname:function() {alert ('My name!')} }; varP1= NewPerson (); varP2= NewPerson (); P1.friends.push ('Zhao Liu');//the. Push method of the arrayalert (p1.friends);//Change thealert (p2.friends); //we generally use a combination of a constructor and a prototype model, which is the most widely used in real-world development. //use a combination of prototypes and constructors (a common way of defining a class development), not just a constructor or a prototype. functionPerson (name, age, friends, job) { This. Name=name; This. Age=Age ; This. Friends=friends; This. Job=job; } Person.prototype={Constructor:person, sayname:function() {alert ( This. Name);//Who calls Sayname,this is who } }; varP1= NewPerson ('Z3' , - , ['Harry','Zhao Liu'] , 'Technical Director'); varP2= NewPerson ('John Doe', - , ['Harry','Zhao Liu' ,'Zhao 7'] ,'boss'); alert (p1.friends); P1.sayname (); alert (p2.friends); P2.sayname (); //Dynamic Prototyping mode: (Let your code wrap together). Dynamic prototype mode: It is to encapsulate the information into functions, which embodies the concept of encapsulation. functionPerson (name, age, friends, job) { This. Name=name; This. Age=Age ; This. Friends=friends; This. Job=job; //Dynamic Prototyping methods: if( typeof This. Sayname!= 'function'){//The first time new person will add this method to the prototype, the second time the new person object is not created. typeof is lowercase function,constructor is uppercase functionPerson.prototype.sayName= function() {alert ( This. Name); } } } /*var o = {name:2, Age: ""} alert (O.sayname);//undefined A Lert (typeof o.sayname);//undefined*/ //Secure constructor: Durable object (Safe object) in a very secure environment //1 No public properties, 2 cannot use this object functionPerson (name, age, job) {//create an object to return, Factory mode, varobj= NewObject (); //You can define a private variable and function Private,js inside is a var local variable, external access, internal functions, similar to Java inside through the private declaration of some variable methods. varname=name; //var sex = ' male '; //var saysex = function () {}; //Add a methodObj.sayname= function() {alert (name); } returnobj; } varP1= NewPerson ('Zhang San'); P1.sayname (); </Script> </Head> <Body> </Body></HTML>
JS17---Create objects: constructor and prototype combination modes, dynamic prototype patterns, secure constructor functions