JavaScript Object Basics
First, the noun explanation:
1. Object-based
All objects are programmed with the concept of objects.
2. Object-oriented programming (OOP Object oriented programming)
A. Object
It is anything that people want to study, not only to express specific things, but also to represent abstract rules, plans or events.
An unordered collection of properties, each of which can have a value (original value, object, function)
B. Properties and behavior of an object
Attribute: Describes his state with a data value
Behavior: A method used to change the behavior of an object
C. Class
An abstraction of an object of the same or similar nature is a class. The abstraction of an object is a class, and the materialization (instantiation) of a class is called an object.
Second, create the object
1. Constructor Method:
function Fun1 () {
}
var obj=new fun1 ();
Alert (typeof obj);
1. Constructor method: function Fun1 () { } var obj=new fun1 (); alert (typeof obj)
2.Object method
var obj=new Object ();
2.Object method var obj=new Object (); function Object () { alert (123); }
3.json method (JavaScript Object notation) native format
var obj={};
Alert (typeof obj);
3.json method (JavaScript Object notation) native format var obj={};
Iii. How to add properties and methods
If the value of a property is a function, we call it a method of the object, otherwise called a property.
1. Construction method
A. Declarations are added later
A. Declare add function fun1 () { } var obj=new fun1 () later; Obj.name= "Zhangsan"; Obj.say=function () { var a= "speak"; return A; }
B. When the declaration is added
A. When declaring, add function fun1 () { this.name= "Zhang San"; This.eat=function () { alert ("I can Eat");} } var obj=new fun1 (); Obj.eat ()
2.json method
A. When declaring, add
Var obj={property Name: Attribute value, property Name 2: Property value 2, property name 3: Property value 3, ...};
A. add var obj={name when declaring: "Zhang San", Say:function () { alert ("Speak");}}; alert (obj.name); Obj.say ();
B. Declarations added later
B. add var obj={};obj.name= "Zhang San" after the declaration, obj.sex= "man"; obj.play=function () { alert ("I will play");} alert (obj.sex); Obj.play ();
Iv. accessing the properties and methods of an object:
The reference value. Properties
The reference value. Attribute ();
V. How to destroy objects
JavaScript's own garbage collection mechanism is to release memory (destroy) when the object is not referenced;
Object =null;
Vi. How to delete an object's properties
Delete
JavaScript Learning notes-Object basics