Home >Web Front-end >JS Tutorial >Analysis and comparison of js objects and print objects_javascript skills
JS object introduction:
1. Basic concepts
1, Custom object.
According to the object extension mechanism of JS, users can customize JS objects, which is similar to the Java language.
Corresponding to custom objects are JS standard objects, such as Date, Array, Math, etc.
2. Prototype
In JS, this is a way to create object properties and methods. New properties and methods can be added to objects through prototype.
Through prototype we can add new properties and methods to JS standard objects, for example, for String objects, we can add a new method trim().
Unlike strict programming languages such as Java, we can dynamically add new properties to JS objects during runtime.
2. Grammar rules
1, object creation method
1) Object initializer method
Format: objectName = {property1:value1, property2:value2,…, propertyN:valueN}
property is the property of the object
value is the value of the object. The value can be a string, a number or an object. One of them
For example: var user={name:“user1”,age:18};
var user={name:“user1”,job:{salary:3000,title:programmer}
The method of the object can also be initialized in this way, for example:
var user={name:"user1",age:18,getName:function(){
> }
The following will focus on the constructor method, including the definition of attributes and methods, etc., and also explain the constructor method.
2) Constructor method
Write a constructor and create the object through new. The constructor can have construction parameters
For example:2) Private property definition
Private properties can only be defined and used inside the constructor.Syntax format: var propertyName=value;
For example:
4) For the definition of attributes, in addition to the more formal way above, there is also a very special way of definition, the syntax format: obj[index]=value
Example:
3. Define object methods
1) Three types of methods can be defined for objects in JS: private methods, instance methods and class methods, similar to Java:
Private methods can only be used inside the object
Instance methods must be in the object instance
Class methods can be used directly through the class name.
Note: The method cannot be defined through the index method mentioned above.
2) Define private methods
Private methods must be defined within the constructor body and can only be used within the constructor body.
Syntax format: function methodName(arg1,…,argN){ }
For example:
Copy the code
1) In terms of visibility:
Private properties and methods can only be referenced within the object.
Instance properties and methods can be used anywhere, but must be referenced through objects.
Class attributes and methods can be used anywhere, but cannot be referenced through instances of objects (this is different from Java, where static members can be accessed through instances).
2) From the object level:
is similar to Java bean references and can be referenced in depth.
Several ways:
Simple properties: obj.propertyName
Object properties: obj.innerObj.propertyName
Index properties: obj.propertyName[index]
For deeper references, similar to above .
3) In terms of definition:
Attributes defined through index must be referenced through index.
Attributes defined through non-index methods must be referenced through normal methods.
Also note: Object methods cannot be defined through index.
5. Dynamic addition and deletion of attributes and methods
1) For an instantiated object, we can dynamically add and delete its attributes and methods. The syntax is as follows (assuming the object instance is obj):
Dynamically add object properties
obj.newPropertyName=value;
Dynamicly add object methods
obj.newMethodName=method or =function(arg1,…,argN){}
Dynamicly delete object properties
delete obj.propertyName
Dynamic deletion of object method
delete obj.methodName
2) Example:
user.getMotherName=function(){return “mary”;}
alert(user.getMotherName());//Run through
delete user.getMotherName;
alert(user.getMotherName( ));//Error: The object does not support this method
JS object printing: