当前位置:网站首页>简单化构造函数的继承方法(一)- 组合继承
简单化构造函数的继承方法(一)- 组合继承
2022-07-26 10:15:00 【Xaivor】
在各大博客和资料网上,对于构造函数的继承方法也比较多,以下是我个人认为对构造继承的理解:附上代码先
构造函数的组合继承
// 构造一个函数Person
function Person( name, age ){
this.name = name;
this.age = age;
}
// 添加两个原型方法
Person.prototype.say = function(){
alert('我叫' + this.name + ',今年' + this.age + '岁');
}
Person.prototype.say2 = function(){
alert('我很厉害的!');
}
// var person1 = new Person( '徐磊', 18 );person1.say();
// var person2 = new Person( '王思', 19 );person2.say();
// 再构造一个Girl函数
function Gril( name, age ){
// 组合继承1:Girl继承Person实例方法和属性
// Person.call( this, name, age );
Person.call(this);
this.name = name;
this.age = age;
}
// 组合继承2:Girl继承Person原型方法和继承
Girl.prototype = new Person();
// 修复构造器的指向,把指向Person改为指向自己Girl
Girl.prototype.constructor = Girl;
Girl.prototype.sex = '女';
// 实例化一个基于Person的对象person1
var person1 = new Person( '徐磊', 18 ); person1.say();
// 再实例化一个基于继承了Person的Girl对象girl1
var girl1 = new Girl( '李华', 20 ); girl1.say();girl1.say2();
// 看看构造器的指向是否发生改变
alert(girl1.constructor);
边栏推荐
- Principle analysis and source code interpretation of service discovery
- Force deduction DFS
- SPARK中 DS V2 push down(下推)的一些说明
- 输入整数后输入整行字符串的解决方法
- Encapsulation of tabbarcontroller
- The fourth week of summer vacation
- 2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
- Learning about opencv (1)
- Uni app learning summary
- 在.NET 6.0中配置WebHostBuilder
猜你喜欢

SSG framework Gatsby accesses the database and displays it on the page

面试突击68:为什么 TCP 需要 3 次握手?

Session based recommendations with recurrent neural networks

【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦

分布式网络通信框架:本地服务怎么发布成RPC服务

Mysql5.7.25 master-slave replication (one-way)

Introduction to latex, EPS picture bounding box

Okaleido生态核心权益OKA,尽在聚变Mining模式

Learning about opencv (4)

Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
随机推荐
Strange Towers of Hanoi|汉诺塔4柱问题
输入整数后输入整行字符串的解决方法
Principle analysis and source code interpretation of service discovery
Leetcode 504. 七进制数
Learning about opencv (4)
spolicy请求案例
30 minutes to thoroughly understand the synchronized lock upgrade process
I finished watching this video on my knees at station B
Flutter event distribution
The problem of four columns of Hanoi Tower
MySQL function
Rocky basic exercise -shell script 2
[datawhale] [machine learning] Diabetes genetic risk detection challenge
Phpexcel export Emoji symbol error
Solution of inputting whole line string after inputting integer
Docker configuring MySQL Cluster
Leetcode 504. Hex number
云原生(三十六) | Kubernetes篇之Harbor入门和安装
MySQL 5.7.25 source code installation record
Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da