Home  >  Article  >  Web Front-end  >  Detailed answers to JavaScript simulated overloading and rewriting of toString method

Detailed answers to JavaScript simulated overloading and rewriting of toString method

亚连
亚连Original
2018-05-17 10:13:492526browse

Now I will explain in detail the javascript simulation overloading and the rewriting of the toString method. Please see the code below for details.

/***重载模拟***/
function _person(){
var args=arguments;
if(typeof args[0]=="object"&&args[0]){
if(args[0].name){
this.name=args[0].name;
}
if(args[0].age){
this.age=args[0].age;
}
}else{
if(args[0]){
this.name=args[0];
}
if(args[1]){
this.age=args[1];
}
}
}
//toString方法的重写
_person.prototype.toString=function(){
return 'name='+this.name+",age="+this.age;
}
//////创建对象
var stu1=new _person("赵四","23");
stu1.toString();

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Explain in detail the rewriting and overloading techniques of JS rewriting prototype objects

Detailed explanation

Focus on the technique of overriding the alert() method in JavaScript

The above is the detailed content of Detailed answers to JavaScript simulated overloading and rewriting of toString method. 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