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
posted @ 2021-11-04 17:04  詹小生  阅读(40)  评论(0)    收藏  举报