2. Dynamically add, modify, and delete object properties and methods
For example: Create an empty object user with Class object () and modify its behavior.
(1) Adding attributes
var user=New// Create an empty object with no properties and methods // Add property name// Add attributeage user.sex= "Male"
If the result is output, a statement such as alert (User.Name) can be displayed.
(2) Add method
For the previous empty object user, add a Method alert ():
user.alert=function() { alert ("My name is:" +this. name); }
Call: User.alert (); can display its name as Jack
(3) Modifying properties and methods
The modification is to replace the old property with the new attribute.
For example:
User.name= "Tom"; User.alert=function() { alert ("Hello," +this// in this method, the Name property hasalready been replaced by Tom}
If you use the popup dialog box to display its contents, the User.alert () value is "Hello,tom".
(4) deleting properties and methods
In fact, deleting a property or method is defining its value as undefined, which is
User.name=Undefined;user.alert=undefined;
3. Creating untyped objects using curly brace syntax
Its syntax is:
{ property1:statement, property2:statement, ..., propertyn:statementn}
Here, you implement the definition of an object by using braces to make multiple properties or methods a group.
Example: Creating an object with curly brace syntax
<script language= "javascript" type= "Text/javascript" >varobj={};//defines an empty object, equivalent to the Var obj=new object (); varUser={name:"Jack,"//define the Name property and assign the initial valuefavoritecolor:["Red", "green", "black"],//defining a color arrayHellofunction(){//Defining MethodsAlert ("Hello" + This. Name); }, Sex:"Male"} user.hello (); //Calling Methods</script>
4.prototype objects
Each function is actually an object, and their corresponding class is function. They have a special identity, and each function object has a sub-object prototype, which means that prototype represents the prototype of the function. and the function is
Class, prototype is a collection of members that represents a class.
Since Protoype is an object, it is also possible to modify its properties and methods dynamically
For example:
function Class1 () { / / null function }class1.protoype.method=function// Add Method alert ("It ' s a text method");} var obj1 =new Class1 (); // Calling Object Methods
JavaScript basics-Object-oriented programming <2>