this、原型鏈、繼承

b#this 相關(guān)問題

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

  • apply:apply() 方法調(diào)用一個函數(shù), 其具有一個指定的this值,以及作為一個數(shù)組(或類似數(shù)組的對象)提供的參數(shù)。
function fn(a,b){
  console.log(a,b)
}
fn.apply(this,[1,2])
  • call:call() 方法調(diào)用一個函數(shù), 其具有一個指定的this值和分別地提供的參數(shù)(參數(shù)的列表)。
function fn(a,b){
  console.log(a,b)
}
fn.call(this,1,2)

call與apply的區(qū)別:call()方法接受的是若干個參數(shù)的列表,而apply()方法接受的是一個包含多個參數(shù)的數(shù)組。

  • bind:返回一個新函數(shù),并且使函數(shù)內(nèi)部的this為傳入的第一個參數(shù)
function fn(a,b){
  console.log(this)
}
var fn2 = obj.fn.bind(obj)
fn2()//obj

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

var john = { 
  firstName: "John" 
}
function func() { 
  alert(this.firstName + ": hi!")//this將指向?qū)嵗龑ο蠹磳ο骿ohn
}
john.sayHi = func
john.sayHi()
//輸出John:hi!

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

func() //輸出window
function func() { 
  alert(this)//在函數(shù)被直接調(diào)用時this綁定到全局對象(window 就是該全局對象)
}

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

document.addEventListener('click', function(e){
    console.log(this);//輸出document;在事件處理程序中this代表事件源DOM對象(document)
    setTimeout(function(){
        console.log(this);//輸出window;setTimeout()相關(guān)文檔中說明,this為全局對象window.
    }, 200);
}, false);

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

var john = { 
  firstName: "John" 
}

function func() { 
  alert( this.firstName )
}
func.call(john)//輸出John;call方法是將第一個參數(shù)作為this值傳入調(diào)用call的對象中。

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

var module= {
  bind: function(){
    $btn.on('click', function(){
      console.log(this) //this指什么=> $btn
      this.showMsg();//此處的this指向$btn,$btn無showMsg()方法
    })
  },
  
  showMsg: function(){
    console.log('饑人谷');
  }
}
///////////////////////////////////
var module= {
  bind: function(){
    var self = this
    $btn.on('click', function(){
      console.log(this) 
      self.showMsg();
    })
  },
  
  showMsg: function(){
    console.log('饑人谷');
  }
}

原型鏈相關(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();
  • Person:一個構(gòu)造函數(shù),有name屬性,prototype.sayName方法。
  • prototype:Person函數(shù)的原型,有sayName方法。
  • __proto__:p對象的原型,指向Person.prototype。
  • p:調(diào)用構(gòu)造函數(shù)Person生成的實例對象,p 等價于 {name:若愚}。
  • constructor:constructor的值為Person函數(shù)function Person(name){ this.name = name; }。

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

toString()是存在于Person.prototype.__proto__中的方法。
原型圖:


原型鏈:js中每一個對象都帶有__proto__屬性,該屬性指向構(gòu)造該對象的函數(shù)的prototype屬性。遞歸訪問__proto__的終點是null值,這一連串的__proto__就是原型鏈。

問題9:對String做擴展,實現(xiàn)如下方式獲取字符串中頻率最高的字符

var str = 'ahbbccdeddddfg';
var ch = str.getMostOften();
console.log(ch); //d , 因為d 出現(xiàn)了5次
var str = 'ahbbccdeddddfg';
String.prototype.getMostOften = function () {
  let strArr = {}
  let maxString = ""
  let strIdx = 0
  for (let i = 0; i < this.length; i++) {
    if (!strArr[this[i]]) {
      strArr[this[i]] = 1
    } else {
      strArr[this[i]]++
    }
  }
  for (let key in strArr) {
    if (strArr[key] > maxString) {
      maxString = strArr[key]
    }
  }
  return maxString
}
var ch = str.getMostOften();
console.log(ch); //d , 因為d 出現(xiàn)了5次

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

instanceof 運算符用來測試一個對象在其原型鏈中是否存在一個構(gòu)造函數(shù)的 prototype 屬性。
語法:

obj1 instanceof obj2

內(nèi)部邏輯:instanceof 運算符會對比obj1原型鏈中所有的__proto__是否至少有一個等于obj2的prototype。
如果obj1的原型鏈中某一個__proto__與obj2prototype相等返回true,否則返回false。

繼承相關(guān)問題

問題11:繼承有什么作用?

繼承是指一個對象直接使用另一對象的屬性和方法??梢怨?jié)省代碼,提高效率。

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

方法2比方法1更節(jié)省內(nèi)存。
方法1每次創(chuàng)建對象都會生成一次printName方法。
方法2每個創(chuàng)建的對象都會調(diào)用同一內(nèi)存下的printName方法。

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

Object.create() 方法會使用指定的原型對象及其屬性去創(chuàng)建一個新的對象,并將添加新屬性后的新對象返回在指定原型對象上。
語法:

function fn1(){
  //todo
}
fn1.prototype.meth1 = function(){
  console.log(1)
}
function fn2(){
  //todo
}
fn2.prototype = Object.create(fn1.prototype)
fn2.prototype.constructor =fn2
var fn3 = new fn2
console.log(fn3 instanceof fn2) //true
console.log(fn3 instanceof fn1) //true
fn2.meth1()//1

兼容性,可查看ES5的兼容性,大體如下:

  • Opera 11.60
  • Internet Explorer 9
  • Firefox 4
  • Safari 5.1**
  • Chrome 13

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

hasOwnPerpertyObject.prototype的一個方法,可以判斷一個對象是否包含自定義屬性而不是原型鏈上的屬性,hasOwnPropertyJavaScript中唯一一個處理屬性但是不查找原型鏈的函數(shù)。
語法:

o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop');             // 返回 true
o.hasOwnProperty('toString');         // 返回 false
o.hasOwnProperty('hasOwnProperty');   // 返回 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;
}

call把對象Male作為this值傳入Person函數(shù),使Male對象繼承Person的屬性。

問題16: 補全代碼,實現(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();
//diff
function Person(name, sex){
    // todo ...
}

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

function Male(name, sex, age){
   Person.call(this,name, sex)//+
  //todo ...
}

//todo ...
Male.prototype = Object.create(Person.prototype);//+
Male.prototype.getAge = function(){
    //todo ...
};

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

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

  • this 相關(guān)問題 問題1: apply、call 有什么作用,什么區(qū)別 Javascript的每個Functio...
    Maggie_77閱讀 672評論 0 0
  • 1. apply、call 、bind有什么作用,什么區(qū)別? call ,apply的作用:調(diào)用一個函數(shù),傳入函數(shù)...
    Rising_suns閱讀 436評論 0 0
  • 一、this 相關(guān)問題 知乎上關(guān)于this的解答 this 的值到底是什么?一次說清楚 this 的工作原理在js...
    66dong66閱讀 646評論 0 0
  • 1. this2.原型鏈-instanceof實現(xiàn)3.繼承的實現(xiàn) ** this 相關(guān)問題 ** ** 1、 ap...
    饑人谷_阿靖閱讀 413評論 0 0
  • 1: apply、call 、bind有什么作用,什么區(qū)別 call 和 apply 都是為了改變某個函數(shù)運行時的...
    高進(jìn)哥哥閱讀 358評論 0 0

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