让前端飞饥人谷技术博客

面向对象03 相关问题

2017-03-15  本文已影响0人  IT男的成长记录

this相关问题

1.apply、call、bind

window.color = "red";
var o = {color: "blue"};
function sayColor(){
    alert(this.color);
}
sayColor();
sayColor.call(this); // red
sayColor.call(o); //blue
window.color = "red";
var o = {color: "blue"};
function sayColor(){
    alert(this.color);
}
var objectSayColor = sayColor.bind(o);
objectSayColor(); // blue

sayColor()调用bind()并传入对象o,创建了objectSayColor()函数,objectSayColor()函数的this值等于o,因此即使是在全局作用域中调用这个函数,也会看到"blue"

2.以下代码输出什么?

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

输出: John: hi!

原因:sayHi()是作为john对象的方法调用的,因此this等于john对象.

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

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

输出: window
原因: 当执行func()时,其实它是作为Window对象的方法调用的,因此,this指的是window

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

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

输出:
document
window

原因: 事件处理函数中的this指的是其对应的dom对象,setTimeout中的this指的是window

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

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('饥人谷');
  }
}

原型链相关问题

1.有如下代码,解释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("yanxin")
p.sayName();

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

原型链: 每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针.如果让原型对象等于另一个类型的实例,则原型对象将包含一个指向另一个原型的指针,相应的另一个原型也包含着一个指向另一个构造函数的指针.如此层层递进,就构成了实例与原型的链条,即原型链

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

var str = 'ahbbccdeddddfg';
var ch = str.getMostOften();

String.prototype.getMostOften = function(){
    var hash = {};
    var mostOften = '';
    var count = 0;
    for(var i = 0; i< this.length; i++){
        if(!hash[this[i]]){
            hash[this[i]] = 1;
        }else {
            hash[this[i]]++;
        }
        if(hash[this[i]] > count){
            count = hash[this[i]];
            mostOften = this[i];
        }
    }
    return mostOften;
}
console.log(ch); //d , 因为d 出现了5次

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

继承相关:

1.继承有什么作用?

2.下面两种写法有什么区别

//方法1
function People(name, sex){
    this.name = name;
    this.sex = sex;
    this.printName = function(){
        console.log(this.name);
    }
}
var p1 = new People('yanxin', 2)

//方法2
function Person(name, sex){
    this.name = name;
    this.sex = sex;
}

Person.prototype.printName = function(){
    console.log(this.name);
}
var p1 = new Person('yanxin', 27);

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

function object(o){
    function F() {};
    F.prototype = o;
    return new F();
}

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

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

6.补全代码,实现继承

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

function inheritPrototype(subType, superType){
    // 创建了一个只包含superType原型对象内容的对象
    var prototype = Object.create(superType.prototype);
    prototype.constructor = subType;
    subType.prototype = prototype;
}

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

var yanxin = new Male('闫鑫', '男', 22);
yanxin.getName();
上一篇 下一篇

猜你喜欢

热点阅读