对象的创建三种方法

一、使用new关键字调用构造器创建对象

//使用new关键字调用构造器创建对象

function luo(name,age){
    this name    =    name;
    this age    =    age;
}

var s1    =    new luo(true,true);//没有传参数
var s2    =    new luo("haige",18);
document.write(s1.name+'--'+s1.age+"<br/>");
document.write(s2.name+'--'+s2.age);
document.write(s2.name+"--"+s2.age);

二、使用Object直接创建对象

//使用 Object直接创建对象
var    luo            =    new Object();
    luo.name    =    "类名";
    luo.age        =    34;
    luo.info    =    function(){
    document.write("我的名字是:"+this.name+"<br/>");
    document.write("今年"+this.age+"岁<br/>");
    }
    
    luo.info();

三、使用JSON语法创建对象

var    p    =    {
    name:"haiwen",
    gender:"male",
    info:function(){
    document.write("姓名:"+this.name+"<br/>"+"性别:"+this.gender);        
    
    }

}
p.info();

 

posted @ 2017-12-31 23:29  世山海  阅读(197)  评论(0编辑  收藏  举报