題目1:下面的代碼輸出多少?修改代碼讓fnArri 輸出 i。使用兩種以上的方法
var fnArr = [];
for (var i = 0; i < 10; i ++) {
fnArr[i] = function(){
return i;
};
}
//輸出10,執行的時候i等于10,所以整個數組元素執行后都會是10
console.log( fnArr[3]() );
作用域鏈:
globalContext = {
AO:{
fnArr:[function1,function2,...,function10]
i:10
function1:
function2:
...
function10:
}
scope:null
}
function1.[[scope]] = globalContext.AO;
function2.[[scope]] = globalContext.AO;
function1Context = {
AO:{
}
scope:function1.[[scope]];//globalContext.AO;
}
function2Context = {
AO:{
}
scope:function2.[[scope]];//globalContext.AO;
}
法一:
var fnArr = [];
for (var i = 0; i < 10; i ++) {
(function(i){
fnArr[i] = function(){
return i;
}
})(i);
}
console.log( fnArr[3]() ); //3
作用域鏈:
globalContext = {
AO:{
fnArr:[function11,function22,...,function1010]
i:
function1:
function2:
...
function10:
}
scope:null
}
function1.[[scope]] = globalContext.AO;
function2.[[scope]] = globalContext.AO;
function1Context = {
AO:{
i:0;
function11:
}
scope:function1.[[scope]];//globalContext.AO;
}
function11.[[scope]] = function1Context.AO;
function11Context = {
AO:{
}
scope:function11.[[scope]];//function1Context.AO;
}
function2Context = {
AO:{
i:1;
function22:
}
scope:function2.[[scope]];//globalContext.AO;
}
function22.[[scope]] = function2Context.AO;
function22Context = {
AO:{
}
scope:function22.[[scope]];//function2Context.AO;
}
法二:
var fnArr = [];
for (var i = 0; i < 10; i ++) {
fnArr[i] = (function(i){
//IIFE & 閉包
return function(){
return i;
}
})(i);
}
console.log( fnArr[3]() ); //3
作用域鏈:
globalContext = {
AO:{
fnArr:[function1,function2,...,function10]
i:
function1:
function2:
...
function10:
}
scope:null
}
function1.[[scope]] = globalContext.AO;
function2.[[scope]] = globalContext.AO;
function1Context = {
AO:{
i:0;
function11:
}
scope:function1.[[scope]];//globalContext.AO;
}
function11.[[scope]] = function1Context.AO;
function11Context = {
AO:{
}
scope:function11.[[scope]];//function1Context.AO;
}
function2Context = {
AO:{
i:1;
function22:
}
scope:function2.[[scope]];//globalContext.AO;
}
function22.[[scope]] = function2Context.AO;
function22Context = {
AO:{
}
scope:function22.[[scope]];//function2Context.AO;
}
法三:
var fnArr = [];
for (let i = 0; i < 10; i ++) {
//使用ES6: let
fnArr[i] = function(){
return i;
};
}
console.log( fnArr[3]() );
題目2:封裝一個汽車對象,可以通過如下方式獲取汽車狀態
var Car = (function () {
let speed = 0;
function setSpeed(s){
return speed = s;
}
function getSpeed(){
return speed;
}
function accelerate(){
return speed+=10;
}
function decelerate(){
//速度不能為負數
return speed>0?speed-=10:speed;
}
function getStatus(){
return speed>0?'running':'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
代碼:
var Car = {
speed:0,
setSpeed:function(s){
return speed = s;
},
getSpeed:function(){
return speed;
},
accelerate:function(){
return speed+=10;
},
decelerate:function(){ //速度不能為負數
return speed>0?speed-=10:speed;
},
getStatus:function(){
return speed>0?'running':'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
題目4:下面這段代碼輸出結果是? 為什么?
var flag = true;
setTimeout(function(){
//等待所有任務結束后執行
flag = false;
},0)
while(flag){} //setTimeout會等待它執行完畢,此時flag永遠是true,無限循環。
console.log(flag); //不會執行
題目5:下面這段代碼輸出?如何輸出delayer: 0, delayer:1...
(使用閉包來實現)
for(var i=0;i<5;i++){
(function(t){
//參數變量提升 let t;
return setTimeout(function(){
console.log('delayer:' + t );
}, 0);
})(i);
console.log(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(){
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);
}