一、apply、call 、bind有什么作用,什么區(qū)別?
首先說(shuō)明,這三者的主要用途都是為了:調(diào)用函數(shù)時(shí),使用你指定的this
- apply
調(diào)用一個(gè)函數(shù),為其指定
this
,參數(shù)以數(shù)組形式傳入。
- call
調(diào)用一個(gè)函數(shù),為其指定
this
,分別提供參數(shù)。
- bind
返回一個(gè)函數(shù),并為其指定
this
如(當(dāng)全局環(huán)境變量不是window
的時(shí)候,直接運(yùn)行sum
可能返回NaN
):
var value = 2
function sum(a, b) {
// console.log(this.value + a + b, 1)
return this.value + a + b
}
//直接調(diào)用
sum(3, 4) // 2 + 3 + 4 = 9
//apply 指定 this,參數(shù)以數(shù)組形式傳遞
var obj1 = {
value: 5
}
sum.apply(obj1, [3, 4]) // 5 + 3 + 4 = 12
//call 指定 this,分別提供參數(shù)
var obj2 = {
value: 6
}
sum.call(obj2, 3, 4) // 6 + 3 + 4
//bind 指定 this,返回一個(gè)新的函數(shù)
var obj3 = {
value: 7
}
var newSum = sum.bind(obj3)
newSum(3,4) // 7 + 3 + 4
二、 以下代碼輸出什么?
var john = {
firstName: "John"
}
function func() {
alert(this.firstName + ": hi!")
}
john.sayHi = func
john.sayHi()
輸出John: hi!
,因?yàn)檫@里的sayHi
是john
調(diào)用的,所以this
指向的就是join
對(duì)象。
三、下面代碼輸出什么,為什么
func()
function func() {
alert(this)
}
在瀏覽器運(yùn)行時(shí),this
是window
,因此,代碼運(yùn)行后,會(huì)彈窗顯示window
對(duì)象。
四、下面代碼輸出什么
document.addEventListener('click', function(e){
console.log(this);
setTimeout(function(){
console.log(this);
}, 200);
}, false);
第一個(gè)this
是document
,因?yàn)槭录壎ㄗ詣?dòng)把this
指向?yàn)閷?duì)應(yīng)元素。第二個(gè)this
為window
,因?yàn)槟涿瘮?shù)的執(zhí)行是在window
全局下的。
五、下面代碼輸出什么,why
var john = {
firstName: "John"
}
function func() {
alert( this.firstName )
}
func.call(john)
彈窗顯示John
,因?yàn)?code>call指定了this
為john
對(duì)象。
六、以下代碼有什么問(wèn)題,如何修改
var module= {
bind: function(){
$btn.on('click', function(){
console.log(this) //this指什么
this.showMsg();
})
},
showMsg: function(){
console.log('饑人谷');
}
}
上面的this
指向調(diào)用事件的 DOM 元素,要想讓this
指向?qū)ο蟊旧恚梢韵缺4嬖瓉?lái)的this
,如:
var module= {
bind: function(){
var self = this
$btn.on('click', function(){
console.log(self)
self.showMsg();
})
},
showMsg: function(){
console.log('饑人谷');
}
}
七、有如下代碼,解釋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ù),prototype
是構(gòu)造函數(shù)的共有屬性。p
是Person
的實(shí)例,該實(shí)例有__proto__
對(duì)象指向Person.prototype
。constructor
表示該實(shí)例的構(gòu)造器,指向Person
。
p.__proto__.constructor === Person.prototype.constructor === Person
八、上例中,對(duì)對(duì)象 p可以這樣調(diào)用 p.toString()。toString是哪里來(lái)的? 畫(huà)出原型圖?并解釋什么是原型鏈。
toString
是p
原型鏈上Object
構(gòu)造函數(shù)的方法。
九、對(duì) String 做擴(kuò)展,實(shí)現(xiàn)如下方式獲取字符串中頻率最高的字符
題目
var str = 'ahbbccdeddddfg';
var ch = str.getMostOften();
console.log(ch); //d , 因?yàn)閐 出現(xiàn)了5次
解決
String.prototype.getMostOften = function(){
let data = this
let obj = {}
for (let i = 0; i < data.length; i++) {
if (obj[data[i]]) {
obj[data[i]]++
} else {
obj[data[i]] = 1
}
}
let max = 0
let maxStr = ''
for (let it in obj) {
if (obj[it] > max) {
max = obj[it]
maxStr = it
}
}
return maxStr
}
十、instanceOf 有什么作用??jī)?nèi)部邏輯是如何實(shí)現(xiàn)的?
instanceOf
用來(lái)判斷一個(gè)對(duì)象是是否是一個(gè)構(gòu)造函數(shù)的實(shí)例。內(nèi)部是通過(guò)判斷instance.__proto__ === Object.prototype
是否相同來(lái)實(shí)現(xiàn)的,假如當(dāng)前不相等,就會(huì)根據(jù)原型鏈往上找,instance.__proto__.__proto__ === Object.prototype
,直到null
,找到就返回true
,否則為false
。
十一、繼承有什么作用?
繼承可以復(fù)用父類的屬性和方法,無(wú)需重寫(xiě),同時(shí)可以在子類上進(jìn)行擴(kuò)展。繼承既可減少代碼又易于擴(kuò)展。
十二、下面兩種寫(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);
第一種寫(xiě)法,每次new
出來(lái)的實(shí)例,都會(huì)產(chǎn)生新的屬性和方法,占用過(guò)多不必要的內(nèi)存。第二種寫(xiě)法,每次new
出來(lái)的實(shí)例,會(huì)產(chǎn)生新的屬性,但方法都在共有的屬性原型鏈(prototype)上,節(jié)省內(nèi)存。
十三、Object.create 有什么作用?兼容性如何?
Object.create()
方法會(huì)使用指定的原型對(duì)象及其屬性去創(chuàng)建一個(gè)新的對(duì)象,實(shí)現(xiàn)類式繼承。如:
Student.prototype = Object.create(Person.prototype);
十四、hasOwnProperty 有什么作用? 如何使用?
hasOwnProperty()
方法會(huì)返回一個(gè)布爾值,指示對(duì)象自身屬性中是否具有指定的屬性(是自身屬性,不包含prototyep
)。
function Person(){
this.name = 'jack'
}
Person.prototype.commonName = 'Person'
var a = new Person()
console.log(a.hasOwnProperty('name'))
十五、如下代碼中 call 的作用是什么?
function Person(name, sex){
this.name = name;
this.sex = sex;
}
function Male(name, sex, age){
Person.call(this, name, sex); //這里的 call 有什么作用
this.age = age;
}
這里的call
把this
指向當(dāng)前的Male
,實(shí)現(xiàn)繼承Person
。
十六、補(bǔ)全代碼,實(shí)現(xiàn)繼承
function Person(name, sex) {
// todo ...
this.name = name
this.sex = sex
}
Person.prototype.getName = function () {
// todo ...
return this.name
};
function Male(name, sex, age) {
//todo ...
Person.call(this, name, sex)
this.age = age
}
Male.prototype = Object.create(Person.prototype)
Male.prototype.constructor = Male
//todo ...
Male.prototype.getAge = function () {
//todo ...
return this.age
};
Male.prototype.printName = function(){
console.log('My name is ' + this.getName() + '.')
}
var ruoyu = new Male('若愚', '男', 27);
ruoyu.printName();