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>

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

  • 相关阅读:
    五种Sublime text 3同时快速编辑多行内容
    update 更新某个字段自动加1
    oracle 一行记录被锁
    事件
    练习题1
    语法
    开始js
    js简述
    概述
    软连接
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/14165853.html
Copyright © 2011-2022 走看看