1. 下面的代码输出多少?修改代码让 fnArr i 输出 i。使用 两种以上的方法
var fnArr = [];
for (var i = 0; i < 10; i ++) {
fnArr[i] = function(){
return i;
};
}
console.log( fnArr[3]() ); //
方法一:
var fnArr = [];
for (var i = 0; i < 10; i ++) {
fnArr[i] = (function(i){
return function(){
return i;
}
})(i);
}
console.log(fnArr[3]()); //
方法二:
var fnArr = [];
for (var i = 0; i < 10; i ++) {
fnArr[i] = (function(){
return i;
})(i);
}
console.log(fnArr[3]);
方法三:
var fnArr = [];
for (var i = 0; i < 10; i ++) {
(function(i){
fnArr[i] = function(){
return i;
};
})(i);
}
console.log(fnArr[3]());
2. 封装一个汽车对象,可以通过如下方式获取汽车状态
var Car = (function(){
var speed = 0;
function setSpeed(s){
speed = s
}
...
return {
setSpeed: setSpeed,
...
}
})()
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
var Car = (function(){
var speed = 0;
function setSpeed(s){
speed = s
}
function getSpeed(){
return speed;
}
function accelerate(){
speed = speed + 10;
}
function decelerate(){
speed = 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);
console.log(Car.getSpeed()); //30
Car.accelerate();
console.log(Car.getSpeed()); //40;
Car.decelerate();
Car.decelerate();
console.log(Car.getSpeed()); //20
console.log(Car.getStatus()); // 'running';
Car.decelerate();
Car.decelerate();
console.log(Car.getStatus()); //'stop';
3.下面这段代码输出结果是? 为什么?
var a = 1;
setTimeout(function(){
a = 2;
console.log(a);//2
}, 0); //参数为0,被放入执行队列的最后
var a ;
console.log(a); //1
a = 3;
console.log(a); //3
输出:
1 3 2
因为setTimeout最后才执行
4. 下面这段代码输出结果是? 为什么?
var flag = true;
setTimeout(function(){
flag = false;
},0)
while(flag){}
console.log(flag);
没有输出结果
因为 setTimeout最后执行
所以 while一直在循环
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 delayer:5
for(var i=0;i<5;i++){
(function(i){
return setTimeout(function(){
console.log('delayer:' + i );
}, 0);
})(i);
}
6. 如何获取元素的真实宽高
function trueStyle(element,pseduoElement){
//IE不支持window.getComputedStyle(),支持element.currentStyle();
return element.currentStyle ? element.currentStyle: window.getComputedStyle(element,pseduoElement);
}
let trueWidth = trueStyle(element).width;
let trueHeight = trueStyle(element).height;
7. URL 如何编码解码?为什么要编码?
let myURL = 'https://www.google.com/#q=javascript';
//如果我们想编码一个URL并且可以使用它(访问),使用encodeURI();
let simpleURL = encodeURI(myURL); //"https://www.google.com/#q=javascript"
//如果我们想编码一个URL并且可以将其放置在某URL的参数中,使用 encodeURIComponent();
let completeURL = encodeURIComponent(myURL);
let newURL = 'https://www.google.com/?back=' + completeURL; //"https://www.google.com/? back=https%3A%2F%2Fwww.google.com%2F%23q%3Djavascript"
window.open(simpleURL); //将会打开一个窗口,地址为 https://www.google.com/#q=javascript
8. 补全如下函数,判断用户的浏览器类型
function isAndroid(){
}
funcnction isIphone(){
}
function isIpad(){
}
function isIOS(){
}
function isAndroid(){
return /Android/.test(navigator.userAgent);
}
funcnction isIphone(){
return /iPhone/.test(navigator.userAgent);
}
function isIpad(){
return /iPad/.test(navigator.userAgent);
}
function isIOS(){
return /(iPad)|(iPhone)/i.test(navigator.userAgent);
}
网友评论