The JavaScript language itself can also be object-oriented programming, as the following is a summary of JavaScript object-oriented programming in recent days. Creation of objects There are two ways to create JavaScript objects 1. Use object initializers: objname = { Prop1:value_1, Prop2:value_2, ... } This method creates the instance object directly without having to declare it. 2. Use constructors: such as: Fuction Engineer (PARA1,PARA2) { This.para1 = PARA1; This.para2 = Para2; ... } My bill = new Engineer ("Bill", "24");
The method needs to create the instance with new (). Add a new attribute for an object type: such as: Bill.prototype.sex = "man"; In this way, all objects of the engineer type have attribute sex, whose value is "man", And the following statement: Bill.sex = "Man"; You just add a property to the Bill object itself. Define a method for an object: function Draw () { ... } Fuction Engineer (PARA1,PARA2) { This.para1 = PARA1; This.para2 = Para2; This.draw = Draw; ... } My bill = new Engineer ("Bill", "24"); Bill.draw (); You can also use the following definition method: objname = { Prop1:value_1, Prop2:value_2, Draw:function (num) { ... } ... } Use Objname.draw () when quoting; To delete an attribute of an object: Creates a new, MyObj, with two properties, A and B. myobj = new Object; myobj.a=5; myobj.b=12; Removes the A property, leaving MyObj and only the "B" property. Delete myobj.a;//deletes the A property of the MyObj instance object One way to delete an object: Delete objname.draw;//deletes the draw function of the Objnmae instance object |