链式调用原理:
链式调用原理就是作用域链;实现需要做的工作;
对象方法的处理 (操作方法)
处理完成返回对象的引用(操作对象)
第2步链式实现的方式:
- <1> this的作用域链,jQuery的实现方式;
<2> 返回对象本身, 同this的区别就是显示返回链式对象;
一:
var person = {
set: function (age){ this.age = 10; //this调用位置决定其词法作用域 return person ; }, get: function (){ var age = this.age; if(age < 6){ return '我还是个宝宝'; }else if(age < 18){ return '我还是个少年'; }else{ //…… } } }
资源搜索网站大全 https://www.renrenfan.com.cn 广州VI设计公司https://www.houdianzi.com
二:
var Person = function() {}; Person.prototype.set = function (age){ this.age = 10; return this; //this调用位置决定其词法作用域 } Person.prototype.get = function (){ var age = this.age; if(age < 6){ return '我还是个宝宝'; }else if(age < 18){ return '我还是个少年'; }else{ //…… } } var person = new Person(); person.set(10).get(); // '我还是个少年'