整理几个被遗忘的js基础代码题,还都能答对吗?

本文通过一系列JavaScript面试题目,包括闭包、原型链、作用域、变量声明的区别、值传递问题以及Promise的用法,来检验开发者对JavaScript基础知识的掌握程度。通过分析代码执行结果,深入理解这些核心概念。
摘要由CSDN通过智能技术生成

我们每个人面试的时候,面试官都会问一些js基础的概念。比如说说你对闭包的理解,说说你对原型和原型链的理解,作用域和作用域链的理解,var、let、const的区别、promise的理解等等,相信这些概念性的东西,我们都能回答个八九不离十。

但如果面试官把这些概念性的问答,换成代码题让你来分析呢?是否还都能给出正确答案,并且说出为什么得出这个结果呢?

其实看代码,分析执行结果才是最考验人,是真是假一试便知啊。

作用域考察

下面代码输出什么?为什么?

如果这个题回答不准确,估计会被直接挂掉,毕竟太基础了。

第1题

var a=100 
function f1(){ console.log(a) } 
function f2(){ var a=200 f1() } 
f2()
var a=100 
function f1(){ console.log(a) } 
function f2(fn){ var a=200 fn() } 
f2(f1)

var a=100 

function f2(){ 
  var a=200 
  function f1(){ console.log(a) } 
  retunr f1
} 
var f = f2()
f()

闭包考察

第2题

let x = 1;
function A(y){
    let x = 2;
    function B(z){
        console.log(x+y+z); //会输出什么? 为什么?
    }
    return B;
}
let C = A(2);
C(3);

原型考察

第3题

var F = function () {}
Object.prototype.a = function () {}
Function.prototype.b = function () {}

var f = new F()

请问f有方法a  方法b吗 为什么?

值传递问题

第4题


var a=100
var b={a:200}

function f1(a1){
 a1= -100
}

function f2(b1){
 b1.a= -200
}


f1(a)
//a 

f2(b)
// b

请分析 f1 和 f2 函数执行后, a 和 b的值?为什么?


如果f2 改成下面的呢?f2 执行后 b 的值是什么? 为什么?

function f2(b1){
 b1 = {
  a:-200
 }
}

promise 考察

第5题

const promise = new Promise((resolve, reject) => {
    console.log(1);
    resolve();
    console.log(2);
    reject('error');
})
promise.then(() => {
    console.log(3);
}).catch(e => console.log(e))
console.log(4);

第6题

const promise = new Promise((resolve, reject) => {
        setTimeout(() => {
             console.log('once')
             resolve('success')
        }, 1000)
 })
promise.then((res) => {
       console.log(res)
     })
promise.then((res) => {
     console.log(res)
 })

第7题

Promise.resolve(1)
.then(2)
.then(Promise.resolve(3))
.then(console.log)

第8题

Promise.resolve(1).then((res)=>{
  throw new Error('dd')
},(error)=>{
console.log('error1',error)
}).catch(e=>{
console.log('error2',e)
})
Promise.resolve(1)
.then((res)=>{
  throw new Error('dd')
},(error)=>{
console.log('error1',error)
})
.then((res)=>{conso.log('ok')},
 (error)=>{console.log('error2',error)
}

彩蛋 -邀你作答

以上题目没有写答案,还是想邀请小伙伴们来一起回答下。

可以选择一个题目,把答案写在留言区,我会找出5个精选评论送出【5个8.8元】红包。(什么是精选评论,首先结果正确,而且还能够说清楚为什么得出这个结果?也就是你的理解是什么?)

小伙伴们回答的时候,记得带上题号哈。

被选中的,我会直接给你回复,通知加我领奖哦。

点个『在看』支持下 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zz_jesse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值