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

 

相关文章:

  • 2021-09-10
  • 2021-09-03
  • 2022-12-23
  • 2021-07-12
  • 2022-12-23
猜你喜欢
  • 2018-10-18
  • 2020-07-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
相关资源
相似解决方案