標籤:blog whether persist 寫部落格 span move typeof indexof json
今天看了vue.js源碼 發現非常不錯,想一邊看一遍寫部落格和大家分享
/** * Convert a value to a string that is actually rendered.
*轉換一個值為字串 */function _toString (val) { return val == null? ‘‘: typeof val === ‘object‘? JSON.stringify(val, null, 2): String(val)
//如果該值是null則返回Null 字元串,如果該值為對象,則返回json字串,否則把對象的值轉化為字串
//知識點:JSON.stringify(val, null, 2),String(val)
}
/** * Convert a input value to a number for persistence. * If the conversion fails, return original string.轉化一個輸入值為一個數字,如果轉換失敗,則返回原始的字串
*/function toNumber (val) { var n = parseFloat(val); return isNaN(n) ? val : n//判斷n是不是 不是數字,如果不是數字則返回字串,如果是則返迴轉換好的數字}
/**
* Remove an item from an array//從數組刪除一個元素
*/
function remove (arr, item) {
if (arr.length) {
var index = arr.indexOf(item);//擷取元素的位置
if (index > -1) {//如果元素存在
return arr.splice(index, 1)//從數組中刪除並且返回這個元素
}
}
}
/**
* Check whether the object has the property.//檢查對象中是否有這個屬性
*/
var hasOwnProperty = Object.prototype.hasOwnProperty;//從對象的原型中擷取hasOwnProperty這個方法
function hasOwn (obj, key) {
return hasOwnProperty.call(obj, key)
}
vue.js源碼學習分享(一)