VueJs 源码解读之细碎点

在分析 VueJs 2.0 的源码中会碰到一些不知道的知识点,记录下来。

JSON.stringify

JSON.stringify(val, null, 2)

JSON.stringify(value[, replacer [, space]])
//第二个参数如果该参数是一个函数,则在序列化过程中,被序列化的值的每个属性都会经过该函数的转换和处理
//如果该参数是一个数组,则只有包含在这个数组中的属性名才会被序列化到最终的 JSON 字符串中
//如果该参数为null或者未提供,则对象所有的属性都会被序列化

//第三个参数,指定缩进用的空白字符串,用于美化输出(pretty-print)
//如果参数是个数字,它代表有多少的空格;上限为10。该值若小于1,则意味着没有空格;
//如果该参数为字符串(字符串的前十个字母),该字符串将被作为空格;如果该参数没有提供(或者为null)将没有空格。


var obj = {
	name: '11',
	age: 12,
	func: function() {
		console.log(1)
	}
}

//输出
"{
  "name": "11",
  "age": 12
}"

MDN 文档

 

Vue 中存在很多这种写法

/**
 * Check whether the object has the property.
 */
var hasOwnProperty = Object.prototype.hasOwnProperty;
function hasOwn (obj, key) {
  return hasOwnProperty.call(obj, key)
}

典型的单例模式

/**
 * Ensure a function is called only once.
 */
function once (fn) {
  var called = false;
  return function () {
    if (!called) {
      called = true;
      fn.apply(this, arguments);
    }
  }
}