当前位置:网站首页>简单化构造函数的继承方法(一)- 组合继承
简单化构造函数的继承方法(一)- 组合继承
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);
边栏推荐
- Flutter Event 派发
- 点赞,《新程序员》电子书限时免费领啦!
- Server memory failure prediction can actually do this!
- Learning about opencv (1)
- Sqoop [put it into practice 02] sqoop latest version full database import + data filtering + field type support description and example code (query parameter and field type forced conversion)
- 服务发现原理分析与源码解读
- Solution of inputting whole line string after inputting integer
- IE7 set overflow attribute failure solution
- Write a script that can run in Bash / shell and PowerShell
- Basic usage of protobuf
猜你喜欢

数通基础-二层交换原理

Learning about opencv (3)

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

The charm of SQL optimization! From 30248s to 0.001s

Data communication foundation TCPIP reference model

Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool
![Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)](/img/f9/c86ccbb0967894d370498c3a9630eb.png)
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)

Meeting OA project (III) -- my meeting (meeting seating and submission for approval)

点赞,《新程序员》电子书限时免费领啦!

新建福厦铁路全线贯通 这将给福建沿海带来什么?
随机推荐
About automatic operation on Web pages
Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
Keeping alive to realize MySQL automatic failover
SQL Server 2008 server engine failed to start?
Wu Enda linear regression of machine learning
In Net 6.0
在.NET 6.0中配置WebHostBuilder
[fluorescent character effect]
SQL Server 2008 R2 installation problems
Some descriptions of DS V2 push down in spark
Replay the snake game with C language (II) end
How to use Gmail to pick up / send mail on Foxmail
数据库的复习--1.概述
Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component
Yarn 'TSC' is not an internal or external command, nor is it a runnable program or batch file. The problem that the command cannot be found after installing the global package
Data communication foundation - layer 2 switching principle
Principle analysis and source code interpretation of service discovery
Session based recommendations with recurrent neural networks
Interpretation of the standard of software programming level examination for teenagers_ second level
In Net 6.0