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
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!