1104-创建对象

三种方法

第一种:自变量 json 通常用在数据解析

第二种:构造函数 function

第三种:Object  代码重复率太高  灵活,易扩展

function:

function 函数名(属性名){

this.id=id;  函数名的.id=id

this.eat = function(返回值1){ eat方法

console.log("吃"+返回值1) 输出 吃+返回值1

return this  返回函数名

} ,

this.sleep =function(){

console.log("睡") 输出一个睡

return this  为了方便下面操作

}

}

构造函数

var name = new 函数名 (属性名);

name.eat("内容") 这里的内容是最终显示内容 返回值1是给他输出机会   输出结果是吃+内容

name.sleep()  输出结果就是睡

name.eat("nr").sleep().eat("12334")  return this 为了这一步可以简化调用 因为他调用的是name.eat

因此需要返回到他本身函数名身上

 

function student(id,name){
    this.id=id;
    this.name=name;
    this.eat =function(food){
        console.log("吃"+ food)
        console.log(this)
        return this// this 指41行student
    },
    this.sleep =function(){
        console.log("睡")
        return this //调用之后返回stud
    }
}
var stud =new student(1001,"lisi");
stud.eat("22");
stud.sleep();//有stud. 可以调用后面函数
stud.eat("33").sleep().eat("55")//没有return 无法完成 因为没有返回到stud
//链式编程  jquery就是链式编程
posted @ 2021-11-04 19:22  林启  阅读(35)  评论(0)    收藏  举报