博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
前端面试经典
阅读量:7154 次
发布时间:2019-06-29

本文共 1939 字,大约阅读时间需要 6 分钟。

这边列举了5个前端经典的面试题

首先我们来看一下题目,都各自思考一下,自己是否能够做出来。然后再跟后面的答案对比一下

题目1

var a = {n:1}var b = {n:2}b.x = a = {n:3}console.log(b.x)a.x = a = {n:3}console.log(a.x)

题目2

(1)var start = new Date()setTimeout(function() {    console.log(new Date() -start)}, 500)(2)var start = new Date()setTimeout(function() {    console.log(new Date() -start)}, 500)while((new Date() - start <= 1000) {}

题目3

var log = console.logvar hint = window.alertvar write = document.writelog('123')hint('123')wirte('123')

题目4

var name = 'A'function getName() {    return this.name}var obj = {    name: 'B',    getName: function() {        return this.name    },    showName:function(a) {        console.log(getName())        console.log(a())        console.log(a === arguments[0])        console.log(arguments[0]())    } }obj .showName(getName, 1)

题目5

async function async1() {    console.log('async1 start')    await async2()    console.log('async1 end')}async function async2() {    console.log(async2())}console.log('script start')setTimeout(function() {    console.log('setTimeout')}, 0)async1()new Promise(function (resolve) {    console.log('promise1')    resolve()}).then(function() {    console.log('promise2')})console.log('script end')

答案1

console.log(b.x)   // {n:3}console.log(a.x)   // undefined这是跟js的优先级有关的 .的优先级最高,因此先计算左边的b.x,a.x

答案2

(1)console.log(new Date() -start)  // 不是一定500,而是大于等于500主要是由于定时器的时间不一定准,这个跟异步的机制有关当定时器的时间到了之后,执行函数会进入栈中,会根据栈中的执行任务,依次执行(不懂的可以学习一下异步机制)(2)console.log(new Date() -start)  // 大于等于1000由于是单线程

答案3

log('123') // 正常hint('123') // 正常write('123') // 报错这是由于write是相当于window.write;而document.write下面应该会用到this对象,所以会报错应当改成window.write(document, '123')

答案4

console.log(getName()) // 'A'  this === windowconsole.log(a()) // 'A' this === windowconsole.log(a === arguments[0]) // trueconsole.log(arguments[0]()) // undefined this 为argument对象

答案5

script startasync1 startasync2promise1async1 endscript endpromise2setTimeout这一块跟异步有关先同步,后异步,在回调。还有promise机制

转载地址:http://sgegl.baihongyu.com/

你可能感兴趣的文章
Chrome 插件
查看>>
c++的重载,覆盖与隐藏
查看>>
大数据的三个入口
查看>>
void指针
查看>>
hackerrank-knapsack
查看>>
Atitit. 软件开发中的管理哲学--一个伟大的事业必然是过程导向为主 过程导向 vs 结果导向...
查看>>
hive load from hdfs出错
查看>>
IOS开发:xcode5版本引发的问题
查看>>
asp.net 负载均衡下session存储的解决方法
查看>>
构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(17)-LinQ动态排序
查看>>
领域驱动开发推荐代码示例 — Microsoft NLayerApp
查看>>
Linux 安装Rsync和配置
查看>>
hadoop fs -mkdir testdata错误 提示No such file or directory
查看>>
etcd的学习心得和使用
查看>>
AAAI 2018论文解读 | 基于置信度的知识图谱表示学习框架
查看>>
服务器命令审计
查看>>
【Go语言】【1】windows操作系统下GO环境配置
查看>>
zebra路由软件使用大全
查看>>
Android WebService
查看>>
PostgreSQL security - don't use password method in pg_hba.conf
查看>>