1.0
function text(){
console.log(a);
var a = "1";
console.log(a)
}
結果是 undefined// 1
所謂變量提升,意思就是
函數任意地方聲明變量都可以,就相當於是在頂部聲明了(只是聲明了而已,並不是賦值)
所以上面的例子就類似於
function text(){
var a;
console.log(a);
a = "1";
console.log(a)
}
2.0 提高js 性能的時候比如 for 循環里面 假設用到了某個變量的長度,,盡量提前聲明,有關想能問題,假設是遍歷的dom,那不就很消耗性能了么。
3.0 for-in 循環應該用來比阿尼非數組對象,for-in 循環也被稱為枚舉
此外也可循環數組,因為數組也是對象,但是不建議使用。
4.0 遍歷對象屬性時遇到原型鏈的屬性時,使用hasOwnProperty()方法是很重要的
demo:
let man = {
one:1,
two:2,
head:3,
}
// 將一個方法添加到所有對象上
if(typeof Object.prototype.clone === undefined){
Object.prototype.clone = function (){}
}
循環
for(let i in man){
if(man.hasOwnProperty(i)){
console.log(i,':',main[i]);
}
}
結果 one:1
two:2
head:3,
不用hasOwnProperty()
for(let i in man){
console.log(i,':',main[i]);
}
結果 one:1
two:2
head:3,
clone:function()