关于 this_原型链_继承 相关问题总结

2017-10-14  本文已影响30人  osborne

关于this

1- apply、call 、bind的作用和区别

function a(xx, yy) {     
console.log(xx, yy);     
console.log(this);     
console.log(arguments);
} 
var thisObj = {name:'haha'};

a.apply(null, [5, 55]); //第一个参数不设置,不改变原this指向
a.call(thisObj, 5, 55); //设置第一个参数,该方法里的this改变成thisObj
var b = a.bind(null, 5, 55); //返回一个新函数
b();

这些方法主要的作用是在一些本身不具备某些方法时,可以借用其他对象的方法来完成。例如:

var nodes = document.getElementsByClass('item'); //得到一个类数组对象
//我想获取数值最大的那个对象
Arrary.prototype.max.apply(null, nodes); //借用Arrary的方法,将我需要的结果算出来

2- 以下代码输出什么

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

3- 下面代码输出什么,及原因

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

4- 下面代码输出结果

document.addEventListener('click', function(e){ 
    console.log(this); //document ,执行click方法的是document
    setTimeout(function(){
        console.log(this); //Window  setTimeout方法的调用者是Window
    }, 200);
}, false);

5- 下面代码输出什么,why

var john = {
  firstName: "John" 
}

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

6- 下面代码有什么问题,怎么来修改

var module= {
  bind: function(){
   $btn.on('click', function(){
    console.log(this) //this指什么
    this.showMsg(); 
 }) 
},

 showMsg: function(){
    console.log('饥人谷');
  }
}
var module= {
  bind: function(){
    var _this = this;
    $btn.on('click', function(){
      console.log(this) //this指什么
      _this.showMsg();
    })
  },

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

原型链相关问题

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

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

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

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

结果:

var str = 'ahbbccdeddddfg';

String.prototype.getMostOften = function(){
    var maxKey;
    var countArr = this.split("");
    var countObj = {};
    var i = 0;
    //计算每个字符出现的次数
    for(i; i < countArr.length; i++){
        if(countObj[countArr[i]]){
            countObj[countArr[i]] += 1;
        }
        else{
            countObj[countArr[i]] = 1;
        }
    }
    console.log(countObj);
    var sortNum = 0;
    for(var key in countObj){
        if(countObj[key] > sortNum){
            sortNum = countObj[key];
            maxKey = key;
        }
    }
    return maxKey;
};


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

10- instanceOf的作用 ,内部逻辑是如何实现的

function _instanceof(A, B) {
    var O = B.prototype;// 取B的prototype
    A = A.__proto__;// 取A的__proto__
    while (true) {
        //Object.prototype.__proto__ === null
        if (A === null)
            return false;
        if (O === A)// 当 O 严格等于 A 时,返回 true
            return true;
        A = A.__proto__;
    }

继承相关

11- 继承的作用

在使用一个类,并想在此类上做扩展,而不需要改动到原类时,就需要用到继承。

12- 下面两种写法有的区别

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

13- Object.create 的作用,及兼容性

//创建一个空对象
var o = {};
var o = Object.create(Object.prototype);

//创建一个带有属性的对象
var o = Object.create(Object.prototype,{ p:{value:88 } } );
var o = {
  p:88
}

//创建一个实例
function Person(){
}
var o = new Person();
var o = Object.create(Person.prototype);

14- hasOwnProperty的作用,及如何使用

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;
}

16- 通过补全代码来实现继承

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();

完整:

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);
   this.age = age;
}

Male.prototype = Object.create(Person.prototype); //继承Person的原型

/**
** 自定义方法写在继承之后
**/
Male.prototype.getAge = function(){
    console.log(this.age);
    return this.age;
    
};

Male.prototype.printName = function(){
    console.log(this.getName());
    return this.getName();
    
};

var ruoyu = new Male('若愚', '男', 27);
ruoyu.printName();
- 其他文章参考学习
上一篇 下一篇

猜你喜欢

热点阅读