this相關問題
問題1: apply秀撇、call 但汞、bind有什么作用免猾,什么區(qū)別
call() 方法調用一個函數(shù), 其具有一個指定的this值和分別地提供的參數(shù)(參數(shù)的列表)。apply()和call() 方法類似眉反,只有一個區(qū)別,就是call()方法接受的是若干個參數(shù)的列表穆役,而apply()方法接受的是一個包含多個參數(shù)的數(shù)組寸五。
bind()方法創(chuàng)建一個新的函數(shù),當被調用時耿币,將其this關鍵字設置為提供的值梳杏,在調用新函數(shù)時,在任何提供之前提供一個給定的參數(shù)序列淹接。
問題2: 以下代碼輸出什么?
var john = {
firstName: "John"
}
function func() {
alert(this.firstName + ": hi!")
}
john.sayHi = func
john.sayHi() // John: hi!
問題3: 以下代碼輸出什么?
func()
function func() {
alert(this) // window
}
問題4: 以下代碼輸出什么?
document.addEventListener('click', function(e){
console.log(this); // document
setTimeout(function(){
console.log(this); // window
}, 200);
}, false);
問題5: 以下代碼輸出什么?
var john = {
firstName: "John"
}
function func() {
alert( this.firstName )
}
func.call(john) // John
問題6: 以下代碼輸出什么?
var module= {
bind: function(){
$btn.on('click', function(){
console.log(this) // $btn
this.showMsg();
})
},
showMsg: function(){
console.log('饑人谷');
}
}
原型鏈相關問題
問題7:有如下代碼十性,解釋Person、 prototype塑悼、proto劲适、p、constructor之間的關聯(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();
問題8: 上例中霞势,對對象 p可以這樣調用 p.toString()。toString是哪里來的? 畫出原型圖?并解釋什么是原型鏈斑鸦。
p.proto指向Person.prototype愕贡,Person.prototype.proto指向Object.prototype,在Object.prototype中存在toString()方法巷屿,因此p可以調用toString()方法固以。
原型鏈的基本思想是利用原型讓一個引用類型繼承另一個引用類型的屬性和方法。每一個構造函數(shù)都有一個原型對象嘱巾,原型對象都包含一個指向構造函數(shù)的指針憨琳,而實例都包含一個指向原型對象的內部指針,那么當原型對象又等于另一個類型的實例浓冒,如此層層遞進栽渴,就構成了實例和原型的鏈條,就是原型鏈稳懒。
問題9:對String做擴展闲擦,實現(xiàn)如下方式獲取字符串中頻率最高的字符
String.prototype.getMostOften = function(){
var obj = {};
var maxValue = 0;
var maxKey = '';
for (var i = 0; i < this.length; i++) {
if (obj[this[i]]) {
obj[this[i]] ++
} else {
obj[this[i]] = 1
}
}
for(var key in obj){
if (obj[key] > maxValue) {
maxValue = obj[key];
maxKey = key;
}
}
return maxKey + "慢味,因為" + maxKey + "出現(xiàn)了" + maxValue + "次"
}
var str = 'ahbbccdeddddfg';
var ch = str.getMostOften();
console.log(ch); //d , 因為d 出現(xiàn)了5次
問題10: instanceOf有什么作用?內部邏輯是如何實現(xiàn)的墅冷?
// 語法
object instanceof constructor
instanceof 運算符用來檢測 constructor.prototype 是否存在于參數(shù) object 的原型鏈上纯路。MDN參考
繼承相關問題
問題11:繼承有什么作用?
子類可以繼承父類的屬性和方法,因此只需定義子類特有的屬性即可寞忿,可以簡化子類的描述驰唬。父類提供的方法,子類通過繼承可以直接使用腔彰,可提高代碼的重用性叫编。
問題12: 下面兩種寫法有什么區(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);
方法1中,printName()方法在構造函數(shù)中霹抛,每次實例化搓逾,printName()方法都需要單獨占用內存;
方法2中杯拐,printName()方法寫在原型對象中霞篡,當Person實例化后,調用printName()時會在原型鏈上找到該方法端逼,這些方法都指向Person.prototype對象中朗兵,減少了內存的占用。
問題13: Object.create 有什么作用顶滩?兼容性如何余掖?
Object.create() 方法使用指定的原型對象和其屬性創(chuàng)建了一個新的對象。兼容性參考MDN
問題14: hasOwnProperty有什么作用礁鲁? 如何使用浊吏?
hasOwnProperty() 方法會返回一個布爾值,指示對象是否具有指定的屬性作為自身(不繼承)屬性救氯。該方法會忽略掉那些從原型鏈上繼承到的屬性
obj.hasOwnProperty(prop) //prop代表要檢測的屬性
問題15:如下代碼中call的作用是什么?
function Person(name, sex){
this.name = name;
this.sex = sex;
}
function Male(name, sex, age){
Person.call(this, name, sex); //使Person內部this對象指向Male的實例對象找田,使Male的實例對象可以使用Person的屬性。
this.age = age;
}
//
問題16: 補全代碼着憨,實現(xiàn)繼承
function Person(name, sex){
this.name = name;
this.sex = sex;
}
Person.prototype.getName = function(){
return this.name
};
function Male(name, sex, age){
Person.call(this,name,sex);
this.age = age;
}
Male.prototype = new Person();
Male.prototype.getAge = function(){
return this.age
};
var ruoyu = new Male('若愚', '男', 27);
ruoyu.getName();