/*
* not type checking this file because flow doesn't play well with
* dynamically accessing methods on Array prototype
*/ import { def } from '../util/index' const arrayProto = Array.prototype // Object.create 如果传入的是数组, 那么这个数组会被封装成一个对象,这个对象作为目标对象的__proto__属性缓存起来
// arrayMethods变成一个新对象, 通过 arrayMethods.__proto__.concat可以访问到原来的数组方法
// 这样就为 Object.definedProperty创造了条件
export const arrayMethods = Object.create(arrayProto) /**
* Intercept mutating methods and emit events
这几个操作都会改变数组本身, 像slice不会改变数组本身, 这里没列出
*/
;[
'push',
'pop',
'shift',
'unshift',
'splice',
'sort',
'reverse'
]
.forEach(function (method) {
// cache original method 获得数组的原型方法, 并缓存
const original = arrayProto[method] // arrayMethods 已经是一个对象, 带有__proto__属性, 下有各种原生数组方法
def(arrayMethods, method, function mutator () {
// avoid leaking arguments:
// http://jsperf.com/closure-with-arguments
let i = arguments.length //作者看上去不喜欢用 [].slice.call , 具体没明白, 好像是slice获取arguments在有的浏览器会有参数泄露问题.
const args = new Array(i)
while (i--) {
args[i] = arguments[i]
} //先执行原生的方法, 这里的this 就是指向 arrayMethods
const result = original.apply(this, args)
const ob = this.__ob__
let inserted
switch (method) {
case 'push':
inserted = args
break
case 'unshift':
inserted = args
break
case 'splice':
     //因为 siplce (0,2,a,b) 这里是要取得新插入的数组值 a b
inserted = args.slice(2)
break
} //如果有新增的值, 监听一下
if (inserted) ob.observeArray(inserted)
// notify change 通知改变
ob.dep.notify() return result
})
})

vue.js 源代码学习笔记 ----- core array.js的更多相关文章

  1. vue.js 源代码学习笔记 ----- core scedule.js

    /* @flow */ import type Watcher from './watcher' import config from '../config' import { callHook } ...

  2. vue.js 源代码学习笔记 ----- core lifecycle

    /* @flow */ import config from '../config' import Watcher from '../observer/watcher' import { mark, ...

  3. vue.js 源代码学习笔记 ----- instance event

    /* @flow */ import { updateListeners } from '../vdom/helpers/index' import { toArray, tip, hyphenate ...

  4. node.js day01学习笔记:认识node.js

    Node.js(JavaScript,everywhere) 1.Node.js 介绍 1.1. 为什么要学习Node.js 企业需求 + 具有服务端开发经验更好 + front-end + back ...

  5. vue.js 源代码学习笔记 ----- html-parse.js

    /** * Not type-checking this file because it's mostly vendor code. */ /*! * HTML Parser By John Resi ...

  6. vue.js 源代码学习笔记 ----- 工具方法 share

    /* @flow */ /** * Convert a value to a string that is actually rendered. { .. } [ .. ] 2 => '' */ ...

  7. vue.js 源代码学习笔记 ----- 工具方法 option

    /* @flow */ import Vue from '../instance/index' import config from '../config' import { warn } from ...

  8. vue.js 源代码学习笔记 ----- 工具方法 lang

    /* @flow */ // Object.freeze 使得这个对象不能增加属性, 修改属性, 这样就保证了这个对象在任何时候都是空的 export const emptyObject = Obje ...

  9. vue.js 源代码学习笔记 ----- 工具方法 env

    /* @flow */ /* globals MutationObserver */ import { noop } from 'shared/util' // can we use __proto_ ...

随机推荐

  1. Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别

    Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别 一.Web.xml中自动扫描Spring的配置文件(applicationCont ...

  2. spring数据源、数据库连接池

    什么是数据源.数据库连接池? DataSource通常被称为数据源,它包含连接池和连接池管理两个部分,习惯上也经常把DataSource称为连接池. 数据库连接池的基本思想:为数据库连接建立一个“缓冲 ...

  3. Eclipse安装zylin[转]

    本文转载自:https://blog.csdn.net/dns888222/article/details/9263485 Eclipse安装zylin 在网上搜的是安装页为http://www.zy ...

  4. LeetCode——Find Duplicate Subtrees

    Question Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, yo ...

  5. java和groovy的混用

    java在语言的动态性方便不是很灵活,如果你想快速增加或改变一些方法,那么只能通过反射机制,并且参数传递的格式很严格. 相比之下,基于groovy可以快速写出一些自定义方法,并能和java很好结合,类 ...

  6. arm-linux-gcc安装使用教程

    arm-linux-gcc如何下载安装2(转) [转]ubuntu下交叉编译环境构建(arm-linux-gcc-3.4.1.tar.bz2 ) 2009-03-03 10:05 1.下载arm-li ...

  7. init() 方法

    1 /** * servlet init()在web应用程序启动之后,第一个请求到达Servlet之前调用. * 问题1:如果init()中需要处理大量的工作,那么servlet在处理第一个请求需要花 ...

  8. Kettle 数据抽取

    1.创建数据库连接 2.建立转换 3.指定源数据库和目标数据库的字段映射 一定要在「输出」中勾选「指定字段」,然后点按钮「Get All fields」,再「Enter mapping」,在弹出窗口映 ...

  9. spark streaming之 windowDuration、slideDuration、batchDuration​

    spark streaming 不同于sotm,是一种准实时处理系统.storm 中,把批处理看错是时间教程的实时处理.而在spark streaming中,则反过来,把实时处理看作为时间极小的批处理 ...

  10. Warsaw U Contest Petrozavo dsk Summer 2011 Training Camp, Monday, September 5, 2011

    Warsaw U Contest Petrozavo dsk Summer 2011 Training Camp, Monday, September 5, 2011 Problem A.Chocol ...