玩命加载中 . . .

Vue源码与JavaScript知识点回顾


学习Vue的源码,过程中少不了要补习补习javascript的知识,以此博客作为简要记录。

  1. plain javascript object,js简单对象
    简单对象就是通过“{}”或者“new Object”创建的对象。使用typeof运算符的结果为object且不为null。
function isPlainObject(obj) {
	if(typeof obj != 'object' || obj === null)
		return false
}
  1. &&运算符
    特别地,当&&两边都是boolean类型表达式时,整个与运算结果为true当且仅当两边的表达式结果都为true。
    一般地,&&运算符返回从左到右求值时遇到的第一个false操作数的值;当它们都是true时,返回最后一个操作数的值。

    如果一个值可以转换为true,那么它就是真值,否则就是假值。
    可以转化为false的假值包括:
    false
    null
    NaN
    0
    空字符串
    undefined

result = 'foo' && 4;	// return 4, for 'foo' and 4 are both true value
result = '' && undefined	// return '', for '' and undefined are both false value while '' is the former

in运算符

JavaScript中的in运算符,用于检测某一对象中是否存在某一属性
const isExist = key in obj
如果对象obj中具有属性key,isExist为true,否则为false。
如果一个属性是从原型链上继承而来,in运算符也会返回true:
const a = "toString" in {}
上面的栗子中,{}表示空对象实例,它其实继承了Object,toString在proto上,返回true。

Object

Object.create()

根据现有的对象来作为新创建对象的原型proto
我们来看看vue2.x源码中的一个细节:

const arrayProto = Array.prototype
/**将数组原型链prototype上面的所有方法放到一个对象————arrayMethods里面 */
export const arrayMethods = Object.create(arrayProto)

Object.getOwnPropertyNames()

Function.call()

语法格式:
Function.call(thisArg, arg1, arg2, ...)
其中,thisArg是Function在运行时的this,可以使用该函数实现继承。

function Product(name, price) {
    this.name = name
    this.price = price
}
function Food(name, price) {
    console.log('Food调用了.', this)
    Product.call(this, name, price)
    this.category = 'food'
}
console.log(new Food('cheese', 5.9));

上面栗子中,虽然调用了Product.call(this, name, price),但是该函数参数中的this指向的是Food函数对象,而调用该函数之后的Food对象为:

Food {name: 'cheese', price: 5.9, category: 'food'}

With语句


文章作者: 鹿卿
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 鹿卿 !
评论
  目录