zoukankan      html  css  js  c++  java
  • 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>

    二.浏览器打开以上代码渲染结果

  • 相关阅读:
    os
    linux常用命令
    css-基础知识
    awk命令详解
    文献综述
    微信JSAPI支付
    SNMP详解
    SNMP进阶
    SNMP协议入门
    SNMP简单网络管理协议
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/14165853.html
Copyright © 2011-2022 走看看