問(wèn)題1: apply、call 缓待、bind有什么作用蚓耽,什么區(qū)別
- apply() 方法調(diào)用一個(gè)函數(shù), 其具有一個(gè)指定的this值,以及作為一個(gè)數(shù)組(或類似數(shù)組的對(duì)象)提供的參數(shù)旋炒。
fun.apply(thisArg, [argsArray]) - call() 方法調(diào)用一個(gè)函數(shù), 其具有一個(gè)指定的this值和分別地提供的參數(shù)(參數(shù)的列表)。
fun.call(thisArg[, arg1[, arg2[, ...]]])
使用call方法調(diào)用函數(shù)并且指定上下文的'this' - bind签杈,返回一個(gè)新函數(shù)瘫镇,并且使函數(shù)內(nèi)部的this為傳入的第一個(gè)參數(shù)。bind() 最簡(jiǎn)單的用法是創(chuàng)建一個(gè)函數(shù)答姥,使這個(gè)函數(shù)不論怎么調(diào)用都有同樣的 this 值铣除。還可以配合setTimeout。
fun.bind(thisArg[, arg1[, arg2[, ...]]])
apply()與 call()非常相似鹦付,不同之處在于提供參數(shù)的方式尚粘。就是call()方法接受的是若干個(gè)參數(shù)的列表,而apply()方法接受的是一個(gè)包含多個(gè)參數(shù)的數(shù)組敲长。
問(wèn)題2: 以下代碼輸出什么?
var john = {
firstName: "John"
}
function func() {
alert(this.firstName + ": hi!")
}
john.sayHi = func
john.sayHi()//John: hi!郎嫁,john.sayHi調(diào)用func()
問(wèn)題3: 下面代碼輸出什么,為什么
func()
function func() {
alert(this)
}
//[object Window],在函數(shù)被直接調(diào)用時(shí)this綁定到全局對(duì)象
在瀏覽器中祈噪,window 就是該全局對(duì)象
問(wèn)題4:下面代碼輸出什么
document.addEventListener('click', function(e){
console.log(this);//document
setTimeout(function(){
console.log(this);//window
}, 200);
}, false);
問(wèn)題5:下面代碼輸出什么
var john = {
firstName: "John"
}
function func() {
alert( this.firstName )
}
func.call(john)//John泽铛,john調(diào)用了func
問(wèn)題6: 以下代碼有什么問(wèn)題,如何修改
var module= {
bind: function(){
//添加
var _this = this;
$btn.on('click', function(){
console.log(this) //this指btn
this.showMsg();
})
},
showMsg: function(){
console.log('饑人谷');
}
}
原型鏈相關(guān)問(wèn)題
問(wèn)題7:有如下代碼辑鲤,解釋Person盔腔、 prototype、proto月褥、p弛随、constructor之間的關(guān)聯(lián)。
function Person(name){
this.name = name;
}
Person.prototype.sayName = function(){
console.log('My name is :' + this.name);
}
var p = new Person("若愚")
p.sayName();
//Person是構(gòu)造函數(shù)constructor宁赤,new Person("若愚")創(chuàng)建了Person的一個(gè)對(duì)象p
//Person擁有屬性prototype
//其中Person的實(shí)例p的屬性__proto__指向prototype
prototype的constructor指向Person
問(wèn)題8: 上例中舀透,對(duì)對(duì)象 p可以這樣調(diào)用 p.toString()。toString是哪里來(lái)的? 畫(huà)出原型圖?并解釋什么是原型鏈礁击。
每個(gè)對(duì)象都有一個(gè)私有屬性Prototype, 它持有一個(gè)連接到另一個(gè)稱為其 prototype 對(duì)象的鏈接盐杂。該原型對(duì)象具有一個(gè)自己的原型直到達(dá)到一個(gè)對(duì)象的 prototype 為 null逗载。根據(jù)定義,null 沒(méi)有 prototype链烈,并作為這個(gè)原型鏈中的最后一個(gè)
p的prototype中沒(méi)有toString厉斟,就沿著原型鏈,proto指向Person的prototype强衡,其中也沒(méi)有toString擦秽,沿著原型鏈繼續(xù)指向Object的prototype中找到toString。
問(wèn)題9:對(duì)String做擴(kuò)展漩勤,實(shí)現(xiàn)如下方式獲取字符串中頻率最高的字符
String.prototype.getMostOften = function(){
var arr = []
for(var i=0;i<this.length;i++){
if(arr[this[i]]){
++arr[this[i]]
}else{
arr[this[i]] = 1
}
}
var count = 0
var maxKey
for(var key in arr){
if(arr[key]>count){
count = arr[key]
maxKey = key
}
}
var maxCount = count
return maxKey+':因?yàn)?+maxKey+' 出現(xiàn)了'+maxCount+'次'
}
var str = 'ahbbccdeddddfg'
var ch = str.getMostOften()
console.log(ch); //d , 因?yàn)閐 出現(xiàn)了5次
問(wèn)題10: instanceOf有什么作用感挥??jī)?nèi)部邏輯是如何實(shí)現(xiàn)的?
- instanceof用于判斷某個(gè)變量是否是某個(gè)對(duì)象的實(shí)例越败,返回值為true或false
- instanceof通過(guò)探測(cè)obj.proto.proto... === Constructor.prototype來(lái)驗(yàn)證obj是否是Constructor的實(shí)例触幼。
繼承相關(guān)問(wèn)題
問(wèn)題11:繼承有什么作用?
繼承是指一個(gè)對(duì)象直接使用另一對(duì)象的屬性和方法【糠桑可以提高復(fù)用性,省代碼繼承劃分了類的層次性置谦,父類代表的是更一般、更泛化的類亿傅,而子類則是更為具體媒峡、更為細(xì)化
問(wèn)題12: 下面兩種寫(xiě)法有什么區(qū)別?
//方法1
function People(name, sex){
this.name = name;
this.sex = sex;
this.printName = function(){
console.log(this.name);
}
}
var p1 = new People('饑人谷', 2)
//方法2
function Person(name, sex){
this.name = name;
this.sex = sex;
}
Person.prototype.printName = function(){
console.log(this.name);
}
var p1 = new Person('若愚', 27);
方法一定義的printname方法不在原型上,所以實(shí)例再用的時(shí)候要再定義葵擎,方法二的printName方法放在原型上實(shí)例可以共用
問(wèn)題13: Object.create 有什么作用谅阿?兼容性如何?
Object.create(proto, [ propertiesObject ])方法獲取酬滤,clone了一個(gè)新的prototype签餐。Object.create是ES5方法,之前版本通過(guò)遍歷屬性也可以實(shí)現(xiàn)淺拷貝敏晤。
ES5贱田,對(duì)于低版本瀏覽器兼容效果不好
問(wèn)題14: hasOwnProperty有什么作用? 如何使用嘴脾?
檢測(cè)對(duì)象的屬性是定義在自身上還是在原型鏈上男摧,有必要使用 [hasOwnProperty] 方法,所有繼承自 Object.proptotype 的對(duì)象都包含這個(gè)方法译打。會(huì)返回一個(gè)布爾值
[hasOwnProperty]是 JavaScript 中唯一一個(gè)只涉及對(duì)象自身屬性而不會(huì)遍歷原型鏈的方法耗拓。
m.hasOwnProperty('name'); // true
m.hasOwnProperty('printName'); // false
Male.prototype.hasOwnProperty('printAge'); // true
問(wèn)題15:如下代碼中call的作用是什么?
function Person(name, sex){
this.name = name;
this.sex = sex;
}
function Male(name, sex, age){
Person.call(this, name, sex); //這里的 call 有什么作用:指定了上下文的this值,Male繼承Person的name和sex屬性
this.age = age;
}
問(wèn)題16: 補(bǔ)全代碼奏司,實(shí)現(xiàn)繼承
function Person(name, sex){
this.name = name
this.sex = sex // todo ...
}
Person.prototype.getName = function(){
console.log(this.name)// todo ...
};
function Male(name, sex, age){
Person.call(this, name, sex)
this.age = age//todo ...
}
Male.prototype = Object.create(Person.prototype) //todo ...
Male.prototype.getAge = function(){
console.log(this.age)//todo ...
};
var ruoyu = new Male('若愚', '男', 27);
ruoyu.getName();
ruoyu.getAge();
ruoyu.printName();