zoukankan      html  css  js  c++  java
  • JavaScript创建对象的方法

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8">
            <title>JavaScript对象的创建</title>
            <script type="text/javascript">
                /*方式1:使用new关键字调用构造器创建对象*/
                /*function student(name,age){
                    this.name=name;
                    this.age=age;
                    student.gender="male";
                    this.info=function(){
                        document.write("姓名:"+this.name+",年龄:"+this.age+"性别:"+student.gender);
                    }
                }
                var s1=new student("chen",20);
                s1.info();*/
    
                /*-------------------------------------------------------------------------------*/
    
                /*方式2:使用Object直接创建对象*/
                /*var student=new Object();
                student.name="chen";
                student.age=20;
                student.info=function(){
                    document.write("姓名:"+this.name+";年龄:"+this.age);
                }
                student.info();*/
                /*也可以写成下面这种方式*/
                /*var student=new Object();
                student.name="chen";
                student.age=20;
                function abc(){
                    document.write("姓名:"+this.name+";年龄:"+this.age);
                }
                student.info=abc;//这里不加括号,加括号表示调用了函数,但是并不是调用函数
                student.info();*/
    
                /*-------------------------------------------------------------------------------*/
    
                /*方式3:使用JSON语法创建对象*/
    
                /*提示:JSON数据格式比XML数据格式更简洁,数据传输量也更小。
                因此,在需要跨平台跨语言进行数据交换时,有时宁愿选择JSON作为数据交换格式,而不是XML。*/
    
                /*var person={
                    "name":"Bob",
                    "age":30,
                    "son":[
                        {
                            "name":"Teddy",
                            "age":14
                        },
                        {
                            "name":"Chariel",
                            "age":2
                        }
                    ],
                    "info":function(){
                        document.write("父亲名字:"+this.name+",父亲年龄:"+this.age+"<br>");
                        for(var child in this.son){//注意,这里的son不是少了this,否则会说son is not defined.
                            var i=parseInt(child)+1;//经测试发现这里若是没有将child转为整型那么它会默认是string!
                            document.write("第"+i+"个孩子名字:"+this.son[child].name+",孩子年龄:"+this.son[child].age+"<br>");
                        }
                    }
                };
                person.info();*/
    
            </script> 
        </head>
        <body>
        </body>
    </html>
  • 相关阅读:
    存储过程3前台
    最简单Login程序
    存储过程前台2
    程序员 开发工具箱
    存储过程4前台
    存储过程 insert
    公司网络解决方案
    存储过程前台
    linux常用指令
    ReentrantLock源码解析3优先响应中断的lockInterruptibly
  • 原文地址:https://www.cnblogs.com/dorra/p/7309704.html
Copyright © 2011-2022 走看看