js继承的几种方法理解和代码演示
1、属性继承 :call 、apply:不建议使用浪费内存。
function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
Person.prototype.eat = function(){
console.log("正在吃饭")
}
Person.prototype.sleep = function(){
console.log("正在睡觉")
}
function Man(larynx,beard,name,age,sex,){
Person.call(this,name,age,sex)
this.larynx = larynx;
this.beard = beard;
}
Man.prototype.work = function(){
console.log('111')
}
var songlei = new Man(10,20);
console.log(songlei);
// Man{
// age: undefined
// beard: 20
// larynx: 10
// name: undefined
// sex: undefined
// }
2、原型继承:
3、原型拷贝:
function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
Person.prototype.eat = function(){
console.log("正在吃饭")
}
Person.prototype.sleep = function(){
console.log("正在睡觉")
}
function Man(larynx,beard,name,age,sex,){
Person.call(this,name,age,sex)
this.larynx = larynx;
this.beard = beard;
}
for(var key in Person.prototype){
Man.prototype[key] = Person.prototype[key]
}
Man.prototype.work = function(){
console.log('111')
}
var aaa = new Man(10,20);
console.log(aaa);
// Man { name: undefined, age: undefined, sex: undefined, larynx: 10, beard: 20 }
// __proto__:
// eat: ƒ()
// sleep: ƒ()
// work: ƒ()
// constructor: ƒ Man(larynx, beard, name, age, sex)
var p1 = new Person()
console.log(p1)
// Person { name: undefined, age: undefined, sex: undefined }
// __proto__:
// eat: ƒ()
// sleep: ƒ()
// constructor: ƒ Person(name, age, sex)
4、原型链继承:
function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
Person.prototype.eat = function(){
console.log("正在吃饭")
}
Person.prototype.sleep = function(){
console.log("正在睡觉")
}
function Man(larynx,beard,name,age,sex,){
Person.call(this,name,age,sex)
this.larynx = larynx;
this.beard = beard;
}
// __proto //__proto__
Man.prototype = new Person();
Man.prototype.work = function(){
console.log('111')
}
var aaa = new Man(10,20);
console.log(aaa);
// Man{
// age: undefined
// beard: 20
// larynx: 10
// name: undefined
// sex: undefined
// __proto__: Person
// age: undefined
// name: undefined
// sex: undefined
// work: ƒ()
// }
var p1 = new Person()
console.log(p1)
// Person{
// age: undefined
// name: undefined
// sex: undefined
// __proto__:
// eat: ƒ()
// sleep: ƒ()
// constructor: ƒ Person(name, age, sex)
// }
5、寄生继承:
缺点:增加了无用的函数
function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
Person.prototype.type="人类";
Person.prototype.eat = function(){
console.log("正在吃饭")
}
Person.prototype.sleep = function(){
console.log("正在睡觉")
}
function Man(larynx,beard,name,age,sex,){
Person.call(this,name,age,sex)
this.larynx = larynx;
this.beard = beard;
}
//创建了一个寄生器
function fn(){};
//寄生器的原型对象 = 人类的原型对象
fn.prototype = Person.prototype;
//原型链继承 寄生器的实例对象
Man.prototype = new fn();
Man.prototype.constructor = Man;
Man.prototype.work = function(){
console.log('111')
}
var aaa = new Man(10,20);
console.log(aaa);
// Man{
// age: undefined
// beard: 20
// larynx: 10
// name: undefined
// sex: undefined
// __proto__: Person
// constructor: ƒ Man(larynx, beard, name, age, sex)
// work: ƒ()
// }
6、混合继承:我最喜欢的一种方式
function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
Person.prototype.type="人类";
Person.prototype.eat = function(){
console.log("正在吃饭")
}
Person.prototype.sleep = function(){
console.log("正在睡觉")
}
function Man(larynx,beard,name,age,sex,){
Person.call(this,name,age,sex)
this.larynx = larynx;
this.beard = beard;
}
//Man.prototype = Object.create(Person.prototype);
Man.prototype = {
constructor:Man,
__proto__:Person.prototype
}
Man.prototype.work = function(){
console.log('111')
}
var aaa = new Man(10,20);
console.log(aaa);
// Man{
// age: undefined
// beard: 20
// larynx: 10
// name: undefined
// sex: undefined
// __proto__: Person
// constructor: ƒ Man(larynx, beard, name, age, sex)
// work: ƒ()
// }
var p1 = new Person();
console.log(p1)
// Person{
// age: undefined
// name: undefined
// sex: undefined
// __proto__:
// eat: ƒ()
// sleep: ƒ()
// type: "人类"
// constructor: ƒ Person(name, age, sex)
// }
7、Es6继承
class Person{
constructor(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
eat(){}
sleep(){}
}
class Man extends Person{
constructor(larynx,beard){
//实现继承必须使用super
super();
this.larynx = larynx;
this.beard = beard;
}
work(){}
}
var aaa = new Man()
console.log(aaa)
// Man{
// age: undefined
// beard: undefined
// larynx: undefined
// name: undefined
// sex: undefined
// __proto__: Person
// constructor: class Man
// work: ƒ work()
// }
js继承的几种方法理解和代码演示的更多相关文章
- 实现JS继承的几种方法
总的来说,JS的继承大体上分为两种:借用构造函数方式和原型方式 首先,我们来看看借用构造函数方式的几种做法: //方式一function Person(name, sex){ this.name = ...
- JS继承的6种方法
1.原型链 基本思想:利用原型让一个引用类型继承另外一个引用类型的属性和方法. 构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原 ...
- JS学习笔记——JavaScript继承的6种方法(原型链、借用构造函数、组合、原型式、寄生式、寄生组合式)
JavaScript继承的6种方法 1,原型链继承 2,借用构造函数继承 3,组合继承(原型+借用构造) 4,原型式继承 5,寄生式继承 6,寄生组合式继承 1.原型链继承. <script t ...
- js对象之间的"继承"的五种方法
今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个"动物"对象的构造函数. function Animal(){ this.species = & ...
- 4.Javascript中实现继承的几种方法及其优缺点
要搞懂JS继承,我们首先要理解原型链:每一个实例对象都有一个__proto__属性(隐式原型),在js内部用来查找原型链:每一个构造函数都有prototype属性(显示原型),用来显示修改对象的原型, ...
- js去除空格12种方法
注:本文非本人原著:原文作者: 黄卉 <js去除空格12种方法> //JS去除空格的方法目前共有12种: //实现1 String.prototype.trim = function() ...
- 判断数组的方法/判断JS数据类型的四种方法
参考文: 以下 3 个判断数组的方法,请分别介绍它们之间的区别和优劣Object.prototype.toString.call() . instanceof 以及 Array.isArray() h ...
- Java执行shell脚本并返回结果两种方法的完整代码
Java执行shell脚本并返回结果两种方法的完整代码 简单的是直接传入String字符串,这种不能执行echo 或者需要调用其他进程的命令(比如调用postfix发送邮件命令就不起作用) 执行复杂的 ...
- js继承的几种实现方法
一.用function实现: function Person(name) { this.name = name; } Person.prototype.getName = function() { r ...
随机推荐
- C语言 define实现的宏函数汇总
最大值,最小值 #define MAX( x, y ) ( (x) > (y) ? (x) : (y) )#define MIN( x, y ) ( (x) < (y) ? (x) : ( ...
- c++合并两个序列函数merge()和inplace_merge()
大家在写归并排序时是不是觉得合并两个序列有点麻烦,有快速的方法吗? 我们全部函数自己写,比如: #include<bits/stdc++.h> using namespace std; # ...
- codeforces B. Make Them Odd -C++stl之set的使用
B. Make Them Odd There are nn positive integers a1,a2,…,ana1,a2,…,an. For the one move you can choos ...
- CLR 调试概述
利用公共语言运行时 (CLR) 调试 API,工具供应商可以编写调试器来调试运行于 CLR 环境中的应用程序. 要调试的代码可为 CLR 支持的任何代码种类.CLR 调试 API 主要是使用非托管代码 ...
- Xamarin.IOS/Mac开发中遇到的问题
虚拟机中安装的mac系统无法识别iphone 今天在 Xamarin.iOS 应用的免费预配 时,进行到 5.插入要在其中部署应用的 iOS 设备. 在第8选择iphone设备时,发现iphone并没 ...
- PDO和MySQLi区别和数度;到底用哪个?
当用PHP访问数据库时,除了PHP自带的数据库驱动,我们一般还有两种比较好的选择:PDO和MySQLi.在实际开发过程中要决定选择哪一种首先要对二者有一个比较全面的了解.本文就针对他们的不同点进行分析 ...
- 钟长者P71解题报告
T1 [题目描述] 给你N个字符串,你每次可以选择其中一个字符串的一段前缀进行翻转,但是你必须保证这个前缀的长度是偶数.你可以进行无限次这样的操作,并且如果两个字符串变得相同的时候,你就可以把这两个字 ...
- static final与final修饰的常量有什么不同
最近重头开始看基础的书,对一些基础的概念又有了一些新的理解,特此记录一下 static final修饰的常量: 静态常量(static修饰的全部为静态的),编译器常量,编译时就确定其值(java代码经 ...
- 最大子段和(洛谷 P1115)
题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入格式 第一行是一个正整数NNN,表示了序列的长度. 第二行包含NNN个绝对值不大于100001000010000的整数AiA_iA ...
- 【洛谷】P1449 后缀表达式
P1449 后缀表达式 分析: 简单的模拟题. 熟练容器stack的话很容易解决. stack,栈,有先进后出的特性. 比如你有一个箱子,你每放进第一个数时,就往箱底放,放第二个数时就在第一个数的上面 ...