方法一
var hero = new Object();
hero.name = "xm";
hero.level = 0;
hero.diamond =0;
hero.moveLeft = function(name) {
console.log("left");
};
//只有在当前hero对象的方法中,this才是当前hero对象
hero.attack = function(name) {
console.log(this.name +"攻击"+name);
};
console.log(hero.name);
console.log(hero.level);
hero.attack("xjs");
方法二
function Student(name,age,sex){ this.name = name; this.name = name; this.name = name; } // 内存开辟空间,存储新创建的对象new Object(); // 会把this设置为当前对象 // 执行函数内部的代码,设置对象的属性和方法 // 返回新创建的对象 var s1 = new Student("zs",18,1);