1.下面的代码输出多少?修改代码让fnArri 输出 i。使用两种以上的方法
<script>
var fnArr = [];
for (var i = 0; i < 10; i ++) {
(function () {
var s = i;
fnArr[i] = function () {
return s;
};
})()
}
console.log( fnArr[3]() ); //
</script>
<script>
var fnArr = [];
for (var i = 0; i < 10; i ++) {
(function (n) {
fnArr[i] = function () {
return n;
};
})(i)
}
console.log( fnArr[3]() ); //
</script>
2.封装一个汽车对象,可以通过如下方式获取汽车状态
<script>
var Car = (function(){
var speed = 0;
function setSpeed(s){
speed = s
}
function getSpeed () {
return speed;
}
function accelerate () {
return speed += 10;
}
function decelerate () {
return speed -= 10;
}
function getStatus () {
if (speed > 0)
{
return 'running';
}
else
{
return 'stop';
}
}
return {
'setSpeed': setSpeed,
'getSpeed': getSpeed,
'accelerate': accelerate,
'decelerate': decelerate,
'getStatus': getStatus
}
})()
Car.setSpeed(30);
Car.getSpeed(); //30
Car.accelerate();
Car.getSpeed(); //40;
Car.decelerate();
Car.decelerate();
Car.getSpeed(); //20
Car.getStatus(); // 'running';
Car.decelerate();
Car.decelerate();
Car.getStatus(); //'stop';
//Car.speed; //error
</script>
3.下面这段代码输出结果是? 为什么?
var a = 1;
setTimeout(function(){
a = 2;
console.log(a);
}, 0);
var a ;
console.log(a);
a = 3;
console.log(a);
/*1
3
2*/ //定时器的执行队列问题
4.下面这段代码输出结果是? 为什么?
var flag = true;
setTimeout(function(){
flag = false;
},0)
while(flag){}
console.log(flag);
//死循环,setTimeout在代码解释完后的队列中
5.下面这段代码输出?如何输出delayer: 0, delayer:1...
(使用闭包来实现)
for(var i=0;i<5;i++){
setTimeout(function(){
console.log('delayer:' + i );
}, 0);
console.log(i);
}
/*
0
1
2
3
4
5:delayer:5
*/
<script>
for(var i=0;i<5;i++){
(function () {
var n = i;
setTimeout(function(){
console.log('delayer:' + n );
}, 0);
})()
console.log(i);
}
</script>
6.如何获取元素的真实宽高
- 主流浏览器:window.getComputesStyle
var style = window.getComputedStyle("元素", "伪类");
- 低版本IE:element.currentStyle
7.URL 如何编码解码?为什么要编码?
- decodeURI()
- decodeURIComponent()
- encodeURI()
- encodeURIComponent()
因为有些字符会引起歧义
8.补全如下函数,判断用户的浏览器类型
···
<script>
function isAndroid(){
return /Android/.test(navigator.userAgent);
}
function isIphone(){
return /iphone/.test(navigator.userAgent);
}
function isIpad(){
return /iPad/.test(navigator.userAgent);
}
function isIOS(){
return /(iPad)|(iPhone)/i.test(navigator.userAgent);
}
</script>
···
网友评论