1.拿一個(gè)節(jié)點(diǎn)里的文本時(shí),不要直接nodevalue,如果該節(jié)點(diǎn)的子節(jié)點(diǎn)是標(biāo)簽,那么< > /會(huì)被轉(zhuǎn)義,這種情況使用innerText
2.對(duì)于類數(shù)組元素是不可以使用array類型的forEach方法的,這時(shí)應(yīng)該用call方法來(lái)調(diào)用forEach,而且在這里要注意,不能這樣: Arrary.forEach.call(params),應(yīng)該[].forEach.call()
昨天的demo,
//items是一個(gè)HTML collections 所以不能直接使用forEach方法,對(duì)于類數(shù)組對(duì)象
//應(yīng)該用call
[].forEach.call(items, function(item,index,array) {
data.push([item.innerText.slice(0,2),Number(item.innerText.slice(7,9))]);
});
我按常規(guī)使用第一種方式時(shí)報(bào)錯(cuò)
3.input標(biāo)簽里的值是string類型,當(dāng)判斷輸入框?yàn)榭罩禃r(shí)可使用(value==="")這樣的語(yǔ)句
4.昨天試了下策略模式,非常的好用 以后再遇到分支判斷多的情況,優(yōu)先考慮策略模式
//策略模式
var strategies = {
"shift":function (value) {
arr.shift(value);
render();
},
"push":function (value) {
arr.push(value);
render();
},
"unshift":function (value) {
arr.unshift(value);
render();
},
"pop":function (value) {
arr.pop(value);
render();
}
};
var handleItem = function (model) {
var value = document.getElementById('dataInput').value;
console.log(typeof value);
if (value === "") {
return;
} else {
strategies[model](value);
}
}