|
1 |
| -import './patch-array' |
2 | 1 | import Dep from './dep'
|
| 2 | +import { arrayMethods } from './array' |
3 | 3 | import {
|
4 | 4 | def,
|
5 | 5 | isArray,
|
6 | 6 | isObject,
|
7 | 7 | isPlainObject,
|
| 8 | + hasProto, |
8 | 9 | hasOwn
|
9 | 10 | } from '../util/index'
|
10 | 11 |
|
| 12 | +const arrayKeys = Object.getOwnPropertyNames(arrayMethods) |
| 13 | + |
11 | 14 | /**
|
12 | 15 | * By default, when a reactive property is set, the new value is
|
13 | 16 | * also converted to become reactive. However in certain cases, e.g.
|
@@ -40,6 +43,10 @@ export function Observer (value) {
|
40 | 43 | this.dep = new Dep()
|
41 | 44 | def(value, '__ob__', this)
|
42 | 45 | if (isArray(value)) {
|
| 46 | + var augment = hasProto |
| 47 | + ? protoAugment |
| 48 | + : copyAugment |
| 49 | + augment(value, arrayMethods, arrayKeys) |
43 | 50 | this.observeArray(value)
|
44 | 51 | } else {
|
45 | 52 | this.walk(value)
|
@@ -111,6 +118,37 @@ Observer.prototype.removeVm = function (vm) {
|
111 | 118 | this.vms.$remove(vm)
|
112 | 119 | }
|
113 | 120 |
|
| 121 | +// helpers |
| 122 | + |
| 123 | +/** |
| 124 | + * Augment an target Object or Array by intercepting |
| 125 | + * the prototype chain using __proto__ |
| 126 | + * |
| 127 | + * @param {Object|Array} target |
| 128 | + * @param {Object} src |
| 129 | + */ |
| 130 | + |
| 131 | +function protoAugment (target, src) { |
| 132 | + /* eslint-disable no-proto */ |
| 133 | + target.__proto__ = src |
| 134 | + /* eslint-enable no-proto */ |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * Augment an target Object or Array by defining |
| 139 | + * hidden properties. |
| 140 | + * |
| 141 | + * @param {Object|Array} target |
| 142 | + * @param {Object} proto |
| 143 | + */ |
| 144 | + |
| 145 | +function copyAugment (target, src, keys) { |
| 146 | + for (var i = 0, l = keys.length; i < l; i++) { |
| 147 | + var key = keys[i] |
| 148 | + def(target, key, src[key]) |
| 149 | + } |
| 150 | +} |
| 151 | + |
114 | 152 | /**
|
115 | 153 | * Attempt to create an observer instance for a value,
|
116 | 154 | * returns the new observer if successfully observed,
|
|
0 commit comments