function def(obj, key, val, enumerable) {
console.log(enumerable, !!enumerable)
Object.defineProperty(obj, key, {
value: val,
enumerable: false,
writable: true,
configurable: true
})
}
const methods = [
'push',
]
var aryPro = Array.prototype
var arrayMethods = Object.create(Array.prototype)
methods.forEach(method => {
arrayMethods[method] = function (...args) {
const result = aryPro[method].apply(this, args);
let inserted;
let ob = this.__ob__;
switch (method) {
case 'push':
case 'unshift':
inserted = args;
break;
case 'splice':
inserted = args.slice(2)
default:
break;
}
if (inserted) ob.observerArray(inserted);
return result;
}
})
const arrayKeys = Object.getOwnPropertyNames(arrayMethods)
console.log(arrayKeys)
function protoAugment(target, src) {
target.__proto__ = src
}
function copyAugment(target, src, keys) {
for (let i = 0, l = keys.length; i < l; i++) {
const key = keys[i]
def(target, key, src[key])
}
}
function runObserve(val) {
this.value = val
let value = val
def(value, '__ob__', this)
if (Array.isArray(val)) {
if ('__proto__' in {}) {
protoAugment(val, arrayMethods)
} else {
debugger
copyAugment(value, arrayMethods, arrayKeys)
}
observeArray(value)
} else {
}
}
function observeArray(arys) {
for (let i = 0, l = arys.length; i < l; i++) {
observe(arys[i])
}
}
const hasOwnProperty = Object.prototype.hasOwnProperty
function hasOwn(obj, key) {
return hasOwnProperty.call(obj, key)
}
function isObject(obj) {
return obj !== null && typeof obj === 'object'
}
function observe(value, asRootData) {
debugger
if (!isObject(value) || value) {
return
}
let ob
if (hasOwn(value, '__ob__') && value.__ob__) {
ob = value.__ob__
}
if (asRootData && ob) {
ob.vmCount++
}
return ob
}
var test = [1, 2, 3, 4]
new runObserve(test)