This article describes almost all the basic concepts of objects, what objects are, how objects are created, how objects are set up and read, how to delete attributes, constructors, object prototypes, parent classes, subclasses, inheritance, and so on.
1. The object
* Objects are composite data types that centralize multiple data values in one cell and run using a name to access those values. Another way to interpret an object is that the object is a unordered collection of attributes, each with its own name and value. Named values stored in an object can be original values such as numbers and strings, or objects.
*/
var o = new Object ();
2. Object Direct Volume Create object
* The direct amount of the object consists of a list of attribute descriptions, which are enclosed in curly braces, where the property descriptions are separated by commas.
* Each property description in the object's direct volume is made up of a property name plus a colon and a property value.
*/
var Zhangsan = {name: "Zhangsan", Age:34, married:true};
Window.alert (typeof Zhangsan); Output Object
3. Read and Set properties
* The properties of the object are read and set by the dot operator. The new properties of the object can be created directly.
*/
Window.alert (Zhangsan.car); Output undefined
Zhangsan.car= "BMW"; Directly assigned value
Window.alert (Zhangsan.car); Output BMW
4. Enumeration of attributes
* The order is not guaranteed to be fixed by enumerating each property through a for loop.
* Note that the name of the attribute is not a property value.
*/
var values = "";
for (Var v in Zhangsan) values + = v+ "\ n"; Enumerate each property
Window.alert (values);
5. Deletion of properties using delete, such as delete Zhangsan.car
*/
6. Constructor function
The * constructor is a JavaScript function with two attributes:
* (1) It is called by the new operator;
* (2) to pass to it is an application of the newly created empty object, the reference as the value of this keyword, and it also has to initialize the newly created object appropriately.
* Remember: The constructor simply initializes the object and does not return the newly created object.
*/
function Rectangle (W, h) {
This.width = W;
This.height = h;
}
var ret1 = new Rectangle (2, 1);
Note how the constructor uses its arguments to initialize the properties of the object referenced by the This keyword