Object-oriented, basic mode, object literal mode, Factory mode, constructor mode, prototype mode, composite constructor and prototype mode, other modes see ebook: Dynamic prototype mode, parasitic constructor mode (not recommended), secure constructor mode (requires secure environment, does not use new and this)
//Object Oriented/*"Data Properties" * Configurable true to modify properties by deleting properties * enumberable True by for-in return properties * Writable true to modify property values * Value data values * ES5 can be modified by Object.defineproperty (obj, property name, Object {}) * Property name configurable/enumerable/writable/value* set configurable to F After alse, it can no longer be set to True, * call Object.defineproperty () default property name is False**/varperson ={}object.defineproperty (person,"Name", {writable:false,//prevent property values from being modifiedValue: ' Xiaoming '}) Console.log (person.name) person.name= ' xxx '; Console.log (person.name)/*Accessor Properties * Configurable false* enumerable* get function default undefined* Set function default undefined* add get Set function **/varBook ={_year:2014,//An underscore represents a property that can be accessed only through an object method, with direct access toEdition:1}object.defineproperty (book,' Year ', {get:function () { return This. _year}, set:function(val) {if(Val> 2014){ This. _year =Val This. edition + = val-2004}}) Book.year= 2015; Console.log (book.edition); Console.log (book._year);/*define multiple properties with object.defineproperties () plural*//*Object.getownpropertydescriptor () to view data properties and accessor property values * Any object in JS, including DOM objects and BOM objects, has Object.getownpropertydescriptor () method * **/varDesc =object.getownpropertydescriptor (book, ' _year ') Console.log (desc.value); Console.log (desc.configurable);varDESC2 =object.getownpropertydescriptor (book, ' Year ')) Console.log (Desc2.value); //undefinedConsole.log (desc2.configurable);//false/*"1. General mode"*/varobj =NewObject () obj.name= ' name 'Console.log (' Name: ' +obj.name);/*2. Object Literals*/varObj2 ={name:' Mama '}console.log (' Mama: ' +obj2.name);/*"3. Factory mode"*/functionCreteperson (name,age) {varn \NewObject (); O.name=name; O.age=Age ; O.sayname=function() {Console.log (' XX: ' + This. Name); } returno;}varPerson1 = Creteperson (' Kang ', 22) Person1.sayname ()/*"4. Constructor Mode"*/functionPerson (name, age) { This. name=name; This. age=Age ; This. sayname2=function() {Console.log ( This. Name + ' ABCCDE ')//equivalent to this.sayname2 = new function (' ... ') each time the instantiation will be new function (), which is the flaw }}varPer2 =NewPerson (' Xiao ', 33)//Person () as a constructor, with newper2.sayname2 () console.log (Per2instanceofPerson );//Person (' kangddd ', +//person () as normal function,//window.sayname2 ();varPer3 =NewPerson (' AA ', 33)varPer4 =NewPerson (' BB ', 33)//instantiate two objects with the Sayname () method, the ECMAScript function is also an object, each defining a function, is instantiating an objectConsole.log (' ddd333 ' + (Per3 = = per4));//+ priority ratio = = HighConsole.log (' eeefff ' +false);/*"5. Prototype Mode" * Each function has a prototype (prototype) attribute, which is a pointer to an object * Object instance shared prototype defined properties and methods **/functionPersonsix () {}personsix.prototype.name= ' Li 'PersonSix.prototype.sayName3=function() {Console.log ( This. name);}varPer10 =Newpersonsix ();p Er10.sayname3 ()//First Find Per10 there is no SayName3 () method, have then return, no then find prototype whether there is Sayname3 () method/*each creation of a function has prototype*/functionTest () {return1}console.log (test); //input functions function Test () {}Console.log (Test.prototype);//returns an object each creating a function that has prototypeConsole.log (Test.prototype.constructor);//returns the function functions Test () {}functionPerson100 () {}person100.prototype.name= ' 100name 'Person100.prototype.age= ' 100age 'varP100 =NewPerson100 ()varp101 =NewPerson100 () p100.name= ' 110name '//The name in the prototype is overridden by a property with the same name in the instanceConsole.log (P100.name);//' 110name 'Console.log (P101.name);//' 100name 'DeleteP100.name;//You can retrieve the name from the prototype by using deleteConsole.log (P100.name);//' 100name 'Console.log (P100.hasownproperty (' name '));//determine if an instance has an attribute hasOwnPropertyConsole.log (' name 'inchP100);//name exists on instance or prototype will return TrueConsole.log (Object.keys (Person100.prototype));//[' name ', ' age '] get an array of all prototype propertiesConsole.log (Object.getownpropertynames (Person100.prototype));//[' constructor ', ' name ', ' age '] get an array of all prototype propertiesConsole.log (' OK? ' + (P100.constructor = = Person100));//This is true before simplifying/*Simplified prototyping Syntax*/functionCar () {}car.prototype={constructor:car,//manually specify constructor, omit, Car.constructor no longer points to carName: ' Baoma ', Age:22}varCar =NewCar () console.log (car.name); Console.log (Car.prototype.constructor); Console.log (Car.constructor= = Car);//after simplification, this returns false/*the substring () method can be found in the String.prototype*//*the flaw of the prototype pattern is that if the attribute in the object is a reference type, such as an array, an instance modifies the arrays and the other instance gets the modified data .*/functionDog () {}dog.prototype={constructor:dog, friends:[' Kang ', ' Jia ']}varF1 =NewDog ()varF2 =NewDog () F1.friends.push (' Hehe ') Console.log (f1.friends); //[' Kang ', ' Jia ', ' hehe ']Console.log (F2.friends);//[' Kang ', ' Jia ', ' hehe ']/*"combined use of constructors and prototype primitives"*/functionPig (name,age) {//property is written in the constructor This. Name =name; This. age=Age ; This. Friends = [' dd ', ' EE ']}pig.prototype={//The method is written in the prototypeConstructor:person, Sayname:function() {Console.log ( This. Name); }}varPIG1 =NewPig (' Piga ', 11)varPig2 =NewPig (' PIGB ', 12) Pig1.friends.push (' FF ') Console.log (pig1.friends); Console.log (pig2.friends); Console.log (Pig1.friends= = = Pig2.friends);//falseConsole.log (Pig1.sayname = = = Pig2.sayname);//true
View Code
JS Advanced Programming 2