JavaScript基础知识-toString()
JavaScript基础知识-toString()
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.JavaScript源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>toString</title> <script type="text/javascript"> function Person(name,age,address) { this.name = name; this.age = age; this.address = address; } Person.prototype.toString = function(){ return "<Person name = " + this.name + ", age = " + this.age + ", address = " + this.address + ">"; } Person.prototype.arms = "冲锋枪"; var p1 = new Person("孙悟空",500,"花果山"); var p2 = new Person("如来佛祖",1000,"大雷音寺"); /** * 当我们直接在页面中打印一个对象时,实际上是输出对象的toString()方法的返回值。 */ console.log(p1); console.log(p1.toString()); console.log("result = " + p1); console.log("result = " + p1.toString()); console.log(p2); console.log("result = " + p2); console.log("result = " + p2.toString()); </script> </head> <body> </body> </html>
二.浏览器打开以上代码渲染结果
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/14165853.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。