面试题(代码执行结果题)

88 阅读3分钟

代码执行结果考题

5.1 请写出以下打印顺序

console.log( 1 )
setTimeout(()=>{
	console.log( 4 )
},0)
Promise.resolve().then(()=>{
	console.log(5)
})
console.log(3)

结果:1、3、5、4

5.2 下面程序的打印结果是什么?

var student = {
	firstName:'Alice',
	lastName:'Jones',
	age:21,
	sayHi:()=>{
		return 'Hi,I am' + this.firstName;
	}
}
student.sayHi()

答案:Hi,I amundefined

5.3 下面的执行结果是什么?

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

答案:1、2、4、3

5.4 下面两个程序的打印结果分别是什么?

for(var i=0;i<3;i++){
	setTimeout(()=>{
		console.log(i);
	})
}

答案:3个3

for(let i=0;i<3;i++){
	setTimeout(()=>{
		console.log(i);
	})
}

答案:0、1、2

5.5 写出下面代码执行结果,并解释原因

let a = { n:1 };
let b = a;
a.x = a = { n:2 };
console.log( a.x );
console.log( b.x );

答案:undefind、{n:2}

1. 前面2行代码==>指向同一个地址的数据:{n:1}
	let a = { n:1 };
	let b = a;
2. 最关键是这里:a.x = a = { n:2 };
	这里要注意.的优先级要高于=
	所以a.x:去找a的x,没有那就是undefined,这里就变成了{ n:1,x:undefined}
	然后 = a = {n:2},这里是从右往左赋值。那么a的指向就变成了{n:2}
	然后a.x = a 相当于{n:1,x:undefined}.x = a,这个结果最后就是
	a = {n:2}
	b= {n:1,x:{n:2}}
3. 那么最终的结果就是undefined和{n:1}

5.6 以下代码打印顺序为

async function async1(){
	console.log('1');
	await async2();
	console.log('2');
}
async function async2(){
	console.log('3')
}
console.log('4');
async1();
console.log('5');

答案:4、1、3、5、2

5.7 test1()的执行结果

const test = async ()=>{
	console.log(2);
	await new Promise(function(resolve,reject){ resolve() })
	console.log(3);
}
const test1 = ()=>{
	console.log(1);
	test();
	console.log(4);
}
test1();

答案:1、2、4、3

5.8 obj.a()、obj.b()、new obj.a()、new obj.b()的输出结果分别是什么

var id = 'GLOBAL';
var obj = {
	id:'OBJ',
	a:function(){
		console.log(this.id );
	},
	b:()=>{
		console.log(this.id );
	}
}

答案:

obj.a()//OBJ obj.b()//GLOBAL new obj.a()//这里是undefined,因为new这个a,这个a没有id这个属性。所以是undefined new obj.b()//箭头函数不能new,不能当作构造函数==》报错

5.9 下列打印结果是?

var foo = 'hello';
(function( foo ){
	console.log( foo );
	var foo = foo || 'word';
	console.log( foo );
})( foo )
console.log( foo );

答案:hello、hello、hello

5.10 写出下面js的运行后的结果

var a = 100;
function test(){
	alert( a );
	var a = 10;
}
test();

答案:undefined

5.11 写出下面js的运行后的结果

this.m = 1000;
var obj = {
	m:100,
	test:function(){
		return function(){
			alert( this.m );
		}
	}
}
obj.test()();

答案:1000

5.12 看下列代码,将会输出什么?

var foo = 1;
(function(){
	console.log( foo )
	var foo = 2;
	console.log( foo );
})()

答案:undefined、2

5.13 请写出下面代码输出结果

var myObj = {
	name:'小鹿线1',
	showThis:function(){
		console.log( this );
		var self = this;
		function bar(){
			self.name = '小鹿线2';
		}
		bar();
	}
}
myObj.showThis();
console.log( myObj.name );
console.log( window.name );

答案:小鹿线2、空(因为window自带一个name属性,这个属性是空字符串)

5.14 以下JS程序的输出是什么

var a = 'undefined';
var b = 'false';
var c = '';
function assert(aVar){
	if( aVar ){
		alert( true );
	}else{
		alert( false );
	}
}
assert( a );
assert( b ); 
assert( c );

答案:true、true、false

5.15 以下程序的输出是什么

function Foo(){
	var i = 0;
	return function(){
		document.write( ++i );
	}
}
var f1 = Foo();
var f2 = Foo();
f1();
f1();
f2();

答案:1、2、1

5.16 输出下面代码执行结果

var iterable = new Set([1,1,2,2,3,3]);
var value = 0;
for(let value of iterable){
	console.log( value );
}
console.log( value );

答案:1、2、3、0

5.17 写出以下代码执行顺序

const f1 = ()=>{
	return new Promise((resolve)=>{
		console.log('f1');
		resolve('f2');
		console.log('f3');
	})
}
const async1 = async ()=>{
	console.log('a1');
	const a2 = await f1();
	console.log('a2');
	console.log('a3');
}
console.log('start');
setTimeout(()=>{
	async1();
	console.log('f4');
	
},500);
console.log('end');

答案:start、end、a1、f1、f3、f4、a2、a3