- 1.JS面向?qū)ο罄^承都有哪幾種寫法,請(qǐng)?jiān)敿?xì)寫出具體代碼并附上上下文解釋。
原型繼承:
(1)子類.prototype = new 父類;
子類.prototype.constructor = 子類;
繼承父類所有的屬性和方法
(2)子類.prototype = 父類.prototype;
子類.prototype.constructor = 子類;
直接使用父類的原型對(duì)象來(lái)覆蓋子類原型對(duì)象
構(gòu)造函數(shù)繼承:(父類在子類中使用)
call():有多個(gè)參數(shù),取決于父類實(shí)例化的時(shí)候需要傳多少個(gè)參數(shù)
play():有兩個(gè)參數(shù),第二個(gè)參數(shù)是數(shù)組
- 2.簡(jiǎn)述原型鏈。
實(shí)例化出來(lái)的對(duì)象的原型就是其構(gòu)造函數(shù)的原型對(duì)象;
構(gòu)造函數(shù)的原型對(duì)象的原型是Object的原型對(duì)象;
Object的原型對(duì)象的原型是null;
p1 是 Person() 實(shí)例化出來(lái)的對(duì)象
p1.proto 指向 Person.prototype;
Person.prototype.proto 指向 Object.prototype;
Object.prototype.proto 指向 null;
- 3.詳細(xì)描述 _ _ proto _ _ 、prototype和constructor。
_ _ proto _ _ 原型(指向構(gòu)造函數(shù)的原型對(duì)象)
prototype原型對(duì)象(指向object)
constructor (指向)構(gòu)造函數(shù)
4.實(shí)現(xiàn)一個(gè)數(shù)組的深拷貝有幾種寫法,分別是什么?
1.for循環(huán)
let arr = [1,2,3,4,5];
let arr1 = [];
for(let i = 0;i < arr.length;i++){
arr1[i] = arr[i];
}
console.log(arr1);//[1, 2, 3, 4, 5]
2.concat();
let arr2 = [].concat(arr);
console.log(arr2);//[1, 2, 3, 4, 5]
3. ...
let arr3 = [...arr];
let [...arr4] = arr;
console.log(arr3);//[1, 2, 3, 4, 5]
console.log(arr4);//[1, 2, 3, 4, 5]
5.canvas畫圓方法里是什么?參數(shù)分別是什么?
ctx.arc();
6個(gè)參數(shù)
1.圓心x
2.圓心y
3.半徑R
4.起始點(diǎn)和0度j夾角
5.結(jié)束點(diǎn)和0度夾角
6.bool(false默認(rèn)值,順時(shí)針;true逆時(shí)針)
6.簡(jiǎn)述let 和 var 的區(qū)別。
var:(1)存在變量的提升;(2)允許重復(fù)聲明變量;
let:(1)不存在變量的提升;(2)不允許重復(fù)聲明變量;(3)塊級(jí)作用域(局部變量)
7.簡(jiǎn)述箭頭函數(shù)和普通函數(shù)的區(qū)別。
1.箭頭函數(shù)不能用于構(gòu)造函數(shù),普通函數(shù)可以;
2.箭頭函數(shù)全都是匿名函數(shù);
3.箭頭函數(shù)不具有arguments對(duì)象,而每一個(gè)普通函數(shù)調(diào)用后都具有一個(gè)arguments對(duì)象;
4.this指向不同,箭頭函數(shù)(我被定義時(shí),定義的環(huán)境中this指向誰(shuí),我的this就指向誰(shuí)),普通函數(shù)(誰(shuí)調(diào)用我,我的this指向誰(shuí));
8.在數(shù)組原型上補(bǔ)償一個(gè)數(shù)組去重的方法。
9.canvas 編程:畫一個(gè)邊框10px顏色為黑色,填充顏色為紅色,寬高100px的正菱形,菱形的中心店在屏幕正中間。
10.書(shū)寫正則驗(yàn)證用戶名,要求長(zhǎng)度在6-16位,以字母開(kāi)頭,允許字母數(shù)字下劃線。
/^[a-zA-Z]\w{5,15}$/g
11.請(qǐng)用工廠模式創(chuàng)建一個(gè)對(duì)象。
function fn1(name,sex,age){
var obj = new Object();
obj.name = name;
obj.sex = sex;
obj.age = age;
return obj;
}
12.簡(jiǎn)述幀頻率跟計(jì)時(shí)器的區(qū)別及原理,并用幀頻率的方式實(shí)現(xiàn)一個(gè)小球移動(dòng)。
// 獲取canvas標(biāo)簽
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// 時(shí)間軸
var timer = 0;
var deg = Math.PI / 180;
var x1 = 100;
var y1 = 100;
var y2 = 300;
var r = 20;
function run() {
// 幀頻率
var a = requestAnimationFrame(run);
// console.log(a);
timer++;
// console.log(timer);
canvas.width = canvas.width;
x1 += 2;
ctx.beginPath();
ctx.arc(x1, y1, r, 0, 360 * deg);
ctx.strokeStyle = '2';
ctx.fillStyle = 'red';
ctx.fill();
ctx.stroke();
}
run();