原文连接: juejin.cn/post/704261…
1、 for in 和 for of和forEach简要差异
- for…in循环有几个缺点 ①数组的键名是数字,但是for…in循环是以字符串作为键名“0”、“1”、“2”等等。 ②for…in循环不仅遍历数字键名,还会遍历手动添加的其他键,甚至包括原型链上的键。 ③某些情况下,for…in循环会以任意顺序遍历键名。 for…in循环主要是为遍历对象而设计的,不适用于遍历数组。
-
for…of循环 有着同for…in一样的简洁语法,但是没有for…in那些缺点。 不同于forEach方法,它可以与break、continue和return配合使用。 提供了遍历所有数据结构的统一操作接口(可以遍历类数组对象)
-
forEach forEach()方法不能跳出函数 参考的网站资料: blog.csdn.net/weixin_3886…
结论:遍历方法那么多,这三个基本上可以不需要使用了……
2、 URLSearchParams
3、 isPlainObject
vue和axios对纯粹的对象判断不同。
function isPlainObject (obj) {
return _toString.call(obj) === '[object Object]'
}
function isPlainObject(val) {
if (kindOf(val) !== 'object') {
return false;
}
var prototype = Object.getPrototypeOf(val);
return prototype === null || prototype === Object.prototype;
}
4、stripBOM Byte Order Mark 介绍:blog.csdn.net/qq_38486203…
function stripBOM(content) { if (content.charCodeAt(0) === 0xFEFF) { content = content.slice(1); } return content; }
5 、merge
/**
* Accepts varargs expecting each argument to be an object, then
* immutably merges the properties of each object and returns result.
*
* When multiple objects contain the same key the later object in
* the arguments list will take precedence.
*
* Example:
* var result = merge({foo: 123}, {foo: 456});
* console.log(result.foo); // outputs 456
*/
function merge(/* obj1, obj2, obj3, ... */) {
var result = {};
function assignValue(val, key) {
if (isPlainObject(result[key]) && isPlainObject(val)) {
// 如果原对象中有该值并且该值为对象,那么新的值覆盖原值
result[key] = merge(result[key], val);
} else if (isPlainObject(val)) {
// 如果是纯对象,复制对象到一个空对象,并把该新对象作为key的值。
result[key] = merge({}, val);
} else if (isArray(val)) {
// 复制数组
result[key] = val.slice();
} else {
// 直接赋值。
result[key] = val;
}
}
// 根据自定义的forEach函数,传入要合并的参数对象。
for (var i = 0, l = arguments.length; i < l; i++) {
forEach(arguments[i], assignValue);
}
return result;
}
6、 inherits 原型链的继承问题;
7、 toFlatObject 不知道函数的设计目的。
留2个函数待研究