4种方法:字面量、function、object
1、 字面量 json 对象 var student={ id:1000, name:"张三", scores:[ {subject:"html",score:90}, {subject:"js",score:80} ] }2、function function student(id,name){ this.id=id; this.name=name; this.scores=[ {subject:"html",score:90}, {subject:"js",score:80} ] } // 扩展 (prototype) student.prototype.sex="男"; student.prototype.eat=function(foot){ console.log("吃"+foot); } //添加 var stu=new student(10001,"张三"); stu.eat("米饭"); console.log(stu.sex); //结果
3、object var stu2=new Object(); stu2.id=1000; stu2.name="张三"; stu2.scores=[ {subject:"html",score:90}, {subject:"js",score:80} ] 调用:
链式编程:
例:
function student(id,name){ this.id=id; this.name=name; this.eat=function(foot){ console.log("吃"+foot); return this; } this.sleep=function(){ console.log("睡"); return this; } } var stu=new student(1001,"张三"); // 链式编程 stu.eat("").sleep().eat("").sleep(); 显示:吃 睡 吃 睡 return this; 让student中的方法返回出来,这个this=student中的方法;