美文网首页
this_原型链_继承

this_原型链_继承

作者: jamesXiao_ | 来源:发表于2017-07-03 00:32 被阅读0次

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

  • fn.apply( obj,[])
    将fn函数里的this指向改为obj,然后后面的数组当成参数传入函数
  • fn.apply( obj, arr1, arr2, ...)
    将fn函数里的this指向改为obj,后面的的arr1,arr2为传入函数的参数,与apply的区别为,apply传入的是数组,call传入的只是几个参数
  • fn().bind( obj )
    将fn函数里的this指向改为obj

以下代码输出什么?

var john = { 
  firstName: "John" 
}
function func() { 
  alert(this.firstName + ": hi!")
}
john.sayHi = func
john.sayHi()
输出: John: hi!
因为函数func赋给sayHi,所以tihis指向的是john

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

func() 
function func() { 
  alert(this)
}
因为this指向的window

下面代码输出什么

document.addEventListener('click', function(e){
    console.log(this);
    setTimeout(function(){
        console.log(this);
    }, 200);
}, false);
输出 document   window
因为会先输出第一个this,第一个this指向document, 然后是setTimeout, 这个this指向的window

下面代码输出什么,why

var john = { 
  firstName: "John" 
}

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

输出John   因为 func.call(john) 是执行func函数,将this指向的john 所以函数的this.firstName 是 john.firstName,所以输出John

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

var module= {
  bind: function(){
    $btn.on('click', function(){
      console.log(this) //this指什么
      this.showMsg();
    })
  },
  
  showMsg: function(){
    console.log('饥人谷');
  }
}
第一个this是指$btn,$btn没有this.showMsg()方法
修改为:
var module= {
  bind: function(){
    $btn.on('click', function(){
      console.log(this) //this指什么
      this.showMsg();
    }).bind(this)
  },
  
  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();

Person 是构造函数  Person.prototype指向 prototype   Person是prototype的constructor    p是Person的实例     p.__proto__ 指向 prototype    p.sayName()会首先查找自身有这个方法没.没有会找p,__proto__指向的prototype 是否有这个方法  p.__proto__ == Person.prototype

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

QQ图片20170702230159.png

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

var str = 'ahbbccdeddddfg';
var ch = str.getMostOften();
console.log(ch); //d , 因为d 出现了5次
var str = 'ahbbccdeddddfg';
String.prototype.getMostOften = function () {
  var strArray = {};
  var maxString = "";
  var maxIndex = 0;
  
  for (let i = 0; i < this.length; i++) {
    
     if (!strArray[this[i]]) {
       strArray[this[i]] = 1;
     } else {
       strArray[this[i]]++;
     }
  }
  
  for (var key in strArray) {
    if (maxIndex < strArray[key]) {
      maxIndex = strArray[key];
      maxString = key;
    }
  }
  
  return maxString;
};

var ch = str.getMostOften();

console.log(ch); //d , 因为d 出现了5次

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

  • instanceof运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性。即判断是不是一个对象的实例,是返回 true,不是返回false。
var arr = [1,2,3],
    obj = {name:'xuguojun'};

arr instanceof Array     //true
    //内部逻辑为看实例的 __proto__ 是否等于构造函数的 prototype 。    
arr.__proto__ === Array.prototype //true
    //如果为 true ,则返回结果 true

arr instanceof Object     //true
    //内部逻辑为看实例的 __proto__ 是否为构造函数的 prototype 原型   
arr.__proto__ === Object.prototype //false
    //如果为 false ,则继续看下一层的 __proto__
arr.__proto__.__proto__ === Object.prototype //true
    //如果为 true ,则返回结果 true

obj instanceof Array     //false
    //内部逻辑为看实例的 __proto__ 是否为构造函数的 prototype 原型
obj.__proto__ === Array.prototype //false
    //如果为 false ,则继续看下一层的 __proto__
obj.__proto__.__proto__ === Array.prototype //false
    //如果到最深一层的 __proto__ (最深一层为null)比较还是不相等,则返回 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);
方法一把属性和方法都放在类里面,当new的时候就赋予一次全部的属性和方法,比较占内存,而方法二把部分写在原型里,让对象继承,节省内存

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

  • 作用:使用指定的原型对象和其属性创建一个子对象
    方法:Son.prototype = object.create( Father.prototype );
    Son.prototype.constructor = Son
    不支持ie8以下浏览器

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

  • hasOwnProperty() 方法会返回一个布尔值,指示对象是否具有指定的属性作为自身(不继承)属性。
o = new Object();
o.prop = 'exists';

function changeO() {
  o.newprop = o.prop;
  delete o.prop;
}

o.hasOwnProperty('prop');   // 返回 true
changeO();
o.hasOwnProperty('prop');   // 返回 false

如下代码中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的属性

补全代码,实现继承

function Person(name, sex){
    this.name = name;
    this.sex = sex;
}

Person.prototype.getName = function(){
    return this.name;
};    

function Male(name, sex, age){
    // 继承父类的属性
   Person.call(this, name, sex);
  /*
   或者
   (
   Person.bind(this)(name, sex)
   )
  */
   this.age = age;
}

// 继承父类的prototype
Male.prototype = Object.create(Person.prototype);
Male.prototype.constructor = Male;

/*
或者
(
// 继承父类的prototype
function parent() {};
parent.prototype = Person.prototype;
Male.prototype = new parent();
Male.prototype.constructor = Male;
)
*/

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

Male.prototype.printName = function () {
  console.log(this.getName());
};
var ruoyu = new Male('若愚', '男', 27);
ruoyu.printName();

相关文章

  • this_原型链_继承

    1.apply、call 、bind有什么作用,什么区别 bind:返回一个新函数,并且使函数内部的this为传入...

  • this_原型链_继承

    this 相关问题 1: apply、call 、bind有什么作用,什么区别? 在JS中,这三者都是用来改变函数...

  • this_原型链_继承

    this 相关问题 1.apply、call 、bind有什么作用,什么区别 fn.apply(context ,...

  • this_原型链_继承

    this 相关问题 问题1: apply、call 、bind有什么作用,什么区别 在 javascript 中,...

  • this_原型链_继承

    问题1: apply、call 、bind有什么作用,什么区别 改变函数执行时的上下文,具体的就是指绑定this指...

  • this_原型链_继承

    1: apply、call 、bind有什么作用,什么区别 作用:改变函数体内 this 的指向 区别:对于 ap...

  • this_原型链_继承

    1.apply、call 、bind有什么作用,什么区别 call apply的作用:调用一个函数,传入函数执行上...

  • this_原型链_继承

    this 相关 1. apply、call 、bind有什么作用,什么区别 apply、call、bind可以改变...

  • this_原型链_继承

    问题1: apply、call 、bind有什么作用,什么区别? apply和call call apply,调用...

  • this_原型链_继承

    this 相关问题 问题1: apply、call 、bind有什么作用,什么区别? apply,call,bin...

网友评论

      本文标题:this_原型链_继承

      本文链接:https://www.haomeiwen.com/subject/shcocxtx.html