js怎样实现继承
js实现继承的5种方式
js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承有以下通用的几种方式
1.使用对象冒充实现继承(该种实现方式可以实现多继承)
实现原理:让父类的构造函数成为子类的方法,然后调用该子类的方法,通过this关键字给所有的属性和方法赋值
- function Parent(firstname)
- {
- this.fname=firstname;
- this.age=40;
- this.sayAge=function()
- {
- console.log(this.age);
- }
- }
- function Child(firstname)
- {
- this.parent=Parent;
- this.parent(firstname);
- delete this.parent;
- this.saySomeThing=function()
- {
- console.log(this.fname);
- this.sayAge();
- }
- }
- var mychild=new Child("李");
- mychild.saySomeThing();
2.采用call方法改变函数上下文实现继承(该种方式不能继承原型链,若想继承原型链,则采用5混合模式)
实现原理:改变函数内部的函数上下文this,使它指向传入函数的具体对象
- function Parent(firstname)
- {
- this.fname=firstname;
- this.age=40;
- this.sayAge=function()
- {
- console.log(this.age);
- }
- }
- function Child(firstname)
- {
- this.saySomeThing=function()
- {
- console.log(this.fname);
- this.sayAge();
- }
- this.getName=function()
- {
- return firstname;
- }
- }
- var child=new Child("张");
- Parent.call(child,child.getName());
- child.saySomeThing();
3.采用Apply方法改变函数上下文实现继承(该种方式不能继承原型链,若想继承原型链,则采用5混合模式)
实现原理:改变函数内部的函数上下文this,使它指向传入函数的具体对象
- function Parent(firstname)
- {
- this.fname=firstname;
- this.age=40;
- this.sayAge=function()
- {
- console.log(this.age);
- }
- }
- function Child(firstname)
- {
- this.saySomeThing=function()
- {
- console.log(this.fname);
- this.sayAge();
- }
- this.getName=function()
- {
- return firstname;
- }
- }
- var child=new Child("张");
- Parent.apply(child,[child.getName()]);
- child.saySomeThing();
4.采用原型链的方式实现继承
实现原理:使子类原型对象指向父类的实例以实现继承,即重写类的原型,弊端是不能直接实现多继承
- function Parent()
- {
- this.sayAge=function()
- {
- console.log(this.age);
- }
- }
- function Child(firstname)
- {
- this.fname=firstname;
- this.age=40;
- this.saySomeThing=function()
- {
- console.log(this.fname);
- this.sayAge();
- }
- }
- Child.prototype=new Parent();
- var child=new Child("张");
- child.saySomeThing();
5.采用混合模式实现继承
- function Parent()
- {
- this.sayAge=function()
- {
- console.log(this.age);
- }
- }
- Parent.prototype.sayParent=function()
- {
- alert("this is parentmethod!!!");
- }
- function Child(firstname)
- {
- Parent.call(this);
- this.fname=firstname;
- this.age=40;
- this.saySomeThing=function()
- {
- console.log(this.fname);
- this.sayAge();
- }
- }
- Child.prototype=new Parent();
- var child=new Child("张");
- child.saySomeThing();
- child.sayParent();
js怎样实现继承的更多相关文章
- 浅谈JS中的继承
前言 JS 是没有继承的,不过可以曲线救国,利用构造函数.原型等方法实现继承的功能. var o=new Object(); 其实用构造函数实例化一个对象,就是继承,这里可以使用Object中的所有属 ...
- JS创建对象、继承原型、ES6中class继承
面向对象编程:java中对象的两个基本概念:1.类:类是对象的模板,比如说Leader 这个是泛称领导,并不特指谁.2:实例:实例是根据类创建的对象,根据类Leader可以创建出很多实例:liyi,y ...
- js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。
js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } Class ...
- js模拟实现继承功能
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- js中实现继承的几种方式
首先我们了解,js中的继承是主要是由原型链实现的.那么什么是原型链呢? 由于每个实例中都有一个指向原型对象的指针,如果一个对象的原型对象,是另一个构造函数的实例,这个对象的原型对象就会指向另一个对象的 ...
- js怎么实现继承?
3. js怎么实现继承? 1. 使用原型prototype 这个问题其实之前总结过了……但是面试时候有点忘……主要思想是记得的,但是不会写,还是基础太不牢靠,写的太少了.一开始因为不知道怎么能继承父类 ...
- [js]js原型链继承小结
这是之前总结的, 发现有很多的毛病,就是重点不突出,重新翻看的时候还是得耗费很长时间去理解这玩意. js中的继承 js中什么是类 1,类是函数数据类型 2.每个类有一个自带prototype属性 pr ...
- js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。
js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } ClassA ...
- 【学习笔记】六:面向对象的程序设计——理解JS中的对象属性、创建对象、JS中的继承
ES中没有类的概念,这也使其对象和其他语言中的对象有所不同,ES中定义对象为:“无序属性的集合,其属性包含基本值.对象或者函数”.现在常用的创建单个对象的方法为对象字面量形式.在常见多个对象时,使用工 ...
- JS中的继承(上)
JS中的继承(上) 学过java或者c#之类语言的同学,应该会对js的继承感到很困惑--不要问我怎么知道的,js的继承主要是基于原型(prototype)的,对js的原型感兴趣的同学,可以了解一下我之 ...
随机推荐
- 使用WindowsAPI实现播放PCM音频的方法
这篇文章主要介绍了使用WindowsAPI实现播放PCM音频的方法,很实用的一个功能,需要的朋友可以参考下 本文介绍了使用WindowsAPI实现播放PCM音频的方法,同前面一篇使用WindowsAP ...
- 图形化升级单机oracle 11.2.0.4 到 12.2.0.1
1. 讲补丁包上传到 Oracle server ,解压.安装 [oracle@11g tmp]$ unzip linuxx64_12201_database.zip 2. 检查当前版本 SQL> ...
- Poj 2411 Mondriaan's Dream(压缩矩阵DP)
一.Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, ...
- requests 的使用
1.1.实例引入 # 引入Requests库 import requests # 发起GET请求 response = requests.get('https://www.baidu.com/') ...
- 杂项:Webpack
ylbtech-杂项:Webpack 本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).当 webpack 处理应用程序时,它会递归地 ...
- ES6学习之正则扩展
RegExp正则函数 var reg = new RegExp("abc","igm"); //等价于 var reg = new RegExp(/abc/ig ...
- Ajax前端调后台方法
后台对当前页面类进行注册 Ajax.Utility.RegisterTypeForAjax(typeof(Login));//Login 当前类名 在方法上面加 [Ajax.AjaxMethod(Aj ...
- [matlab]一道笔试题
x=[1 1; 1 -1; -1 -1; -1 1]'; X=-2:0.01:2; Y=X; N=length(X); [X,Y]=meshgrid(X,Y); Z1=0;Z2=0;Z3=0;Z4=0 ...
- iOS开发中,修改ASIHTTPRequest源码,禁止在POST时URL编码
通过ASIHTTPRequest库进行POST时,会对POST的文本内容进行encodeURL,而且ASIHTTPRequest自身并没有配置项可以关闭这个转换. 本文提供一个方法关闭encodeUR ...
- Python版的数据库查询构造器、ORM及动态迁移数据表。
Orator Orator提供一个简单和方便的数据库数据处理库. 它的灵感来源于PHP的Laravel框架,借助其思想实现了python版的查询构造器和ORM. 这是完整的文档:http://orat ...