js面试题--js的继承
js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明白的继承机制。而是通过模仿实现的。依据js语言的本身的特性,js实现继承有下面通用的几种方式
1.使用对象冒充实现继承(该种实现方式能够实现多继承)
实现原理:让父类的构造函数成为子类的方法,然后调用该子类的方法,通过thiskeyword给全部的属性和方法赋值
- 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面试题--------JS中数字和字符,布尔类型相加相减问题
JS中数字和字符相加相减问题 <html lang="en"> <head> <meta charset="utf-8" /> ...
- js经典试题之原型与继承
js经典试题之原型与继承 1:以下代码中hasOwnProperty的作用是? var obj={} …….. obj.hasOwnProperty("val") 答案:判断obj ...
- vue.js面试题整理
Vue.js面试题整理 一.什么是MVVM? MVVM是Model-View-ViewModel的缩写.MVVM是一种设计思想.Model 层代表数据模型,也可以在Model中定义数据修改和操作的业务 ...
- 2019前端面试系列——JS面试题
判断 js 类型的方式 1. typeof 可以判断出'string','number','boolean','undefined','symbol' 但判断 typeof(null) 时值为 'ob ...
- [ 转载 ] vue.js面试题一
转载自:https://www.cnblogs.com/aimeeblogs/p/9501490.html 如有侵权 联系删除 Vue.js面试题整理 一.什么是MVVM? MVVM是Model-Vi ...
- 2017、2018面试分享(js面试题记录)记得点赞分享哦;让更多的人看到~~
2017面试分享(js面试题记录) 1. 最简单的一道题 '11' * 2 'a8' * 3 var a = 2, b = 3; var c = a+++b; // c = 5 2. 一道this的问 ...
- 10个常见的Node.js面试题
如果你希望找一份有关Node.js的工作,但又不知道从哪里入手评测自己对Node.js的掌握程度. 本文就为你罗列了10个常见的Node.js面试题,分别考察了Node.js编程相关的几个主要方面. ...
- js中的原型、继承的一些想法
最近看到一个别人写的js类库,突然对js中的原型及继承产生了一些想法,之前也看过其中的一些内容,但是总不是很清晰,这几天利用空闲时间,对这块理解了一下,感觉还是有不通之处,思路上没那么条理,仅作为分享 ...
- 所在实习公司的JS笔试题
在班上无聊的时候看到了一份JS笔试题(我是电面进去的,没做过这份题~~),开始还觉得蛮简单......后来觉得还是很有意思的,贴出来一起看看. 题目一: if(!("a" in w ...
随机推荐
- P1796 汤姆斯的天堂梦_NOI导刊2010提高(05)
题目描述 汤姆斯生活在一个等级为0的星球上.那里的环境极其恶劣,每天12小时的工作和成堆的垃圾让人忍无可忍.他向往着等级为N的星球上天堂般的生活. 有一些航班将人从低等级的星球送上高一级的星球,有时需 ...
- 自己整理的HTML基本标签参考知识
基 本 标 签 创建一个HTML文档 <html></html> 设置文档标题以及其他不在WEB网页上显示的信息 <head></head> ...
- create-react-app 中设置反向代理、项目打包资源引入路径设置及 map 文件
1.配置反向代理 (1)porxy 配置一个代理 修改package.json文件 "proxy":"http://teng.com/website/web", ...
- Android中Button四种点击事件实现方式
1.Xml添加监听属性,这里添加的doClick. <Button android:id="@+id/bt1" android:layout_width="wrap ...
- Leetcode0019--Remove Nth Node From End of List 移除链表第N个结点
[转载请注明]http://www.cnblogs.com/igoslly/p/8672656.html 看一下题目: Given a linked list, remove the nth node ...
- 扩展银行项目,添加一个(客户类)Customer类。Customer类将包含一个Account对象。
练习目标-使用引用类型的成员变量:在本练习中,将扩展银行项目,添加一个(客户类)Customer类.Customer类将包含一个Account对象. 任务 在banking包下的创建Customer类 ...
- jQuery中容易让人困惑的东西
前言:jqueryt很灵活,太灵活了,可以说是他一个优点,也是他一个缺点,达到一种效果,十个人也许会用十种不同的方法来实现这个过程,结果一样,过程不一样,这到底是好,还是坏呢. 一,什么是jquery ...
- C#:winform项目在win7,xp32位和64位都能运行
vs中项目配置管理器活动解决方案平台选择X86平台.
- HTML input 控件
<input type="file" id="file1" onChange="test()"> function test() ...
- Redis 通用key操作命令
1.在redis里面允许模糊查询key,有3个通配符:*,?,[]. *:通配任意字符 ?:通配单个字符 []:通配中括号内的某个字符 例如: 2.randomKey 随机返回所有key中的某个 3. ...