01、如何判斷?個變數是不是陣列?
let arr = [1,2,3,4]
function fun(){
return arr instanceof Array
}
02、如何使?class實作繼承?
class fu {consructor(name,age)}
class son extends fu{super(...arguments))}
擴展:
//父類
class People {
constructor(name, age) {
this.name = name;
this.age = age;
}
eat() {
console.log("吃吃吃");
}
}
// 子類
class Student extends People {
constructor(name, age) {
super(name, age);
}
sayHi() {
console.log(this.name + " " + this.age);
}
}
let stu = new Student("abc", 20);
stu.eat();
【原型】
console.log(stu.__proto__) 隱式原型
console.log(Student.prototype) 顯示原型
console.log(Student.prototype === stu.__proto__)
每個類都有顯示原型、每個實體都有隱式原型
實體的隱式原型__proto__指向類的顯示原型prototype
【原型鏈】
console.log(Student.prototype.__proto__)
console.log(People.prototype)
console.log(Student.prototype.__proto__ === People.prototype)

03、this在不同場景下如何取值?
this的值是什么,取決于調?什么時候,與定義?關
舉例:
// 在普通函式
function fn(){
alert(this) // this -> window
}
fn()
// 使? call、apply、bind
let fn2 = fn.bind({a:'abc'})
fn2() // this -> {a:'abc'}
1,以普通函式的形式呼叫,this是window
2,以物件的方法被呼叫時,this是呼叫方法的物件
3,以建構式形式呼叫,this是新創建的物件
4,使用call和apply呼叫時,this是call和apply的第一個引數,如果引數為空,默認指向全域物件
5,全域作用域中this是window
6,箭頭函式的this由外層作用域決定
04、?寫bind函式
Function.prototype.myBind = function(){
let arr = Array.prototype.slice.call(arguments)
let newThis = arr.shift()
let _this = this
return function(){
_this.apply(newThis,arr)
}
}
05、閱讀代碼,填寫結果
function foo(fn){ const a=200; fn()}const a=100;function fn() { console.log(a)}foo(fn) 結果是多少?
100
function foo() {
const a=100;
return function (){
console.log(a)
}
}
const fn=foo();
const a=200;
fn() 結果是??
100
作?域 —— 變數的合法使?范圍,全域作?域、函式作?域、塊級作
?域
?由變數 —— ?個變數在當前作?域沒有定義,但被使?了
閉包(56T) —— 所有?由變數的查找,是在函式定義的地?向上級作?域查找
06、實際開發中閉包的應?場景,舉例說明
// 隱藏資料,資料被隱藏,外部?法訪問
function myData(){
let data=https://www.cnblogs.com/LiuSan/archive/2022/10/26/{}
return {
setData(key, value){
data[key]=value
},
getData(key) {
return data[key ]
}
}
}
閉包就是能夠讀取其他函式內部變數的函式
07、閱讀代碼,填寫結果
// ??中有3個按鈕
let aBtn=document.getElementsByClassName('btn')
for (var i=0; i<aBtn.length; i++) {
aBtn[i].addEventListener('click', function (){
alert(i) // 每個按鈕點擊時,i的值分別是什么?
})
}
3,3,3
當按鈕執行的時候,for回圈已經執行完了,解決方法用立即執行 (function (){})():這也算一個閉包
for (var i = 0; i < aBtn.length; i++) {
(function (index) {
aBtn[i].addEventListener("click", function () {
alert(index);
});
})(i);
}
08、閱讀代碼,填寫結果
console.log(1)
setTimeout(() => {
console.log(2)
}, 1000)
console.log(3)
setTimeout(() => {
console.log(4)
}, 0)
console.log(5)
1,3,5,4,2
同步和異步的區別是什么?
js是單執行緒的,同步就是?件事做完再做下?件事,
異步是多件事情?起做
同步阻塞代碼執?,異步不阻塞代碼執?
前端常?異步場景有哪些?
以下2種情況必須使?異步
?絡請求 ajax
$.ajax(url, function (data){ console.log(data) })
定時任務 setTimeout setInterval
09、?寫Promise加載?張圖?
let url = '01.jpg' // 地址
function fun (){
let oimg = docments.createElements('img')
return new Promise((res,req)=>{
oimg.src = https://www.cnblogs.com/LiuSan/archive/2022/10/26/url
oimg.onload = function (){
res(oimg)
}
oimg.onerror = function (){
req(new Error('失敗'))
}
})
}
10、請描述 event loop的機制,可畫圖
console.log('start')
setTimeout(() => {
console.log('timeout')
}, 5000)
console.log('end'
個人博客地址:http://blog.qianbaiyv.cn/get/my/blog/details/2311887075%40qq.com/121
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/519400.html
標籤:其他

