一、链式调用:在调用一个方法之后,可以使用方法的返回值来继续进行方法调用
二、一般的函数调用于链式调用的区别:调用完方法后,return this返回当前调用方法的对象
| 【示例】```javascript function Dog() { this.run = function() { alert(‘The dog is running…’) return this; // 返回当前对象Dog } this.eat = function() { alert(‘After running the dog is eatting…’); return this; // 返回当前对象Dog } this.sleep = function() { alert(‘After eatting the dog is running…’); return this; // 返回当前对象Dog } }
// 一般的调用方式 const dog1 = new Dog(); dog1.run(); dog1.eat(); dog1.sleep();
// 链式调用 const dog2 = new Dog(); dog2.run().eat().sleep(); ``` | | —- |