Home  >  Article  >  Web Front-end  >  Detailed explanation of the three types of method codes for javascript custom objects

Detailed explanation of the three types of method codes for javascript custom objects

伊谢尔伦
伊谢尔伦Original
2017-07-21 13:20:121916browse

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 used after the object is instantiated.

Class methods can be used directly through the class name.

Note: The definition of methods cannot be done 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 instance methods. Currently, there are two ways:

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 externally, and methodName is the method of the object to be defined, which means Directly assign an external method to a method of the object.

Defining object methods in the form of function(arg1,…,argN){} is what developers should master.

Some examples of defining instance methods: 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 instance methods: 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 instance methods: 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 instance methods: Example 4

4) Defining class methods

Class methods need to be defined outside the constructor and can be referenced directly through 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());

The above is the detailed content of Detailed explanation of the three types of method codes for javascript custom objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn