js 实现继承

我们现在要做的一件事情是像其他语言的面向对象一下实现继承多态

具体要求如下:

一个 Father 构造函数,一个 Child 构造函数,其中改写 Father中的部分参数, new Child() 表示出一个新的child

        var Father = function (name) {
            this.name = name
            this.say = function () {
                console.log(‘i am ‘ + name)
            }
        }

        var Child = function (name,age) {
            this.age = age;
            this.say = function () {
                console.log("name:" + this.name + " and age:" + this.age);
            }
        }
        Child.prototype = new Father()
        var he = new Child(‘asd‘,12)     console.log(he.firstName) // qiao
        console.log(he.name)  
        console.log(he.age)
        he.say()    

无法输出 name 是因为不能穿参数

    var Father = function (name) {
            this.name = name
            this.firstName = ‘qiao‘
            this.say = function () {
                console.log(‘i am ‘ + name)
            }
        }
        var Child = function (name,age) {
            this.tempMethod = Father
            this.tempMethod(name)
            this.age = age;
            this.say = function () {
                console.log("name:" + this.name + " and age:" + this.age);
            }
        }  

        var he = new Child(‘asd‘,12)
        console.log(he.firstName)   // qiao
        console.log(he.name)    // sad
        console.log(he.age)     // 12
        he.say()                // name:undefined and age:12

这样书写就可以继承name了

利用call可以这样书写

    var Child = function (name,age) {
            Father.call(this,name)
            this.age = age
            this.say = function(){
                console.log(‘i am ‘ + name +‘and age ‘+ age )
            }
        }  

利用apply的话会更加巧妙一点,不用管参数是什么

      var Child = function (name,age) {
            Father.apply(this,arguments)
            this.age = age
            this.say = function(){
                console.log(‘i am ‘ + name +‘and age ‘+ age )
            }
        }  
时间: 2024-08-24 09:40:18

js 实现继承的相关文章

js中继承的几种用法总结(apply,call,prototype)

本篇文章主要介绍了js中继承的几种用法总结(apply,call,prototype) 需要的朋友可以过来参考下,希望对大家有所帮助 一,js中对象继承 js中有三种继承方式 1.js原型(prototype)实现继承 <SPAN style="<SPAN style="FONT-SIZE: 18px"><html>   <body>  <script type="text/javascript"> 

JS对象继承篇

JS对象继承篇 ECMAScript只支持实现继承,而且其实现继承主要是依靠原型链来实现的 原型链 其基本思路是利用原型让一个引用类型继承另一个引用类型的属性和方法 function Person(){ this.name = "Person"; } Person.prototype.getName = function(){ return this.name; }; function SuperPerson(name,sex){ this.name = name; this.sex

JS 类继承 原型继承

参考文档:JS原型继承和类继承 <script src="jquery-2.0.3.js"> </script> <script> /*//类继承 var father=function(){ this.age=53; this.say=function(){ console.log("name:"+this.name+",age:"+this.age); } }; var child=function(){

关于 JS 面向对象继承属性和方法的小例子

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h1>关于 JS 面向对象继承属性和方法的小例子</h1> </body> </html> <script> //人的构造函

js:深入继承

/** * js实现继承: * 1.基于原型链的方式 * 2.基于伪造的方式 * 3.基于组合的方式 */ 一.基于原型链的方式 function Parent(){ this.pv = "parent"; } Parent.prototype.showParentValue = function(){ console.log(this.pv); } function Child(){ this.cv = "child"; } //让Child的原型链指向Paren

JS组合继承的通用工具函数

此工具函数没实际意义,只是鉴于EXT的extend方法不太好理解,写了一个简化的extend方法,帮助理解. /** * */ E = {}; E.extend = function(sub, sup) { //借用构造函数 sub.prototype = sup; //保留父类的构造函数,以便在子类构造函数中用调用,将父类变量绑定在this下 sub.prototype.superclass = sup.constructor; //因为重写了构造函数所以重新指定constructor,以使i

浅谈JS的继承

JS继承 继承是OO语言中最为人津津乐道的概念,许多OO语言都支持两种方式的继承:接口继承:实现继承. 接口继承:只继承方法签名. 实现继承:继承实际的方法. 由于ES里函数没有签名,所以在ES里面无法实现接口继承,ES只支持实现继承.                                                                                                                                    

js实现继承的5种方式

js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承有以下通用的几种方式1.使用对象冒充实现继承(该种实现方式可以实现多继承)实现原理:让父类的构造函数成为子类的方法,然后调用该子类的方法,通过this关键字给所有的属性和方法赋值 Js代码   function Parent(firstname) { this.fname=firstname; this.age=40; this.sayAge=func

浅谈js中继承的理解和实现

一.前言 java.C#等正统面向对象语言都会提供类似extend之类的处理类的继承的方法,而javascript并没有提供专门的方法用于继承,在javascript中使用继承需要一点技巧.js中实例的属性和行为是由构造函数和原型两部分组成的,js的继承也分为这两部分.下面给大家分享一下在js中如何实现继承,讲的不对的地方望大家指正! 二.继承构造函数中的属性和行为 我们定义两个类Animal和Bird类,来实现js中类的继承. //定义Animal类 function Animal(name)

js的继承实现方式

1. 使用call或者apply来实现js对象继承 function Animal(age){ this.age = age; this.say = function(){ console.log('age:'+this.age); }; } function Dog(age,nickname){ Animal.call(this,age); // Animal.apply(this,[age]); this.nickname = nickname; this.say = function(){