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

Detailed answers to JavaScript simulated overloading and rewriting of toString method

亚连
Release: 2018-05-17 10:13:49
Original
2586 people have browsed it

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];
}
}
}
Copy after login
//toString方法的重写
_person.prototype.toString=function(){
return 'name='+this.name+",age="+this.age;
}
Copy after login
//////创建对象
var stu1=new _person("赵四","23");
stu1.toString();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template