11月4
1.// 链式编程
function student(id,name){
this.id =id;
this.name =name;
this.scores = [
{subject:"html",score:90 },
{subject:"js",score:90 }
],
this.eat = function(food){
console.log("吃" + food)
return this
},
this.sleep = function(){
console.log("睡")
return this
}
}
var stu = new student(1001,"张三");
stu.eat("").sleep().eat("").sleep().eat("").sleep().eat("").sleep();// 链式编程
运行结果:
[Running] node "c:\Users\詹詹\Desktop\js\tempCodeRunnerFile.js"
吃
睡
吃
睡
吃
睡
吃
睡
[Done] exited with code=0 in 0.116 seconds
2. 可以拓展
//function
function student(id,name){
this.id =id;
this.name =name;
this.scores = [
{subject:"html",score:90 },
{subject:"js",score:90 }
]
}
student.prototype.sex="男";
student.prototype.eat = function (food) { //扩展
console.log("吃" + food)
}
var stu = new student(1000,"张三");
stu.eat("米饭");
console.log(stu.sex)
运行结果:
[Running] node "c:\Users\詹詹\Desktop\js\tempCodeRunnerFile.js"
吃
睡
吃
睡
吃
睡
吃
睡
[Done] exited with code=0 in 0.116 seconds

浙公网安备 33010602011771号