创建对象的三种方法
1.通过字面量创建对象
``
var obj={ name:"yj", age:18, height:178, hero:function(){ return "盖世英雄"; } } console.log(obj.hero());
``
2.通过构造函数创建对象
``
function Person(){ this.name="yj", //通过this关键字设置默认成员,没有this关键字,属性是非成员。 this.age=18, this.height=178, this.hero=function(){ return "盖世英雄"; } } var obj=new Person() console.log(obj.hero());
``
3.通过Object方式创建,先通过Object构造器new一个对象,再往里面丰富成员信息
``
var obj=new Object(); obj. name="yj"; obj.age=18; obj.height=178; obj.hero=function(){ return "盖世英雄"; } console.log(obj.hero());
``
浙公网安备 33010602011771号