1.prototype方式

示例:没有使用prototype(下列这些代码只能获取array1数组的总和,而无法对array2数据进行求和)

var array1 = new Array(1,4,9,13);
var array2 = new Array(15, 20, 35); array1.sum1 = function( ) {
    var result = 0;
    for(var i=0; i<array1.length; i++) {
        result = result + array1[i];
    }
    return result;
};

示例:使用 prototype后

Array.prototype.sum = function( ) {
    var result = 0;
    for(var i=0; i<array1.length; i++) {
        result=result + array1[i];
    }
    return result;
}
//使用prototype代表对所有数组的求和计算
console.log(array1.sum1( ));     //
console.log(array2.sum( ));        //

总结:使用 prototype 可以对某一类对象方法进行扩展

那么如何使用prototype继承?

示例:

// 父类
function Parent( ) {
    var name;
    var age;
    this.setName = function(thisName) {
        name = thisName;
    }
    this.getName = function( ) {
        return name;   
    }
    this.setAge = function(thisAge) {
        age = thisAge;   
    }
    this.getAge = function() {
        return age;   
    }
}
// 子类
function Child( ) { }
// 子类继承父类,将直接继承父类的所有public属性和方法
Child.prototype = new Parent();
var c = new Child();
c.setName("张三");
c.setAge(25);
console.log(c.getName( )+" "+c.getAge( ));

2.apply方式

语法:

Function.apply(obj, args)  方法能接收两个参数

obj:这个对象将代替Function类里this对象

args:这个是数组,它将作为参数传给Function(args-->arguments)

apply方法的使用:

function Parent(name) {
    this.name = name;
    var age = 25;
    this.getAge = function( ) {
        return age;
    }
}
function Child(name) {
    this.say = function() {
        return this.name+" "+this.getAge();
    }
    this.getName = function(){
        return name;
    }
}
var c = new Child("张三");
Parent.apply(c,[c.getName()]);
console.log(c.say());     //张三 25 var c=new Child("张三");
Parent.apply(c,[""]);
console.log(c.say());     //

如何使用apply继承?

示例一(不推荐):

function Parent(name) {
var name = name;
this.getName = function( ) {
return name;
}
}
function Child() { } var c = new Child();
Parent.apply(c,["张三"]);
console.log(c.getName()); //

示例二:

/*定义一个人类*/
function Person(name, age) {
this.name=name;
this.age=age;
}
/*定义一个学生类*/
function Student(name, age, grade) {
Person.apply(this, arguments);
this.grade = grade;
}
//创建一个学生类
var student=new Student("zhangsan",21,"一年级");
//测试
console.log("name:"+student.name+"\n"+"age:"+student.age+"\n"+"grade:"+student.grade);

分析:
Person.apply(this,arguments);
this:在创建对象在这个时候代表的是student
arguments:是一个数组,也就是[“zhangsan”,”21”,”一年级”];  通俗一点讲就是父类Person构造中的参数

3.call方式

与apply 方法非常类似,只不过是apply 中 args 中一个数据,而call 中是一个一个的参数

语法:

Function.apply(obj, arg1, arg2, ...)   可以接收(0-N)个参数

call 方法的使用

function Show() {
    console.log(this);   
}
// 1.直接调用:Show( );    这种调用方法其实是js 中的一种缩写
// 2.Show.call( );    这种才是js调用
Show.call( );           // window
Show.call(12);       // 将打印12,即show方法中的this值变为12 function Show2(name, age) {
    console.log(this, name, age);   
}
Show2("波哥", 25);                         // window, "波哥", 25
Show2.call("abc", "caoxiaobo", 25);       // {"abc"}, "caoxiaobo", 25

如何使用call方法继承?

function A( ) {
    this.abc = 24;   
}
A.prototype.get = function( ) {
    console.log("abc的值为:" + this.abc);
}
function B( ) {
    A.call(this);            // 继承A的所有属性
}
B.prototype.save = function( ) {
    console.log("B方法中的save");   
} B.prototype = A.prototype;    // 继承A的所有方法(注间,不能写在创建B对象的下面)
var b = new B();
var a = new A();
console.log(b.abc);
b.get( );
a.save( );
function Parent(name, age) {
var name = name;
var age = age;
this.getName = function( ) {
return name;
}
this.getAge = function( ) {
return age;
}
}
function Child() { } var c = new Child();
Parent.call(c,"张三", 20);
console.log(c.getName(), c.getAge()); // 张三 20

同样也可以这样写:

function Parent(name, age) {
var name = name;
var age = age;
this.getName = function( ) {
return name;
}
this.getAge = function( ) {
return age;
}
}
function Child(name, age) {
Parent.call(c, name, age);
} var c = new Child("张三", 20);
console.log(c.getName(), c.getAge()); // 张三 20

4.对象冒充

实现原理:让父类的构造函数成为子类的方法,然后调用该子类的方法,通过this关键字给所有的属性和方法赋值

function Parent(name) {
    this.name = name;
    var age = 40;
    this.getAge = function( ) {
        return age;
    }
}
function Child(name) {
    this.method = Parent;     // this.method是作为一个临时的属性,并且指向Parent所指向的对象,
    this.method(name);        // 执行this.method方法,即执行Parent所指向的对象函数
    delete this.method;       // 销毁this.method属性,即此时Child就已经拥有了Parent的所有属性和方法
    this.say = function( ) {
        return this.name+" "+this.getAge();
    }
}
var c = new Child("李四");
console.log(c.say());

