this,_原型链_,继承

2018-01-07  本文已影响0人  饥人谷_小逸

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

var value = 100
var obj = {
  value:200
}
var obj2 = {
  value:300
}
function sun(a, b){
  console.log(this.value + a + b)
}
sun(1,2)   //103
sun.call(obj, 1,2)  //203
sun.apply(obj2,[1,2])  //303

由此可见call apply,调用一个函数,传入函数执行上下文及参数,第一个参数都是希望设置的this对象,不同之处在于call方法接收参数列表,而apply接收参数数组
fn.call(context, param1, param2...)
fn.apply(context, paramArray)

var obj = {
    x: 81,
};
 
var foo = {
    getX: function() {
        return this.x;
    }
}
 
console.log(foo.getX.bind(obj)());  //81
console.log(foo.getX.call(obj));    //81
console.log(foo.getX.apply(obj));   //81

bind,返回一个新函数,并且使函数内部的this为传入的第一个参数
bind() 最简单的用法是创建一个函数,使这个函数不论怎么调用都有同样的 this 值。

以下代码输出什么?

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

//John: hi!

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

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

//[object Window]
在函数被直接调用时this绑定到全局对象。在浏览器中,window 就是该全局对象。

下面代码输出什么?

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

//#document  在事件处理程序中this代表事件源DOM对象
//window  0.2秒后执行函数this指向全局对象window

下面代码输出什么,why?

var john = { 
  firstName: "John" 
}

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

//John
调用一个函数,传入函数执行上下文及参数(返回值是你调用的方法的返回值)

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

var module= {
  bind: function(){
    $btn.on('click', function(){
      console.log(this) //this指什么
      this.showMsg();
    })
  },
  
  showMsg: function(){
    console.log('饥人谷');
  }
}
//例中的this指向$btn,事件中的this指向发生事件的目标事件
//因此this.showMsg()不存在,会报错

var module= {
  bind: function(){
    let _this = this    //保存最初的this
    $btn.on('click', function(){
      console.log(_this)
      _this.showMsg();
    })
  },

  showMsg: function(){
    console.log('饥人谷');
  }
}
module.bind()

<pre>

有如下代码,解释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();

Person.prototype.__proto__ === Object.prototype;
Person.prototype.constructor == Person

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

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

var str = 'ahbbccdeddddfg';
var ch = str.getMostOften();
console.log(ch); //d , 因为d 出现了5次

代码如下:

//方法一:
String.prototype.getMostOften = function(){
    var obj = {};
    for(var i=0,k;i<this.length;i++){
        k = this[i];
        if(obj[k]){
            obj[k]++
        }else{
            obj[k] = 1
        }
    }

    var max = 0,key;
    for(var k in obj){
        if(obj[k]>max){
           max = obj[k];
           key = k;
        }
     }

    return key;
}
//方法二:
String.prototype.getMostOften = function(){
    var arr = this.split("");
    var result = arr.reduce(function(allLetters,letter){
        if(allLetters[letter]){
            allLetters[letter]++
        }else{
            allLetters[letter] = 1
        }

        return allLetters;          
    },{});
  var max = 0,k;
        for(var key in result){
            if (result[key]>max){
                max = result[key];
                k = key
            }
        }
    return k;
}
var str = 'ahbbccdeddddfg';
var ch = str.getMostOften();
console.log(ch); //d 

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

function instanceOf(obj,fn){
    var oldpro = obj.__proto__;
    while(oldpro){
    if(oldpro === fn.prototype){
        return true;
    }else{
        oldpro = oldpro.__proto__;
    }
    }
    return false;
}

继承相关问题

继承有什么作用?

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

//方法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);

区别:同样都是创建printName方法,方法1的printName方法是在函数Person实例对象里的,方法2是在Person的prototype对象上的。当再创建一个Person实例对象的时候,方法1又将会再创建一个printName方法,占用新的内存,而方法2将一个公用的printName方法写在原型上,当对象要使用该方法只需到原型链里调用就可以了,达到节省内存的效果。

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

image.png
function Person(name, age){
  this.name = name;
  this.age = age;
}
Person.prototype.sayName = function(){
  console.log(this.name);
}
function Male(name, age, sex){
  Person.call(this, name, age);
  this.sex = sex;
}
// Male.prototype = new Person(); //该方法同下,代替不兼容Object.create()的使用场景
Male.prototype = Object.create(Person.prototype);
Male.prototype.constructor = Male;
Male.prototype.sayAge = function(){
    console.log(this.age);
};
var p1 = new Male('hunger', 20, 'nan');
p1.sayName();//hunger
p1.sayAge();//20

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

p1.hasOwnProperty('name');//true
p1.hasOwnProperty('sayName');//false
Male.prototype.hasOwnProperty('sayAge');//true

如下代码中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;
}

补全代码,实现继承

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.constructor = Male;
Male.prototype.getAge = function(){
    console.log(this.name);
};

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

猜你喜欢

热点阅读