vue.js源码学习分享(六)
/* */
/* globals MutationObserver *///全局变化观察者 // can we use __proto__?//我们能用__proto__吗?
var hasProto = '__proto__' in {}; // Browser environment sniffing//浏览器环境嗅探
var inBrowser = typeof window !== 'undefined';//是不是在浏览器中
var UA = inBrowser && window.navigator.userAgent.toLowerCase();
var isIE = UA && /msie|trident/.test(UA);//是不是IE浏览器 内核为trident
var isIE9 = UA && UA.indexOf('msie 9.0') > 0;//是不是IE9
var isEdge = UA && UA.indexOf('edge/') > 0;
var isAndroid = UA && UA.indexOf('android') > 0;
var isIOS = UA && /iphone|ipad|ipod|ios/.test(UA);
var isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge; // this needs to be lazy-evaled because vue may be required before
// vue-server-renderer can set VUE_ENV//这里需要被懒运算,因为vue可能被需要在 vue服务渲染器设置VUE_ENVz之前
var _isServer;
var isServerRendering = function () {
if (_isServer === undefined) {
/* istanbul ignore if */
if (!inBrowser && typeof global !== 'undefined') {
// detect presence of vue-server-renderer and avoid
// Webpack shimming the process
_isServer = global['process'].env.VUE_ENV === 'server';
} else {
_isServer = false;
}
}
return _isServer
}; // detect devtools//探测开发工具
var devtools = inBrowser && window.__VUE_DEVTOOLS_GLOBAL_HOOK__; /* istanbul ignore next */
function isNative (Ctor) {//判断是不是本地的方法
return /native code/.test(Ctor.toString())
} var hasSymbol =
typeof Symbol !== 'undefined' && isNative(Symbol) &&
typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys); /**
* Defer a task to execute it asynchronously.//推迟一个任务异步的执行它
*/
var nextTick = (function () {
var callbacks = [];
var pending = false;
var timerFunc; function nextTickHandler () {
pending = false;
var copies = callbacks.slice(0);
callbacks.length = 0;
for (var i = 0; i < copies.length; i++) {
copies[i]();
}
}
//the nextTick(下一个标记) behavior(行为) leverages(利用) the microtask queue(微任务队列), which can be accessed(被存取)
// via(经由) either native Promise(要么本机的promise).then or MutationObserver(html5新特性之一Mutation Observer(变动观察器)是监视DOM变动的接口。当DOM对象树发生任何变动时,Mutation Observer会得到通知。).
// MutationObserver has wider support, however it is seriously bugged in//变动观察器具有广泛的支持,然而它被严重的干扰在苹果9.3.3以上系统的网页中,当触摸事件被触发时。
// UIWebView in iOS >= 9.3.3 when triggered in touch event handlers. It
// completely stops working after triggering a few times... so, if native//它完全的停止工作在触发后一段时间,所以如果本地的promise是有效的,我们将会使用promise。
// Promise is available, we will use it:
/* istanbul ignore if */
if (typeof Promise !== 'undefined' && isNative(Promise)) {//如果Promise存在
var p = Promise.resolve();
var logError = function (err) { console.error(err); };//打印错误日志
timerFunc = function () {
p.then(nextTickHandler).catch(logError);
// in problematic UIWebViews, Promise.then doesn't completely break, but//在有问题的网页中,Promise.then 不会完全的销毁,但是它会被卡住在一个不可思议的状态下
// it can get stuck in a weird state where callbacks are pushed into the//这个状态是回调函数被推送到微任务队列,但是队列没有被冲洗,直到浏览器需要做其他工作,举例来说操作一个定时器
// microtask queue but the queue isn't being flushed, until the browser
// needs to do some other work, e.g. handle a timer. Therefore we can//因此我们能推动这个微任务队列去冲洗通过增加一个空的定时器
// "force" the microtask queue to be flushed by adding an empty timer.
if (isIOS) { setTimeout(noop); }
};
} else if (typeof MutationObserver !== 'undefined' && (//判断是否支持“变动观察器”
isNative(MutationObserver) ||
// PhantomJS and iOS 7.x
MutationObserver.toString() === '[object MutationObserverConstructor]'
)) {
// use MutationObserver where native Promise is not available,//在本地Promise无效的情况下使用“变动观察器”
// e.g. PhantomJS IE11, iOS7, Android 4.4
var counter = 1;
var observer = new MutationObserver(nextTickHandler);
var textNode = document.createTextNode(String(counter));
observer.observe(textNode, {
characterData: true
});
timerFunc = function () {
counter = (counter + 1) % 2;
textNode.data = String(counter);
};
} else {
// fallback to setTimeout//后退执行定时器
/* istanbul ignore next */
timerFunc = function () {
setTimeout(nextTickHandler, 0);
};
} return function queueNextTick (cb, ctx) {
var _resolve;
callbacks.push(function () {
if (cb) { cb.call(ctx); }
if (_resolve) { _resolve(ctx); }
});
if (!pending) {
pending = true;
timerFunc();
}
if (!cb && typeof Promise !== 'undefined') {
return new Promise(function (resolve) {
_resolve = resolve;
})
}
}
})();
vue.js源码学习分享(六)的更多相关文章
- vue.js源码学习分享(一)
今天看了vue.js源码 发现非常不错,想一边看一遍写博客和大家分享 /** * Convert a value to a string that is actually rendered. *转换 ...
- vue.js源码学习分享(九)
/* */ var arrayKeys = Object.getOwnPropertyNames(arrayMethods);//获取arrayMethods的属性名称 /** * By defaul ...
- vue.js源码学习分享(七)
var _Set; /* istanbul ignore if */ if (typeof Set !== 'undefined' && isNative(Set)) { // use ...
- vue.js源码学习分享(八)
/* */ var uid$1 = 0; /** * A dep is an observable that can have multiple * directives subscribing() ...
- vue.js源码学习分享(五)
//配置项var config = { /** * Option merge strategies (used in core/util/options)//选项合并策略 */ optionMerge ...
- vue.js源码学习分享(四)
/** * Generate a static keys string from compiler modules.//从编译器生成一个静态键字符串模块. */ function genStaticK ...
- vue.js源码学习分享(三)
/** * Mix properties into target object.//把多个属性插入目标的对象 */ function extend (to, _from) { for (var key ...
- vue.js源码学习分享(二)
/** * Check if value is primitive//检查该值是否是个原始值 */ function isPrimitive (value) { return typeof value ...
- Vue.js 源码学习笔记
最近饶有兴致的又把最新版 Vue.js 的源码学习了一下,觉得真心不错,个人觉得 Vue.js 的代码非常之优雅而且精辟,作者本身可能无 (bu) 意 (xie) 提及这些.那么,就让我来吧:) 程序 ...
随机推荐
- Linux - mkdir -p a/b/c
mkdir -p a/b/c -p 会循环创建
- How To Add Swap Space on Ubuntu 16.04
Introduction One of the easiest way of increasing the responsiveness of your server and guarding aga ...
- salt常用模块
salt 常用命令整理 转载:https://www.cnblogs.com/aslongas/p/6964890.html salt 常用命令整理 ***********模块******** ...
- Redis的安装、服务配置
在网上找了很多资料,有些可以正常安装,有些安装会出毛病,仔细想了想,还是自己整理一份吧,仅仅为自己下次再用的时候,能够快速的定位到可以正常用的文章! 我使用的是VMware Workstation P ...
- Python开发环境与开发软件的安装
Python开发的必要因素: 开发软件:PyCharm 社区版 PyCharm安装过程: 首先去官网下载:(链接为: https://www.jetbrains.com/pycharm/downlo ...
- PAT Basic 1079
1079 延迟的回文数(20 分) 给定一个 k+1 位的正整数 N,写成 ak⋯a1a0 的形式,其中对所有 i 有 0≤ai<10 且 ak>0.N 被称 ...
- Linux学习-核心与核心模块
谈完了整个开机的流程,您应该会知道,在整个开机的过程当中,是否能够成功的驱动我们主机的硬 件配备, 是核心 (kernel) 的工作!而核心一般都是压缩文件,因此在使用核心之前,就得要将他解 压缩后, ...
- 利用virt-manager,xmanager, xshell启动界面来管理虚拟机
有时候我们需要搭建一套自己的简单环境来启动一个虚拟机,验证一些问题. 1.首先我利用vmware workstation来创建centos7虚拟机,然后开启虚拟化,如下图所示. 2.其次,启动虚拟机, ...
- Python 综合应用小项目一
数据库报错重连机制 利用异常捕获来获取mysql断开的报错,然后再重连 import MySQLdb as mysql class DB: def __init__(self,host,user,pa ...
- [办公软件][2]screenToGif
https://github.com/NickeManarin/ScreenToGif/wiki/help 下载: .Net Framework 4.6.1 https://www.microso ...