当前位置:网站首页>简单化构造函数的继承方法(一)- 组合继承
简单化构造函数的继承方法(一)- 组合继承
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);
边栏推荐
- Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
- Use of tabbarcontroller
- What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?
- Keeping alive to realize MySQL automatic failover
- SSG framework Gatsby accesses the database and displays it on the page
- What is the principle of reflection mechanism?
- 论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)
- Learning about opencv (3)
- Jpg to EPS
- Installation and use of cocoapods
猜你喜欢
![[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)](/img/a7/b3bb6f584dff0eb9b49e81e5b9dade.png)
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)

PMM (percona monitoring and management) installation record

30 minutes to thoroughly understand the synchronized lock upgrade process

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

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

Basics of data communication - basic knowledge of network

Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool

Learning about opencv (3)

数据库的复习--3.SQL语言

Data communication foundation TCPIP reference model
随机推荐
In the same CONDA environment, install pytroch first and then tensorflow
How to use Gmail to pick up / send mail on Foxmail
网易云UI模仿-->侧边栏
Use of pclint in vs2013
万字详解“用知识图谱驱动企业业绩增长”
Study notes of the first week of sophomore year
AR model in MATLAB for short-term traffic flow prediction
[award-winning question] ask Judea pearl, the Turing prize winner and the father of Bayesian networks
云原生(三十六) | Kubernetes篇之Harbor入门和安装
Use of selectors
Write a script that can run in Bash / shell and PowerShell
Getting started with SQL - combined tables
Opencv image processing
PHP executes shell script
【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦
Study notes at the end of summer vacation
regular expression
Solution of inputting whole line string after inputting integer
数通基础-Telnet远程管理设备
Data communication foundation telnet remote management equipment