1. Overview
In the Java language, we can define our own classes and create objects based on these classes for use. In Javascript, we can also define our own classes, such as defining User class, Hashtable class, etc.
Currently in Javascript, there are some standard classes, such as Date, Array, RegExp, String, Math, Number, etc., which provide us with many conveniences to programming. But for complex client programs, these are far from enough.
Unlike Java, Java2 provides us with many standard classes, which basically meets our programming needs. However, Javascript provides few standard classes, and many programming needs need to be implemented by ourselves. For example, Javascript does not have a Hashtable table, which is inconvenient in handling key values.
Therefore, I personally think that a complete Javascript object view should be as follows:
2. Basic concepts
1. Customize the object.
According to JS's object extension mechanism, 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, and through prototype, you can add new properties and methods to objects.
With prototype we can add new properties and methods to JS standard objects, for example, for String objects, we can add a new method trim() to it.
Unlike strict programming languages such as Java, we can dynamically add new properties to JS objects during runtime.
Three, grammar rules
1. Object creation method
1) Object initializer method
Format: objectName = {property1:value1, property2:value2,…, propertyN:valueN}
property is an object's property
value is the value of the object, and the value can be one of a string, a number or an object
For example: var user={name:"user1", age:18};
var user={name:"user1",job:{salary:3000, title:programmer}
Methods that can also be initialized in this way, for example:
var user={name:"user1",age:18,getName:function(){ return this.name; } }The following will be explained with the focus of constructor method, including the definition of attributes and methods, and will also be explained with reference to the constructor method.
2) Constructor method
Write a constructor and create an object through new method. The constructor could have carried constructor parameters
For example:
function User(name,age){ this.name=name; this.age=age; this.canFly=false; } var use=new User();2. Define object properties
1) In JS, three types of properties can be defined for objects: private properties, instance properties and class properties. Similar to Java, private properties can only be used inside objects. Instance properties must be referenced through the instance of the object, and class properties can be referenced directly through the class name.
2) Private attribute definition
Private attributes can only be defined and used within the constructor.
Syntax format: var propertyName=value;
For example:
function User(age){ this.age=age; var isChild=age<12; this.isLittleChild=isChild; } var user=new User(15); alert(user.isLittleChild);//The correct way alert(user.isChild);//The error: The object does not support this property or method3) There are also two ways to define instance attributes:
prototype method, syntax format: functionName.prototype.propertyName=value
This method, syntax format: this.propertyName=value, pay attention to the location used in the following examples
The value above can be character creation, number and object.
For example:
function User(){ } User.prototype.name="user1"; User.prototype.age=18; var user=new User(); alert(user.age); ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――�3) Class attribute definition
Syntax format: functionName.propertyName=value
For example:
function User(){ } User.MAX_AGE=200; User.MIN_AGE=0; alert(User.MAX_AGE);Refer to the class properties of JS standard objects:
Number.MAX_VALUE //Maximum numerical value Math.PI //Pi
4) In addition to the more formal methods above, there is also a very special way to define attributes, syntax format: obj[index]=value
example:
function User(name){ this.name=name; this.age=18; this[1]="ok"; this[200]="year"; } var user=new User("user1"); alert(user[1]);In the above example, it is important to note that the age attribute is not obtained through this[1], nor can this[0] be obtained through this[0]. That is, the index defined by the index must be referenced using the index mode, and not defined by the index mode, and must be referenced in a normal way.
3. Define object method
1) There are three types of methods that can be defined for objects in JS: private methods, instance methods and class methods, similar to Java:
Private methods can only be used inside objects
Instance methods must be instantiated before they can be used
Class methods can be used directly through class names
Note: The definition of the method cannot be carried out 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:
function User(name){ this.name=name; function getNameLength(nameStr){ return nameStr.length; } this.nameLength=getNameLength(this.name); }3) Define an instance method, currently two methods can be used:
Prototype method, used outside the constructor, syntax format:
functionName.prototype.methodName=method;
or
functionName.prototype.methodName=function(arg1,…,argN){};
This method, used inside the constructor, syntax format:
this.methodName=method;
or
this.methodName=function(arg1,…,argN){};
In the above syntax description, method is a method that already exists outside. The method of the object to be defined by methodName means that an external method is assigned to a method of the object directly.
Defining the object method in the form of function(arg1,…,argN){} is something that developers should master.
Some examples of defining an instance method: Example 1
function User(name){ this.name=name; this.getName=getUserName; this.setName=setUserName; } function getUserName(){ return this.name; } Function setUserName(name){ this.name=name; }Some examples of defining an instance method: Example 2
function User(name){ this.name=name; this.getName=function(){ return this.name; }; this.setName=function(newName){ this.name=newName; }; }Some examples of defining an instance method: Example 3
function User(name){ this.name=name; } User.prototype.getName=getUserName; User.prototype.setName=setUserName(); function getUserName(){ return this.name; } Function setUserName(name){ this.name=name; }Some examples of defining an instance method: Example 4
function User(name){ this.name=name; } User.prototype.getName=function(){ return this.name; }; User.prototype.setName=function(newName){ this.name=newName; };4) Define class methods
Class methods need to be defined outside the constructor and can be referenced directly by the constructor name.
Syntax format:
functionName.methodName=method;
or
functionName.methodName=function(arg1,…,argN){};
example:
function User(name){ this.name=name; } User.getMaxAge=getUserMaxAge; function getUserMaxAge(){ return 200; }or
User.getMaxAge=function(){return 200;};
alert(User.getMaxAge());
4. References of properties and methods
1) From the perspective of visibility:
Private properties and methods can only be referenced inside the object.
Instance properties and methods can be used anywhere, but must be referenced by objects.
Class properties and methods can be used anywhere, but cannot be referenced by an instance of an object (this is different from Java, where static members can be accessed by an instance).
2) From the object level:
Similar to references to Java beans, deep references can be made.
Several ways:
Simple property: obj.propertyName
Object properties: obj.innerObj.propertyName
Index property: obj.propertyName[index]
For deeper references, similar to those above.
3) From the definition method:
Attributes defined through the index method must be referenced through the index method.
Attributes defined by non-index methods must be referenced in a normal way.
Also note: The method of the object cannot be defined through the index method.
5. Dynamic addition and deletion of properties and methods
1) For an object that has been instantiated, we can dynamically add and delete its properties and methods. The syntax is as follows (assuming that the object instance is obj):
Dynamically increase object properties
obj.newPropertyName=value;
Dynamically increase object method
obj.newMethodName=method or=function(arg1,…,argN){}
Dynamically delete object properties
delete obj.propertyName
Dynamically delete object method
delete obj.methodName
2) Example:
function User(name){ this.name=name; this.age=18; } var user=new User("user1"); user.sister="susan"; alert(user.sister);//Run through delete user.sister; alert(user.sister);//Report error: The object does not support this property user.getMotherName=function(){return "mary";} alert(user.getMotherName());//Run through delete user.getMotherName; alert(user.getMotherName());//Report error: The object does not support this methodFour, summary
1. Custom object mechanism is one of the most attractive mechanisms for JS. This is simply great for C++ and Java programmers!
2. There are two ways to create an object: object initializer and constructor.
3. Object properties and methods, constraints with visibility, and attributes and methods with different visibility are defined differently.
The above article is based on js objects, operation attributes and methods, which are all the contents I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.