問題1: apply沉眶、call 、bind有什么作用杉适,什么區(qū)別
apply谎倔、call、bind都作用本質都是改變函數(shù)的執(zhí)行環(huán)境上下文猿推,即this指向片习。區(qū)別是apply的參數(shù)是傳入數(shù)組或類數(shù)組,而call的參數(shù)是若干個指定參數(shù)彤守。bind毯侦,返回一個新函數(shù),并且使函數(shù)內(nèi)部的this為傳入的第一個參數(shù)具垫。
var fn3 = obj1.fn.bind(obj1);
fn3();//bind侈离,返回一個新函數(shù),并且使函數(shù)內(nèi)部的this為傳入的第一個參數(shù)
fn.call(context, param1, param2...)
fn.apply(context, paramArray)
問題2: 以下代碼輸出什么?
var john = {
firstName: "John"
}
function func() {
alert(this.firstName + ": hi!")
}
john.sayHi = func
john.sayHi()
//輸出john:"hi!"
問題3: 下面代碼輸出什么筝蚕,為什么
func()
function func() {
alert(this)
}
//輸出[object Window]卦碾,直接調用函數(shù),輸出全局對象object Window
問題4:下面代碼輸出什么
document.addEventListener('click', function(e){
console.log(this);
setTimeout(function(){
console.log(this);
}, 200);
}, false);
//輸出document(低版本IE有bug起宽,指向了window) setTimeout指向 Window
問題5:下面代碼輸出什么洲胖,why
var john = {
firstName: "John"
}
function func() {
alert( this.firstName )
}
func.call(john)
// join call方法將func方法的this指定為john
問題6: 以下代碼有什么問題,如何修改
var module= {
bind: function(){
$btn.on('click', function(){
console.log(this) //this指什么
this.showMsg();
})
},
showMsg: function(){
console.log('饑人谷');
}
}
//this指向$btn,將this用變量保存起來可避免歧義
var module= {
bind: function(){
$btn.on('click', function(){
var _this=this;
console.log(this) //this指document
_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();
Person是p的原型藻糖,p是Person的實例淹冰,Person的prototype定義了sayName 方法,p根據(jù)proto原型鏈查找sayName方法巨柒,Person的prototype方法上constructor指向了Person的地址
問題8: 上例中樱拴,對對象 p可以這樣調用 p.toString()柠衍。toString是哪里來的? 畫出原型圖?并解釋什么是原型鏈。
- 通過原型鏈找到object里面的toString()
- p.proto==>proto:Object==>toString
- 我們通過函數(shù)定義了類晶乔,類(函數(shù))自動獲得屬性prototype
- 每個類的實例都會有一個內(nèi)部屬性proto珍坊,指向類的prototype屬性
問題9:對String做擴展,實現(xiàn)如下方式獲取字符串中頻率最高的字符
var str = 'ahbbccdeddddfg';
var ch = str.getMostOften();
String.prototype.getMostOften = function() {
var obj = {},tmp = [0];
for (var i = 0; i < this.length; i++) {
if(obj[this[i]]){
obj[this[i]]++
}
else if(!obj[this[i]]){
obj[this[i]]=1;
}
}
for(var key in obj){
if(obj[key]>tmp[0]){
tmp[0]=obj[key];
tmp.key=key;
}
}
return('出現(xiàn)最多的是:'+tmp.key+' num: '+tmp);
};
var str = 'ahbbccdeddddfg';
var ch = str.getMostOften();
console.log(ch); //d , 因為d 出現(xiàn)了5次
問題10: instanceOf有什么作用瘪弓?內(nèi)部邏輯是如何實現(xiàn)的垫蛆?
function _instanceOf(obj, fn) {
let protoOfObj = obj.__proto__;
do{
if( protoOfObj == fn.prototype ) {
return true;
} else {
protoOfObj = protoOfObj.__proto__;
}
}while(protoOfObj)
return false;
}
//instanceof運算符用來測試一個對象在其原型鏈中是否存在一個構造函數(shù)的 prototype 屬性禽最。
繼承相關問題
問題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);
- 方法一使用構造函數(shù),復用性不好仅叫,不同的對象需要重復添加屬性與方法帜篇。
- 方法二使用原型鏈,給構造函數(shù)原型添加方法诫咱,減少重復笙隙。
問題13: Object.create 有什么作用?兼容性如何坎缭?
Object.create() 方法使用指定的原型對象和其屬性創(chuàng)建了一個新的對象竟痰。 ES 5.1 才定義的方法,IE9以上開始兼容掏呼。
問題14: hasOwnProperty有什么作用坏快? 如何使用?
hasOwnPerperty是Object.prototype的一個方法憎夷,可以判斷一個對象是否包含自定義屬性而不是原型鏈上的屬性莽鸿,hasOwnProperty是JavaScript中唯一一個處理屬性但是不查找原型鏈的函數(shù)
function Person(name, sex){
this.name = name;
this.sex = sex;
}
Person.prototype.printName = function(){
console.log(this.name);
};
function Male(name, sex, age){
Person.call(this, name, sex);
this.age = age;
}
inherit(Person, Male);
var m = new Male('Byron', 'm', 26);
m.hasOwnProperty('name'); // true
m.hasOwnProperty('printName'); // false
問題15:如下代碼中call的作用是什么?
function Person(name, sex){
this.name = name;
this.sex = sex;
}
function Male(name, sex, age){
Person.call(this, name, sex); //修改this指向的對象,借用Person的name和sex屬性
this.age = age;
}
問題16: 補全代碼拾给,實現(xiàn)繼承
function Person(name, sex){
this.name=name
this.sex=sex
}
Person.prototype.getName = function(){
console.log('名字是:'+this.name)
};
function Male(name, sex, age){
Person.call(this, name, sex);
this.age=age
}
Male.prototype= Object.create(Person.prototype);
Male.prototype.getAge = function(){
console.log('名字是:'+this.age)
};
var ruoyu = new Male('若愚', '男', 27);
ruoyu.getName();