美文网首页
js中的继承(es5)

js中的继承(es5)

作者: 某言 | 来源:发表于2018-03-07 16:13 被阅读0次

这个问题事关js里面的很多难点的问题,诸如prototype,callapply等,也是js面向对象的问题,值得写一篇.


  • call,apply
    callapply还是比较高级的用法,各个地方解释不好懂,我的解释就是:

改变某个方法执行时候的this指向,就是上下文环境,如果一个函数内输出thisundefined,那么,实际上,call是没作用的,this存在在对象里面

func.call(obj,param1,param2) //对象的参数分别写
func.apply(obj,[param1,param2]) //对象的参数写成array

例如:

var add_obj = {
    a:10,
    b:2
}
function sub_obj(a,b){
    console.log(this.a-this.b)
}
function add(a,b){
    console.log(a,b)
    console.log(this)
}
function sub(a,b){
    console.log(a-b)
}
 //1 仅仅执行sub,因为add中的this不存在,为undefined,不改变this指向
sub.call(add,2,1)
//1 相当于上面的输出
sub.call(undefined,2,1) 
//8, add_obj有this指向,sub_obj执行时候,调用add_obj的上下文环境,所以用的是add_obj的this中的a,b
sub_obj.call(add_obj ,2,1) 
  • 继承的实现
//父类animal
var animal = function(name,food){
    this.name = name
    this.food = food
    this.eat = function(){
        console.log("I like eating "+food)
    }
    this.action = function(){
        console.log("I can jump and run")
    }
}
//子类dog
var dog = function(name,food,praise){
    //实现:将animall的this指向dog的对象,dog的this就有了animal的方法
    //在class内执行,无需要实例化,变可以暴露给this
    animal.call(this,name,food)
    this.praise = function(){
        console.log(praise)
    }
}
//调用
var Dog = new dog("dog","bone","Most of us like dog")
Dog.eat()  //I like eating bone
Dog.action()  //I can jump and run
Dog.praise()  //Most of us like dog

  • prototype继承
var animal = function(name,food){
    this.name = name
    this.food = food
    this.eat = function(){
        console.log("I like eating "+food)
    }
    this.action = function(){
        console.log("I can jump and run")
    }
}
var dog = function(praise){
    this.praise = function(){
        console.log(praise)
    }
}
//实现:其原型链指向animal的实例化对象
dog.prototype = new animal("dog","bone")

var Dog = new dog("Most of us like dog")
Dog.eat()  //I like eating bone
Dog.action()  //I can jump and run
Dog.praise()  //Most of us like dog

  • 构造函数继承
var animal = function(name,food){
    this.name = name
    this.food = food
    this.eat = function(){
        console.log("I like eating "+food)
    }
    this.action = function(){
        console.log("I can jump and run")
    }
}
var dog = function(name,food,praise){
    //实现:讲animal类给dog的this对象,然后属性打开,而非实例化
    this.temp = animal
    this.temp(name,food)
    this.praise = function(){
        console.log(praise)
    }
}

var Dog = new dog("dog","bone","Most of us like dog")
Dog.eat()
Dog.action()
Dog.praise()

  • 面向对象
    而且在用js开发时候,也可以用this或者prototype去进行对象化.

相关文章

  • ES5和ES6中继承的不同之处

    ES5和ES6中继承的不同之处 1、JS中视没有继承的,不过可以通过构造函数或是原型等实现继承,ES5实现继承的方...

  • ES5的继承和ES6的继承

    关于js中的继承,已经老生常谈了,本文将对js的继承做一个大概的总结. 首先我们可以看一下,es5继承关系图,理解...

  • js中的继承(es5)

    这个问题事关js里面的很多难点的问题,诸如prototype,call和apply等,也是js面向对象的问题,值得...

  • JS中的继承(ES5)

    前言 ES6之前,没有严格意义上的class继承, 其实JS(ES5)主要是依靠原型链来实现继承的。 既然要实现继...

  • 浅谈JavaScript原型、原型链的概念与继承的实现原理

    关于js对象的继承,es5和es6提供了两种不同的继承机制。es5通过修改原型链的方式实现继承,由此可见继承与原型...

  • ES5和ES6中继承的不同之处

    1、JS中是没有继承的,不过可以通过构造函数或是原型等实现继承,ES5实现继承的方法——构造函数,当一个构造函数加...

  • JS~继承

    JS中的继承按照是否使用了object.create 可分为两类(object.create 是ES5新增的方法)...

  • JavaScript的多态和封装

    在之前的文章中我们已经介绍了JavaScript的继承包含了ES5和ES6的版本。 ES5继承 传送门 ES6继承...

  • 原生JS - ES5继承

    继承目的:不重复写类的相同属性和方法 摘自JavaScript高级程序设计:继承是OO语言中的一个最为人津津乐道的...

  • ES5和ES6 实现继承方式

    在ES5 中:通过原型链实现继承的,常见的继承方式是组合继承和寄生组合继承;在ES6中:通过Class来继承 组合...

网友评论

      本文标题:js中的继承(es5)

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