对象的创建三种方法

Posted 青年总裁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对象的创建三种方法相关的知识,希望对你有一定的参考价值。

一、使用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();

 

以上是关于对象的创建三种方法的主要内容,如果未能解决你的问题,请参考以下文章