this 原型链 继承

2017-08-30  本文已影响0人  怎么昵称

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

共同点(作用): 都是改变函数this对象的指向,

区别
call( this, p1, p1,p3)
call()第一个参数之后的所有参数都是传入函数的值

apply( this, [p1,p2,p3])
apply() 只有两个参数, 第一个是obj, 第二个是数组,数组中是该函数的参数

bind() 方法和前两者不同在于: bind() 方法会返回执行上下文被改变的函数而不会立即执行,而前两者是直接执行该函数。他的参数和call()相同。

call() apply() 直接t调用执行函数,
bind() 方法会返回执行上下文被改变的函数而不立即执行,必须加()才能立即调用执行
bind() 参数与call() 相同

问题2: 以下代码输出什么?

var john = { 
  firstName: "John" 
}
function func() { 
  console.log(this.firstName + ": hi!")
 // console.log(this)
}
john.sayHi = func
john.sayHi()   // 输出 John: hi!


/*
john.sayHi= func
sayHi: function func(){}
john.func()
this=== john
john.sayHi()  执行函数
this.firstName='John' 找到自己的属性
*/

问题3: 下面代码输出什么,为什么


func()   //输出: Window
function func() { 
  alert(this)  //函数中的this是全局Window对象  因为this 在函数内找不到, 往外找
}

问题4:下面代码输出什么

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

问题5:下面代码输出什么,why

var john = { 
  firstName: "John" 
}

function func() { 
  alert( this.firstName )
}
func.call(john)  //输出: John

call(参数)  参数就是要指向的对象this
而 call(john)  john 就是this对象

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


var module= {
  bind: function(){
    $btn.on('click', function(){
      console.log(this) //this指 $btn
      this.showMsg();
    })
  },
  
  showMsg: function(){
    console.log('饥人谷');
  }
}
修改: 因为this=$btn , $btn  没有 showMsg() 方法,
所以不能调用
var module= {
  bind: function(){
    var  _this= this
    $btn.on('click', function(){
      console.log(this) //this指 $btn
      _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();


解释:
p.__proto__=== Person.prototype
Person.prototype.constructor=== Person

 p 是Person 的实例对象, 拥有Person 的属性
p.__proto__指向了Person的prototype对象,及找到其中的 sayName()方法
 

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

1 调用p.toString()

toString()是继承Object原型对象中定义的toString() 方法。
(1) 首先实例p 会先查找自身有没有这个方法。
(2) 若没有, 则通过 p._proto_去找Person.prototype中有没有toString()。
(3) 还没有找到, 继续往下查找, Person.prototype._proto__指向了Object.prototype, 在prototype中找到了toString()

Paste_Image.png

什么是原型链:
由于_proto_是任何对象都有的属性, 而js 中万物皆是对象, 所以形成一条_proto_连起来的链条,递归访问_proto_必须到头,并且值为null

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


//原型链 一直找到String
String.prototype.getMostofen= function(){
  var dict={};            // 将所有的字符串一次加到 dict,同时为相同值计数
  for(var i=0; i<this.length; i++){
    if(dict[this[i]]){
      ++dict[this[i]]
      
    }else{
      dict[this[i]]=1
    }
  }
  
  
  var count=0,
      maxValue;
  for(key in dict){
    if(dict[key]>count){
      maxValue= key;
      count= dict[key]
    }
    return maxValue;
  }
  
}


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

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

判断一个对象是不是某个构造函数的实例

p instanceOf Person

查看 p._proto_是不是指向Person
如果没有, 继续查找p._proto_._proto_是不是指向Person ,没有的话, 继续往下查找,直到查找到最终都没有时, 返回false
查找到,返回 true

继承相关问题

问题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 有什么作用?兼容性如何?

Obiect.create(参数),
参数为将创建的一个原型对象, 可以使用指定原型对象中的方法和属性

student.prototype=Object.create(Person.prototype)

问题14: hasOwnProperty有什么作用? 如何使用?
判断一个对象中是否包含自定义属性,而不是 原型链上的属性
很特殊, 是js中唯一一个处理属性,但是不查找原型链的函数

function Student(){
this.name=name
}
var s= new Student()
s.hasOwnProperty('name') //true
s对象中有没有自己的属性name

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

对象属性继承作用:call 直接调用 Person函数,将Person中的属性都添加到Male函数中了
this是 Male的实例

问题16: 补全代码,实现继承

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

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

function Male(name, sex, age){
   this.age= age
   Person.call(this, name,sex)    //获取对象属性
}


Male.prototype=Object.create(Person.prototype)  //getName()方法获取
Male.prototype.construtcor=Male

Male.prototype.printName = function(){
    console.log(this.name+'你好')
};
Male.prototype.getAge = function(){
    console.log(this.age+'岁')
};

var ruoyu = new Male('若愚', '男', 27);
ruoyu.getName();
ruoyu.printName();


上一篇下一篇

猜你喜欢

热点阅读