转行前端(JS篇12):阶段练习题

169 阅读2分钟
console.log(a)
var a=12
function fn(){
console.log(a);
var a=13
}
fn();
console.log(a)

答案:undefined undefined 12


console.log(a)
var a=12
function fn(){
console.log(a);
a=13
}
fn();
console.log(a)

答案:undefined 12 13  


var foo=1;
function bar(){
if(!foo){
var foo=10;
}
console.log(foo)
}
bar();

答案:10
变量提升
var foo
bar=aaafff000
形参赋值:无
变量提升
var foo(不管条件是否成立,都要进行变量提升,新浏览器中对于判断体中的函数只是提前声明)
foo=>undefined
!foo=>true



var n=0;
function a(){ 
	var n=10;
	function b(){
		n++;
		console.log(n);
}
	b();
	return b;
}
var c=a();
c();
console.log(n)

答案:11、12、0


var a=10,b=11,c=12
function test(a){
a=1;
var b=2;
c=3
}
test(10)
console.log(a)
console.log(b)
console.log(c)

答案:10 11 3



if(!(“a” in window)){
var a=1;
}
console.log(a)

答案:undefined
变量提升
var a;不管条件是否成立都要进行变量提升,在全局作用域下声明的变量,也相当于给window设置了一个对象的属性,而且两者之间也建立了映射机制window.a=undefined
in:检测某一个属性是否隶属于这个对象(不管是私有属性还是公有属性,这要有这个属性结果就是true)
hasOwnProperty:检测某一个属性是否为对象的私有属性(只有这个属性是私有的才可以)


var a=4;
function b(x,y,a){
	console.log(a)
	arguments[2]=10
	console.log(a)
}
a=b(123)//=>a=b执行的结果=>a=undefined【b函数中没有编写return,所以默认函数的返回值是undefined 】
console.log(a)

答案:3,10,undefined
形参赋值:x=1,y=2;a=3
变量提升
arguments:函数内置的实参集合,不管是否设置形参,传递的实参在这个集合都存在
在JS非严格模式下,函数中的形参变量和arguments存在映射机制