饥人谷技术博客

this_原型链_继承

2017-08-27  本文已影响0人  liushaung

this

apply、call 、bind有什么作用,什么区别?

以下代码输出什么?

var john = { 
  firstName: "John" 
}
function func() { 
  alert(this.firstName + ": hi!")
}
john.sayHi = func
john.sayHi()  // John: hi!
// this为执行上下文,当一个函数作为对象的方法调用时,this默认指向这个对象

下面代码输出什么,为什么?

func() // [object Window]
function func() { 
  alert(this)
}
// 当直接调用一个函数时,相当于执行window.func()所以this指向window对象

下面代码输出什么?

document.addEventListener('click', function(e){
    console.log(this);
    setTimeout(function(){
        console.log(this);
    }, 200);
}, false);
// 输出:#document  、     Window{}
// 第一个:事件中的this默认指向绑定这个事件的对象。
// 第二个:setTimeout的执行函数中的this默认指向window对象

下面代码输出什么,why?

var john = { 
  firstName: "John" 
}

function func() { 
  alert( this.firstName )
}
func.call(john) // Jojn
// call方法用于在函数运行时指定this指,因此执行以上代码时this指向john对象

以下代码有什么问题,如何修改

var module= {
  bind: function(){
    $btn.on('click', function(){
      console.log(this) //this指$btn对象
      this.showMsg();
    })
  },
  
  showMsg: function(){
    console.log('饥人谷');
  }
}
// 在事件处理函数中this指向$btn,而$btn没有showMsg()方法
// 修改:将外部this保存到一个变量中
var module= {
  bind: function(){
    var self = this
    $btn.on('click', function(){
      console.log(this)
      self.showMsg();
    })
  },
  
  showMsg: function(){
    console.log('饥人谷');
  }
}

原型链

有如下代码,解释Person、 prototype、proto、p、constructor之间的关联。

function Person(name){
    this.name = name;
}
Person.prototype.sayName = function(){
    console.log('My name is :' + this.name);
}
var p = new Person("若愚")
p.sayName();  // 'My name is: 若愚'
// Person是一个函数,它拥有一个prototype属性,这个属性指向一个对象,而这个对象拥有一个
constructor属性,这个属性指向Person函数
// 当Person作为构造函数调用时,会先初始化一个对象,将this指向这个对象,并将它的__proto__属性指
向Person.prototype,因此p.name === "若愚",p.sayName() === Person.prototype.sayName()

上例中,对对象 p可以这样调用 p.toString()。toString是哪里来的? 画出原型图?并解释什么是原型链。

对String做扩展,实现如下方式获取字符串中频率最高的字符

String.prototype.getMostOften = function() {
  var  obj = {}
  var strArr = this.split('')
  strArr.forEach(function(i) {
    if (obj[i]) {
      obj[i]++
    } else {
      obj[i] = 1
    }
  })

  var max = 0, maxStr
  for (var x in obj) {
    if (obj[x] > max) {
      maxStr = x
      max = obj[x]
    }
  }

  return maxStr
}
var str = 'ahbbccdeddddfg';
var ch = str.getMostOften();
console.log(ch); //d

instanceOf有什么作用?内部逻辑是如何实现的?

继承

继承有什么作用?

下面两种写法有什么区别?

//方法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方法,
使用方法2构造的每个对象调用printName方法时都是调用的Person.prototype.printName方法
方法2大大节省了内存

Object.create 有什么作用?兼容性如何?

hasOwnProperty有什么作用? 如何使用?

如下代码中call的作用是什么?

function Person(name, sex){
    this.name = name;
    this.sex = sex;
}
function Male(name, sex, age){
    Person.call(this, name, sex);    
    //调用Person函数,并把它的this变为Male函数的this,用此方法可实现对Person实例属性的继承
    this.age = age;
}

补全代码,实现继承

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
}

Male.prototype = Object.create(Person.prototype)
Male.prototype.constructor = Male

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

var ruoyu = new Male('若愚', '男', 27);
ruoyu.printName();
上一篇 下一篇

猜你喜欢

热点阅读