好玩的三道題:
function foo(){
var i =0;
return function(){
console.log(i++);
}
}
var foo1 = foo();
var foo2 = foo();
foo1();
foo1();
foo2();
//0 1 0
解析:第一次調用foo1()後,下次不再執行var i= 0了,直接調用return裡的函數
html布局:
布局題:
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
#father{
width: 300px;
border: 1px solid red;
}
#child1{
width: 100px;
height: 100px;
background: red;
float: left;
}
#child2{
width: 100px;
height: 100px;
background: blue;
float: left;
}
#next{
width: 300px;
height: 300px;
background: yellow;
}
</style>
<body>
<div id="father">
<div id = "child1">div1</div>
<div id="child2">div2</div>
</div>
<div id = "next">hello</div>
</body>
<script>
</script>
</html>
-
注意下hello的位置,思考下為啥?注意最上方的那條紅線為啥?
關于hello的位置涉及到position和float的差別:
因為position:absolute是完全脫離文檔流,其他盒子與其他盒子内的文本都不會受到影響;而float是一種半脫離狀态,被float的元素,文字會環繞到它的旁邊。
紅線主要是以為子元素浮動,沒有撐開父元素,導緻父元素“坍塌”成一條線
面試題三、
function f(n){
n=n||2;
return function(x){
return (x*n);
}
}
var f2 = f(3);
var f3 = f();
console.log(f2(3));
console.log(f3(3));
console.log(f3(f2(3)));
類似題一,debugger試試?