this 原型鏈 繼承

一、apply、call 、bind有什么作用,什么區(qū)別?

首先說明,這三者的主要用途都是為了:調(diào)用函數(shù)時(shí),使用你指定的this

  1. apply

調(diào)用一個(gè)函數(shù),為其指定this,參數(shù)以數(shù)組形式傳入。

  1. call

調(diào)用一個(gè)函數(shù),為其指定this,分別提供參數(shù)。

  1. 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)檫@里的sayHijohn調(diào)用的,所以this指向的就是join對(duì)象。

三、下面代碼輸出什么,為什么

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

在瀏覽器運(yùn)行時(shí),thiswindow,因此,代碼運(yùn)行后,會(huì)彈窗顯示window對(duì)象。

四、下面代碼輸出什么

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

第一個(gè)thisdocument,因?yàn)槭录壎ㄗ詣?dòng)把this指向?yàn)閷?duì)應(yīng)元素。第二個(gè)thiswindow,因?yàn)槟涿瘮?shù)的執(zhí)行是在window全局下的。

五、下面代碼輸出什么,why

var john = { 
  firstName: "John" 
}

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

彈窗顯示John,因?yàn)?code>call指定了thisjohn對(duì)象。

六、以下代碼有什么問題,如何修改

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嬖瓉淼?code>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ù)的共有屬性。pPerson的實(shí)例,該實(shí)例有__proto__對(duì)象指向Person.prototypeconstructor表示該實(shí)例的構(gòu)造器,指向Person

p.__proto__.constructor === Person.prototype.constructor === Person

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

toStringp原型鏈上Object構(gòu)造函數(shù)的方法。

Object 示例

九、對(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用來判斷一個(gè)對(duì)象是是否是一個(gè)構(gòu)造函數(shù)的實(shí)例。內(nèi)部是通過判斷instance.__proto__ === Object.prototype是否相同來實(shí)現(xiàn)的,假如當(dāng)前不相等,就會(huì)根據(jù)原型鏈往上找,instance.__proto__.__proto__ === Object.prototype,直到null,找到就返回true,否則為false

十一、繼承有什么作用?

繼承可以復(fù)用父類的屬性和方法,無需重寫,同時(shí)可以在子類上進(jìn)行擴(kuò)展。繼承既可減少代碼又易于擴(kuò)展。

十二、下面兩種寫法有什么區(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);

第一種寫法,每次new出來的實(shí)例,都會(huì)產(chǎn)生新的屬性和方法,占用過多不必要的內(nèi)存。第二種寫法,每次new出來的實(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;
}

這里的callthis指向當(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();
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 1. apply、call 、bind有什么作用,什么區(qū)別? call ,apply的作用:調(diào)用一個(gè)函數(shù),傳入函數(shù)...
    Rising_suns閱讀 403評(píng)論 0 0
  • 一、this 相關(guān)問題 知乎上關(guān)于this的解答 this 的值到底是什么?一次說清楚 this 的工作原理在js...
    66dong66閱讀 568評(píng)論 0 0
  • this 相關(guān)問題 問題1: apply、call 有什么作用,什么區(qū)別 Javascript的每個(gè)Functio...
    Maggie_77閱讀 611評(píng)論 0 0
  • 通過題目來熟悉this,原型鏈,繼承 開胃菜----this 1. apply、call、bind有什么作用,有什...
    JunVincetHuo閱讀 388評(píng)論 0 0
  • apply、call 、bind有什么作用,什么區(qū)別 apply:fn.apply( obj,])將fn函數(shù)里的t...
    邵志遠(yuǎn)閱讀 549評(píng)論 0 0