链式调用

2023-10-24 15:05:18 浏览数 (2)

前言

我是歌谣 最好的种树是十年前 其次是现在 今天继续给大家带来的是链式调用的讲解

环境配置

npm init -y

yarn add vite -D

修改page.json配置端口

代码语言:javascript复制
{
  "name": "demo1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "vite --port 3002"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "vite": "^4.4.9"
  }
}

基本案例

代码语言:javascript复制
console.log("geyao")
var sched={
    wakeup:function(){
        console.log('Running')
        return this
    },
    morning:function(){
        console.log('Going shopping')
        return this
    },
    noon:function(){
        console.log('Having a rest')
        return this
    },
    afternoon:function(){
        console.log('studying')
        return this
    },
    evening:function(){
        console.log('Walking')
        return this
    },
    night:function(){
        console.log('Sleeping')
        return this
    },
}
sched.wakeup().morning().noon().afternoon().evening().night()

运行结果

案例2

代码语言:javascript复制
var myLang={
    NO1:"HTML",
    NO2:"CSS",
    NO3:"TS",
    myStudyingLang:function(num){
        console.log(this['NO' num])
    }
}
myLang.myStudyingLang(2)

运行结

0 人点赞