当前位置:网站首页>简单化构造函数的继承方法(一)- 组合继承
简单化构造函数的继承方法(一)- 组合继承
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);
边栏推荐
- Study notes of the first week of sophomore year
- Wu Enda linear regression of machine learning
- C language course design Tetris (Part 2)
- Learning about opencv (1)
- Applet record
- AR model in MATLAB for short-term traffic flow prediction
- Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
- Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
- El table implements adding / deleting rows, and a parameter changes accordingly
- 在.NET 6.0中配置WebHostBuilder
猜你喜欢
服务发现原理分析与源码解读
新建福厦铁路全线贯通 这将给福建沿海带来什么?
数通基础-二层交换原理
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
Server memory failure prediction can actually do this!
Data communication foundation STP principle
Leetcode 504. 七进制数
[fluorescent character effect]
Uni app learning summary
随机推荐
The fourth week of summer vacation
In Net 6.0
Okaleido生态核心权益OKA,尽在聚变Mining模式
论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)
Getting started with SQL - combined tables
Study notes of the fifth week of sophomore year
2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
Sqoop【付诸实践 02】Sqoop1最新版 全库导入 + 数据过滤 + 字段类型支持 说明及举例代码(query参数及字段类型强制转换)
Why does new public chain Aptos meet market expectations?
网易云UI模仿-->侧边栏
服务发现原理分析与源码解读
In Net 6.0
Principle analysis and source code interpretation of service discovery
How to write a million reading article
MySQL 5.7.25 source code installation record
Learning about opencv (2)
Basic usage of protobuf
Flask框架初学-03-模板
服务发现原理分析与源码解读
Rowselection emptying in a-table