function makeMap (str, expectsLowerCase) {
// 构建闭包集合map
var map = Object.create(null);
var list = str.split(',');
for (var i = 0; i < list.length; i++) {
map[list[i]] = true;
}
return expectsLowerCase
? function (val) { return map[val.toLowerCase()]; }
: function (val) { return map[val]; }
}
// 利用闭包,每次判断是否是内置标签只需调用isHTMLTag
var isHTMLTag = makeMap('html,body,base,head,link,meta,style,title')
console.log('res', isHTMLTag('body')) // true
3. 二维数组扁平化 % d6 d# m. }2 I) d' z' r: l* k vue中_createElement格式化传入的children的时候用到了simpleNormalizeChildren函数,原来是为了拍平数组,使二维数组扁平化,类似lodash中的flatten方法。( ~0 r# u w% N. ?' b4 N0 f
// 继承方法
function inheritPrototype(Son, Father) {
var prototype = Object.create(Father.prototype)
prototype.constructor = Son
// 把Father.prototype赋值给 Son.prototype
Son.prototype = prototype
}
function Father(name) {
this.name = name
this.arr = [1,2,3]
}
Father.prototype.getName = function() {
console.log(this.name)
}
function Son(name, age) {
Father.call(this, name)
this.age = age
}
inheritPrototype(Son, Father)
Son.prototype.getAge = function() {
console.log(this.age)
}
运行结果测试:( k+ @- c8 X9 n" |. b8 b
var son1 = new Son("AAA", 23) 0 h; w4 F# l7 \/ U2 N7 ?son1.getName() //AAA4 H0 T. l/ X3 x8 I) \
son1.getAge() //23/ P+ H$ O5 c( ~. x* Z0 c
son1.arr.push(4) " z T. V/ H. o. v
console.log(son1.arr) //1,2,3,4# A5 b) }) \8 x u! D! w