5.混合模式

propotype + call / apply 方式

function Parent( ) {
    this.getAge = function( ) {
        return this.age;
    }
} Parent.prototype.sayParent = function( ) {
   alert("this is parentmethod!!!");
} function Child(name) {
    Parent.call(this);
    this.name = name;
    this.age = 40;
    this.say = function( ) {
        return this.name+" "+this.getAge( );
    }
} Child.prototype = new Parent();
var c = new Child("张三");
console.log(c.say( ));
c.sayParent( );

javascript基础-js继承的更多相关文章

  1. Javascript 基础--JS函数(三)

    一.基本概念:未完成某一个功能的代码(语句,指令)的集合. 二.函数的调用方式: 2.1.函数名(传递参数1,传递参数2)   基本语法 function 函数名(参数列表){ //代码; retur ...

  2. Javascript基础 - js中曾经忽略的知识点

    深入那些曾经忽略的Javascript知识 1. parseInt(string, [radix]),parseFloat(string) 一般我们省略第二个参数,parseInt(‘100’) == ...

  3. javascript基础-js对象

    一.js对象的创建 1.普通最简单的方式 var teacher = new Object( ); teacher.name = "zhangsan"; teacher.age = ...

  4. JavaScript基础 -- js常用内置方法和对象

    JS中常用的内置函数如下: 1.eval(str):计算表达式的结果. 2.parseInt(str,n):将符串转换成整数数字形式(可指定几进制). 3.parseFloat(str):将字符串转换 ...

  5. javascript基础-js函数

    一.创建函数的方式 1)普通方式 function cal( num1, num2 ) { return num1+num2; } 2)使用变量初始化方式 var plus = function(nu ...

  6. JavaScript(JS)简介

    历史背景介绍 (Brendan Eich)在其Netscape Navigator 2.0产品中开发出一套livescript的脚本语言.Sun和Netscape共同完成.后改名叫Javascript ...

  7. 三张图搞懂JavaScript的原型对象与原型链 / js继承,各种继承的优缺点(原型链继承,组合继承,寄生组合继承)

    摘自:https://www.cnblogs.com/shuiyi/p/5305435.html 对于新人来说,JavaScript的原型是一个很让人头疼的事情,一来prototype容易与__pro ...

  8. 前端(十二)—— JavaScript基础操作:if语句、for循环、while循环、for...in、for...of、异常处理、函数、事件、JS选择器、JS操作页面样式

    JavaScript基础操作 一.分支结构 1.if语句 if 基础语法 if (条件表达式) { 代码块; } // 当条件表达式结果为true,会执行代码块:反之不执行 // 条件表达式可以为普通 ...

  9. javascript基础入门之js中的结构分支与循环语句

    javascript基础入门之js中的结构分支与循环语句 程序的结构①顺序结构:自上而下:②选择(分支)结构:多条路径,根据不同的条件,只执行其中一个:③循环结构:重复某些代码④配合特定的语句实现选择 ...

随机推荐

  1. CentOS 6安装Oracle 11gR2数据库

    1.安装环境--- 操作系统:CentOS release 6.8 (Final) oracle:Oracle Database 11g Enterprise Edition Release 11.2 ...

  2. zuul 性能分析

    引用几篇博客, 后续整理自己心得 Zuul 性能测试 https://blog.csdn.net/u013815546/article/details/69669165 VisualVM 使用 htt ...

  3. 关于px em rem的一点小总结

    2015-11-28 06:06:40 概念 都是CSS单位. px:像素 Pixel.像素 (计算机屏幕上的一个点) em:1em 等于当前的字体尺寸. 2em 等于当前字体尺寸的两倍. 例如,如果 ...

  4. qml 源码样例

    https://github.com/CodeBees/qtExample https://github.com/zhengtianzuo/QtQuickExamples/blob/master/RE ...

  5. [AirFlow]AirFlow使用指南三 第一个DAG示例

    经过前两篇文章的简单介绍之后,我们安装了自己的AirFlow以及简单了解了DAG的定义文件.现在我们要实现自己的一个DAG. 1. 启动Web服务器 使用如下命令启用: airflow webserv ...

  6. 神奇的 ViewDragHelper,让你轻松定制拥有拖拽能力的 ViewGroup

    为了吸引大家的注意力,先给大家看一张动图: 相信这种效果大家都见过吧?我第一次见到这样的效果时,心里也痒痒的,急于想实现这种功能,后来因为拖延症的问题,就一直没有去弄这件事.现在这段时间,工作比较轻闲 ...

  7. 文件处理工具 gif合成工具 文件后缀批量添加工具 文件夹搜索工具 重复文件查找工具 网页图片解析下载工具等

    以下都是一些简单的免费分享的工具,技术支持群:592132877,提供定制化服务开发. Gif动图合成工具 主要功能是扫描指定的文件夹里的所有zip文件,然后提取Zip文件中的图片,并合成一张gif图 ...

  8. Frame-Relay交换机

  9. [转](阿里笔试)使用多线程和sleep函数生成字符串的伪随机排列

    http://blog.csdn.net/jiaowopan/article/details/12009079 C/C++(Linux平台) 注意编译连接时需要链接pthread库,即g++ XX.c ...

  10. (笔记)JQuery扩展方法实现Form表单与Json互相转换

    JQuery笔记 记两段代码,使用JQuery实现从表单获取json与后端交互,以及把后端返回的json映射到表单相应的字段上. 把表单转换出json对象 //把表单转换出json对象 $.fn.to ...