访问者模式: 针对于对象结构中的元素, 定义在不改变该对象的前提下访问结构中元素的新方法.
核心思想
在不改变操作对象的同时, 为它添加新的操作方法, 来实现对操作对象的访问.
对象访问器
举例: 像处理数组一样处理对象, 为对象添加push, pop方法.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| var Visitor = (function () { return { splice: function () { var args = Array.prototype.splice.call(arguments, 1) return Array.prototype.splice.apply(arguments[0], args) }, push: function () { var len = arguments[0].length || 0 var args = this.splice(arguments, 1) arguments[0].length = len + arguments.length - 1 return Array.prototype.push.apply(arguments[0], args) }, pop: function () { return Array.prototype.pop.apply(arguments[0]) }, } })()
|
测试
1 2 3 4 5 6
| var a = new Object() console.log(a.length) Visitor.push(a, 1, 2, 3, 4, 5) Visitor.push(a, 6, 7, 8) Visitor.pop(a) Visitor.splice(a, 2)
|
刚创建对象a时, a是没有length属性的, 当调用了push或者pop, splice时数组内部的实现会赋予或修改length属性