js 终于明白变量提升的概念了

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()

原文地址:https://www.cnblogs.com/lieaiwen/p/10069522.html