Javascript创建对象的常用方法

直奔主题,JS创建对象的方法:

   //1.字面量创建对象


    //    var student={name:"张三",school:"洛阳师院"};

    //    document.write("姓名:"+student.name+"
"+"
来自:"+student.school)


    //2.构造函数创建对象及输出对象属性


    //    function Student(name,school){

    //        this.name=name;

    //        this.school=school;

    //        this.outFun=function(){

    //            document.write("姓名:"+this.name+"
"+"
来自:"+this.school)

    //        }

    //    }

    //

    //    c

    //    student1.outFun();


   //3.new运算符创建对象,循环输出


    //    var student=new Object();

    //    var lesson=new Object();

    //    student.name="王五";

    //    student["school"]="河南大学";

    //    lesson["0"]="Java基础";

    //    lesson["1"]="HTML";

    //    lesson["2"]="CSS";

    //    lesson["3"]="JavaScript";

    //    document.write("姓名:"+student.name+"
"+"
来自:"+student.school+"
");

    //    document.write("学习的课程有:"+"
");

    //    for(var i in lesson){

    //        document.write(lesson[i]+"
");

    //    }


   //4.通过prototype向对象中添加属性和方法


         //    function Student(name, school) {

         //        this.name = name;

         //        this.school = school;

         //    }

         //

         //    Student.prototype.lesson="HTML5";

         //    var student1=new Student("赵六","郑州大学");

         //    document.write(student1.lesson);



猜你喜欢

转载自blog.csdn.net/WEIGUO19951107/article/details/78176856