面试1

2021-01-20  本文已影响0人  天问ing

知识点

比较奇怪的题目

  1. 写一段代码,可以使下面的代码运行成功 if( a == 1 && a == 2 && a == 3) { console.log('运行成功') }

    // 可以利用隐式转换进行操作
    let a = {
        _default: 0,
        toString() {
            return ++this._default
        }
    }
    if (a == 1 && a == 2 && a == 3) {
        console.log("运行成功")
     }
    
  2. 写一段代码,可以使下面的代码运行成功 if( a === 1 && a === 2 && a === 3) { console.log('运行成功') }

    // 可以利用Object.defineProperty里面的getter进行实现
    let _default = 0
    Object.defineProperty(window, 'a', {
        get() {
           return ++_default 
        }
    })
    if (a === 1 && a === 2 && a === 3) {
        console.log("运行成功")
     }
    

Function

Function 和 new Function一样可以创建函数, 但是不会产生闭包,创建的出来的函数,变量指向全局的作用域

// 有如下的一段代码
let a = 1 , b = 2;
// 1. 以函数声明定义的
function test() {
    let b = 3
    return function(c) {
        console.log(a + b + c)
    }
}
test()(4) // 打印结果是: 8
// 2. 以Function和new Function创建的
function test1() {
    let b = 3
    return new Function('c', 'console.log(a + b + c)')
}
test1()(4) // 打印结果是:7

将扁平化的数据转换成树形结构数据

提供如下的数据, 转换成树形的数据

[
    {
        "name": "微软",
        "pId": "0",
        "id": "1"
    },
    {
        "name": "苹果",
        "pId": "0",
        "id": "2"
    },
    {
        "name": "widnow",
        "pId": "1",
        "id": "11"
    },
    {
        "name": "iphone",
        "pId": "2",
        "id": "21"
    },
    {
        "name": "iphone8",
        "pId": "21",
        "id": "211"
    },
]
// 第一种方案,分成两步骤,先将第一级的选择出来, 后续递归处理子级
function transformToTree(data) {
    let parents = data.filter(item => item.pId === '0')
    let children = data.filter(item => item.pId !== '0')
    return  getChildren(parents, children)
}

function getChildren(parents, children) {
    return parents.map(p => {
        children.map((c,i) => {
            if(p.id === c.pId) {
                let _children  = JSON.parse(JSON.stringify(children))
                _children.splice(i, 1)
                getChildren([c], _children)
                if(p.children) {
                   p.children.push(c)
                 }else {
                    p.children = [c]
                 }
            }
        })
        return p
    })
}
// 第二种方案: 扁平化的处理方式
function transformToTree1(data) {
   return data.filter(p => {
       data.map(c => {
           if(p.id === c.pId) {
            if(p.children) {
                   p.children.push(c)
                 }else {
                    p.children = [c]
                 } 
           }
       })
       return p.pId === '0'
   }) 
}

函数的各种调用方式

function Foo() {
    getName = function() {
        console.log(1)
    }
    return this
}
Foo.getName = function() {
    console.log(2)
}
Foo.prototype.getName = function() {
    console.log(3)
}
var getName = function() {
    console.log(4)
}
function getName() {
    console.log(5)
}
Foo.getName()  // 打印2
getName() // 打印4, 注意函数声明会被提升, 函数表达式则会被后续执行, 所以一开始的全局的getName会打印4
Foo().getName() // 打印1 , Foo函数执行,首先全局的getName被重新赋值, 在浏览器环境下, 返回值this指向window,调用函数打印1
getName() // 打印1 , 执行全局函数, 目前是打印1
new Foo.getName() // 打印2 , 相当于执行Foo.getName()
new Foo().getName() // 打印3, 实例化一个对象, 本身不存在getName,往原型链上面查找
new new Foo().getName() // 打印3 , 和上面的操作一样
上一篇 下一篇

猜你喜欢

热点阅读