首页 > Web开发 > 详细

JS查漏补缺1

时间:2019-07-08 10:42:24      阅读:115      评论:0      收藏:0      [点我收藏+]
1.
function fn(){
        var a = b = 1 //该方式创建的变量,b为全局变量
        // var a = 1, b = 1 //该方法创建的变量都为局部变量
}
fn()
console.log(typeof a) // undefined
console.log(typeof b) // number

2.

fn1(); // 1
function fn1(){
    console.log(1)
}

fn2(); //报错
var fn2 = function(){
    console.log(2)
}

3.for … in

function Student(){
        this.a = 1;
        this.b = 2;
}
Student.prototype.c = 3;
var s = new Student()
for (item in s){
        console.log(item); // a b c
}
// for…in的顺序不确定;
// for in 对象属性时受原型链的影响,enumerable为false时不会出现。

4.不通过var直接赋值的变量是全局变量

function fn(){
        b = 1
}
fn()
console.log(b) // 1

5.arguments

!function(a){
        arguments[0] = 100;
        console.log(a);//100
}(1)

!function(a){
        arguments[0] = 100;
        console.log(a);//undefined
}()

!function(a){
     ‘use strict‘
        arguments[0] = 100;
        console.log(a);//1
}(1)//不传参数打印a为undefined

6.数组

var arr = [,,];
console.log(arr);// undefined * 2 ,最后一个逗号后面的被忽略

//数组的长度大小:0~2^23-1

var arr2 = [1,2,3]
delete arr2[0]
console.log(arr2); // [undefined, 2, 3]
console.log(0 in arr2); //false  0为索引

arr2.length -= 1;//删除最后一项

var arr3 = [undefined];
var arr4 = new Array(1);
0 in arr3;//true
0 in arr4;//false
0 in arr;//false

JS查漏补缺1

原文:https://blog.51cto.com/11569511/2417898

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!