1 //VI: Object-oriented Programming2 //What is an object: "A collection of unordered attributes, they can contain basic values, objects, or functions", strictly speaking, is equivalent to a set of values that have no particular order, each object and method has a specific name,3 //each name is mapped to a value. 4 //Each object is created based on a reference type. 5 6 //attribute types (attributes): Data properties and properties that access their properties are attributes to the Jsvascript engine, so they are not accessed directly7 //1. Data Properties8 //[[[Configurabale]]: Indicates whether the Delete property can be deleted to redefine the property, whether the attribute can be modified or whether the property can be modified to access9 //its properties:Ten //[[[Enumerable]]: Indicates the ability to iterate through for-in One //[[writeble]]: Ability to modify the value of a property A //[[value]]: Save the value of the property - - //To modify the properties of the above attributes, the Object.defineproperty () method specified in ECMAScript 5 must be used; This method accepts three parameters the //The ① property contains the name of the object ② property ③ A Descriptor object, and the descriptor attribute must be one or more of the above four - - varperson = {}; -Object.defineproperty (person, "name",{ + //Configurable:false, -Writable:false, +Value: "Zhd" A }); atPerson.name = ' Qi ';//due to the writable mark, at this time is illegal browser will ignore the execution of this sentence, strict mode error - //Delete person.name; As configurable this operation is also illegal -alert (person.name);//Zhd - - //and once you mark configurable = False, you cannot change configurable to False by Object.defineproperty (), - //once it is defined as a non-configurable type, it cannot be changed to a configurable type, at which point the call to Object.defineproperty () modifies the except writable in //, there will be an error. - //This means that you can modify the same property multiple times by calling Object.defineproperty (), but when configurable is set to Flase, there is to //Limit + //when you call the Object.defineproperty () method, the default is False if you do not specify Configurable,writable,enumerable - //(the default is true when creating objects) the * //2. Accessor properties (getter/setter) $ //[[Configurabale]: Indicates whether the Delete property can be deleted to redefine the property, or whether the attribute can be modified or can be modified to an accessor property:Panax Notoginseng //properties defined directly on the object, so the property value defaults to True - //[[[Enumerable]]: Represents the ability to iterate through for-in directly on the object defined properties, so the property value defaults to True the //[[Get]]: Read property value defaults to undefined + //[[Set]: Write property value defaults to undefined A //accessors can also use only Object.defineproperty () to set the varBook = { +_year:2016,//① -Edition:1 $ }; $Object.defineproperty (book, ' Year ',{ -Getfunction() { - return This. _year;//② the }, -Setfunction(newvalue) {Wuyi if(NewValue > 2016){ the This. _year = newvalue;//③ - //Console.log (Newvlaue); Wu This. edition + = newValue-2016; - } About } $ }); -Book.year = 2017; - //book._year = 2016;//browser will directly ignore -alert (book.year);// Aalert (book.edition);//2 means that you can also change the values of other properties while setting a property + the //do not forget ②②③ "_", after forgetting to appear Maximum call stack size exceeded error. - //"_" is used to represent the property that can only be accessed through the object method _year to the Data property year for the accessor property $ //direct access to attributes with "_", the browser will directly ignore the the //provides an accessor type with a Java private variable, but instead accesses the Get/set method to the year property in place of the _year the //Java: the //int GetYear () {return _year;} Person.getyear () ==> person.year; the //void setyear (int value) {this._year = value;} Person.setyear (2) ==> person.year = 2; - //You do not have to specify the Get/set method: The property is read-only when only get, and only if set, the property can only write in //There are also two old methods: theBOOK.__DEFINEGETTER__ ("Year",function () { the return This. _year; About }); theBOOK.__DEFINESETTER__ ("Year",function(newvalue) { the if(NewValue > 2016){ the This. _year =newvalue; + This. edition + = newValue-2016; - } the });Bayi the //define multiple attributes (Get/set can also be defined directly within the function body) the varBook = {}; - object.defineproperties (book,{ - _year:{ thevalue:2016 the }, the edition:{ theValue:1 - }, the year:{ theGetfunction(){ the return This. _year;94 }, theSetfunction(newvalue) { the if(NewValue > 2016){ the This. _year =newvalue;98 This. edition + = newValue-2016; About } - }101 }102 });103 104 the //reads an attribute of an object property returns a value when an object106 //if it is a data attribute, then there is configurable,enumerable,writable and value107 //if the accessor property is Configurable,enumerable,set and get108 //Accessing Data Properties109 varDescriptor = object.getownpropertydescriptor (book, "_year"); thealert (Descriptor.value);// .111alert (descriptor.configurable);//false the //alert (type Descriptor.get);//"Undefined"113 //Access accessor Properties the varDescriptor = object.getownpropertydescriptor (book, ' Year '); thealert (Descriptor.value);//undefined thealert (descriptor.enumerable);//false117AlerttypeofDescriptor.get);//"function"118alert (descriptor.get);//will return code
JavaScript Object-oriented (i)