牛客JS前端笔试题目知识点整理(长期更新)


关于js函数的声明方式

js常用定义函数的方式有两种

1 函数声明

function test(){

}

2 函数表达式

let test = function(){

}

其他方式都按照2处理

let test1 = function test2(){

}

typeof test2 // undefined

关于call和apply的第一个参数

若参数为null或undefined,函数this指向全局对象

function test(){
    console.log(this)
}

test.call(null)        //global or window
test.call(undefined)   //global or window

函数不同声明方式的优先级对比

var test = function(){      //let会因为TDZ报错
    console.log("test1")
}

function test(){
    console.log("test2")
}

test()  // test1

        //由于变量提升的存在,函数声明的优先级更高,因此会被函数表达式赋值覆盖

不同类型变量逻辑运算时的类型转换

1 NaN参与的运算永远返回false

console.log(NaN == NaN) //false
console.log(1 == NaN)   //false
console.log("" == NaN)  //false
console.log([] == NaN)  //false
console.log({} == NaN)  //false

2 Bool与其他类型比较时会转换为number

console.log(false == 0)  //true
console.log(false == 1)  //false
console.log(true == 1)   //true
console.log(true == 2)   //false

3 String参与比较时会转换为number

console.log("" == false)  //true
console.log("1" == 1)     //true
console.log("123" == 123) //true

4 引用类型比较时先调用valueOf,再调用toString

证明调用valueOf和toString

Array.prototype.valueOf = function(){
    console.log("valueOf")
    return this
}

Array.prototype.toString = function(){
    console.log("toString")
}

console.log([1, 2, 3] == "")

/*
 *  valueOf
 *  toString  
*/

// 对象调用toString为"[object Object]"

isNaN的类型转化

//如果参数值可以转化为Number,则发生类型转化(“1”, true, [])
console.log(isNaN(1))       //false
console.log(isNaN("1"))     //false
console.log(isNaN(true))    //false
console.log(isNaN("true"))  //true
console.log(isNaN([]))      //false
console.log(isNaN({}))      //true

迭代器对空元素的处理

let arr = [,,,,,]

{
    let i = 0
    arr.forEach((val, index, arr) =>{
        ++i
    })
    console.log(i) //0
    
    //forEach会跳过空元素
}

{
    let i = 0
    arr.map((val, index, arr) =>{
        ++i
    })
    console.log(i)  //0

    //map跳过空元素
}

{
    let i = 0
    arr.reduce((acc, val, index, arr) =>{
        ++i
    }, 0)
    console.log(i)

    //reduce跳过空元素
}

{
    let i = 0
    for(index in arr){
        ++i
    }
    console.log(i)  //0

    //for in 跳过空元素
}

{
    let i = 0
    for(val of arr){
        ++i
    }
    console.log(i)  //5

    //for of 不会跳过空元素,空元素为undefined
}

//空元素区别于显示undefined,若直接指定为undefined,map等迭代器依然会进行处理

关于立即执行函数调用时间

//立即执行函数只有在js引擎“真正解析该处代码时才会调用”,真正解析是指当在函数内时,只有该函数进入调用栈才会被执行

function fun1(){
    (function() {
        console.log("fun1")     //未执行
    }())
}

function fun2(){
    (function() {
        console.log("fun2")     
    }())
}

fun2()      //fun2

let obj = {
    fun:     (function() {console.log("fun")}())        //fun
}

Window对象的常见属性

//document 对document对象的只读引用
console.log(window.document)

//history 包含浏览器窗口中访问过的url
console.log(window.history)     //返回一个类数组对象,包含访问过的url,利用该对象的back,forward,go方法可以访问列表中的url

//location 当前url的信息
console.log(window.location)

//navigator 当前浏览器相关信息
console.log(window.navigator)

Location对象


//hash 获取hash部分(前端路由)
console.log(window.location.hash)

//host 获取当前页面主机名,以https://www.google.com/?hl=zh_CN为例
console.log(window.location.host)   // "www.google.com"

//href 完整url
console.log(window.location.href)   // "https://www.google.com/?hl=zh_CN"

//pathname 路径
console.log(window.location.pathname)   // "/"

//protocol 协议
console.log(window.location.protocol)   // "https:"

// search 查询参数
console.log(window.location.search)     // "?hl=zh_CN"

+号作为一元运算符

//作为一元运算符时会对表达式进行toNumber操作
console.log(typeof +"1")        //nuumber
console.log(1 + +"1" + +"2")    //4
console.log(+"A")               //NaN

document 与 document.documentElement

//window.document返回的是对完整文档结构的引用
console.log(window.document)

//window.document.documentElement返回的是文档的根元素的引用(html及其子元素)
console.log(window.document.documentElement)

Author: Maple
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Maple !
  TOC