對(duì)象相關(guān)2

問題1: apply、call 、bind有什么作用,什么區(qū)別

bind,返回一個(gè)函數(shù),傳入一個(gè)參數(shù)作為函數(shù)的this

var obj = {
  a:1
}
function func(){
  console.log(this)  //this指向window

  setTimeout(function(){
    console.log(this)  //this指向obj,輸出{a: 1}
  }.bind(obj),200)

  setTimeout(function(){
    console.log(this)  //this指向window
  },300)
}

call,調(diào)用一個(gè)函數(shù),傳入第一個(gè)參數(shù)作為函數(shù)的this,隨后的參數(shù)作為argumenst傳入

function func(a,b){
  console.log(this.value + a + b)
}

var obj = {
  value: 1
}

直接調(diào)用func(1,2)找不到this.value,返回NaN
調(diào)用func.call(obj,1,2)則輸出4

apply,調(diào)用一個(gè)函數(shù),傳入第一個(gè)參數(shù)作為函數(shù)的this,第二個(gè)參數(shù)為一個(gè)數(shù)組,數(shù)組內(nèi)為要傳入的參數(shù)

function func(a,b){
  console.log(this.value + a + b)
}

var obj = {
  value: 1
}

同理,調(diào)用func.apply(obj,[1,2]),輸出4

問題2: 以下代碼輸出什么?

var john = { 
  firstName: "John" 
}
function func() { 
  alert(this.firstName + ": hi!")
}
john.sayHi = func
john.sayHi()

結(jié)果:執(zhí)行elert,在頁面上彈出John: hi!,返回undefined

問題3: 下面代碼輸出什么,為什么

func() 
function func() { 
  alert(this)
}

結(jié)果:執(zhí)行alert,彈出[object window],聲明前置所以函數(shù)會(huì)執(zhí)行,this指向window

問題4:下面代碼輸出什么

document.addEventListener('click', function(e){
    console.log(this);
    setTimeout(function(){
        console.log(this);
    }, 200);
}, false);

結(jié)果:輸出document對(duì)象,隨后輸出widow對(duì)象,監(jiān)聽函數(shù)中的this指向被監(jiān)聽對(duì)象,而setTimeout中的函數(shù)指向window

問題5:下面代碼輸出什么,why

var john = { 
  firstName: "John" 
}

function func() { 
  alert( this.firstName )
}
func.call(john)

結(jié)果:執(zhí)行alert,彈出顯示John,因?yàn)槭褂胏all,將對(duì)象john作為func的的this

問題6: 以下代碼有什么問題,如何修改

var module= {
  bind: function(){
    $btn.on('click', function(){
      console.log(this) //this指什么
      this.showMsg();
    })
  },
  
  showMsg: function(){
    console.log('饑人谷');
  }
}

監(jiān)聽函數(shù)中的this指向$btn,無法調(diào)用showMsg()函數(shù),在函數(shù)外面將聲明一個(gè)變量將this賦值給他

修改:

bind: function(){
  var _this = this
  $btn.on('click', function(){
    console.log(this)
    this.showMsg();
  })
}

原型鏈相關(guā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();

關(guān)聯(lián):

Person.prototype === p.__proto__

Person === Person.prototype.constructor

問題8: 上例中,對(duì)對(duì)象 p可以這樣調(diào)用 p.toString()。toString是哪里來的? 畫出原型圖?并解釋什么是原型鏈。

Person.peototype.proto中有toString方法

原型圖.png

如圖,實(shí)例中的proto指向原型中的prototype,原型prototype中的proto又指向它的構(gòu)造函數(shù)中的prototype,以此類推,直到最初始的構(gòu)造函數(shù)。

問題9:對(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(){
  var dict = {}
  var max = 0
  var k
  
  for(var i = 0; i < this.length; i++){
    if(dict[this[i]]){dict[this[i]]++}
    else {dict[this[i]] = 1}
  }

  for(var key in dict){
    if(dict[key] > max){
      max = dict[key]
      k = key
    }
  }

  return k
}

問題10: instanceOf有什么作用??jī)?nèi)部邏輯是如何實(shí)現(xiàn)的?

用來判斷一個(gè)對(duì)象在其原型鏈中是否存在一個(gè)構(gòu)造函數(shù)的prototype屬性
可以判斷一個(gè)對(duì)象是否是另一個(gè)對(duì)象的實(shí)例

function C(){}
function D(){}  //構(gòu)造兩個(gè)空的構(gòu)造函數(shù)

var o = new C()  //通過構(gòu)造函數(shù)C() new 一個(gè)實(shí)例o ,

o instanceof C  //true
o instanceof D  //false

邏輯:
if(o.__proto__ === C.prototype){ rerurn trun}
else { return false}

繼承相關(guān)問題

問題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);

創(chuàng)建的實(shí)例不同,
方法1中的方法和屬性在一起;每次new一個(gè)實(shí)例會(huì)聲明一個(gè)printName函數(shù)
方法2中的方法在prototype中;每次new一個(gè)實(shí)例只會(huì)在調(diào)用printName是創(chuàng)建函數(shù)

問題13: Object.create 有什么作用?兼容性如何?

使用指定的原型對(duì)象及其屬性創(chuàng)建一個(gè)新對(duì)象,可以實(shí)現(xiàn)繼承

function C(){}  //創(chuàng)建一個(gè)空的構(gòu)造函數(shù)
C.prototype.func1 = function(){
  console.log(1)
}

function D(){}
D.prototype = Object.create(C.prototype)

var o = new D()
o.func1()  //D繼承了C的方法,所以會(huì)輸出11

此為ES5方法,IE9及以上可用,其他主流瀏覽器都兼容

問題14: hasOwnProperty有什么作用? 如何使用?

判斷某個(gè)方法或?qū)傩允菍儆谧约哼€是從父類處繼承而來的

使用: (參考最后一題中的代碼)

ruoyu.hasOwnProperty('age')  //true
ruoyu.hasOwnProperty('name')  //false
ruoyu.hasOwnProperty('printName')  //true
Male.prototype.hasOwnProperty('getName')  //false

問題15:如下代碼中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;
}

讓Male繼承Person的屬性
通過call,調(diào)用Person函數(shù),并將Male的this傳入,并且傳入name和sex兩個(gè)參數(shù)

問題16: 補(bǔ)全代碼,實(shí)現(xiàn)繼承

function Person(name, sex){
    // todo ...
}

Person.prototype.getName = function(){
    // todo ...
};    

function Male(name, sex, age){
   //todo ...
}

//todo ...
Male.prototype.getAge = function(){
    //todo ...
};

var ruoyu = new Male('若愚', '男', 27);
ruoyu.printName();
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,age)
  this.age = age
}

var temp = Object.create(Person.prototype)
temp.constructor = Male
Male.prototype = temp  //可以將這個(gè)方法寫成一個(gè)函數(shù)封裝起來

Person.prototype.printName = function(){
  console.log(this.name)
}

Male.prototype.getAge = function(){
  console.log(this.age)
}

var ruoyu = new Male('若愚', '男', 27)
ruoyu.printName()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容