vue.js 源代码学习笔记 ----- core array.js
/*
* 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的更多相关文章
- vue.js 源代码学习笔记 ----- core scedule.js
/* @flow */ import type Watcher from './watcher' import config from '../config' import { callHook } ...
- vue.js 源代码学习笔记 ----- core lifecycle
/* @flow */ import config from '../config' import Watcher from '../observer/watcher' import { mark, ...
- vue.js 源代码学习笔记 ----- instance event
/* @flow */ import { updateListeners } from '../vdom/helpers/index' import { toArray, tip, hyphenate ...
- node.js day01学习笔记:认识node.js
Node.js(JavaScript,everywhere) 1.Node.js 介绍 1.1. 为什么要学习Node.js 企业需求 + 具有服务端开发经验更好 + front-end + back ...
- vue.js 源代码学习笔记 ----- html-parse.js
/** * Not type-checking this file because it's mostly vendor code. */ /*! * HTML Parser By John Resi ...
- vue.js 源代码学习笔记 ----- 工具方法 share
/* @flow */ /** * Convert a value to a string that is actually rendered. { .. } [ .. ] 2 => '' */ ...
- vue.js 源代码学习笔记 ----- 工具方法 option
/* @flow */ import Vue from '../instance/index' import config from '../config' import { warn } from ...
- vue.js 源代码学习笔记 ----- 工具方法 lang
/* @flow */ // Object.freeze 使得这个对象不能增加属性, 修改属性, 这样就保证了这个对象在任何时候都是空的 export const emptyObject = Obje ...
- vue.js 源代码学习笔记 ----- 工具方法 env
/* @flow */ /* globals MutationObserver */ import { noop } from 'shared/util' // can we use __proto_ ...
随机推荐
- 从HighGUI的一段代码中看OpenCV打开视频的方式
OpenCV的HighGUI提供了视频和摄像头的直接打开.那么它是如何实现的了?这里进行初步分析. ; switch(apiPreference) { default: ...
- 仔细讨论 C/C++ 字节对齐问题⭐⭐
原文:https://www.cnblogs.com/AlexMiller/p/5509609.html 字节对齐的原因 为了提高 CPU 的存储速度,编译器会对 struct 和 union的存储进 ...
- Mysql 默认编码问题
新安装的数据库默认编码是 latin1 +--------------------------+----------------------------+ | Variable_name | Valu ...
- Myeclipse中java项目转换为Web项目
https://blog.csdn.net/u010097777/article/details/51281059 这两天工作安排做一个跳转页面,不过昨天发布自己的Tomact花了不少时间,给的项目添 ...
- [HAOI2015]T2
[题目描述] 有一棵点数为N的树,以点1为根,且树点有边权.然后有M个操作,分为三种: 操作1:把某个节点x的点权增加a. 操作2:把某个节点x为根的子树中所有点的点权都增加a. 操作3:询问某个节点 ...
- 从零开始玩转JMX(四)——Apache Commons Modeler & Dynamic MBean
Apache Commons Modeler 前面的Model MBean的创建方式看上去特别复杂,一个简单功能的类ModelMBeanUtils 写了很多代码,那有木有简单点的方式呢,答案是肯定的, ...
- JAVA基础补漏--List
Arraylist 通过对ArrayList的源码的查看,他的底层实现是对数组进行数据的操作,所以他的数据特点同数组. 查询快,因为他的内存区域为一个整块,可直接根据索引进行查询. 增删慢,因为每次增 ...
- sqlite的Top筛选
select [CollectDateTime] as '时间',[Channel_34] as '通道34',[Channel_54] as '通道54' from [DataTable] wher ...
- VS2012 QT5.2.0 无法解析的外部符号
背景:在新建QT工程时,可能没有选择一些库,虽然在头文件中引用了,但是程序依然无法识别 现象:一般出现"LNK2019"错误. 解决:以网络为例,在VS2012中加入网络库,分为两 ...
- R语言系列:自定义function
在用R语言做各种事物时,用户自定义函数是不可或缺的.这期来讲讲如何自定义R的function.首先要介绍的是function的基本框架: myfunction <- function(arg1, ...