当前位置:网站首页>TypeScript–es5中的类,继承,静态方法
TypeScript–es5中的类,继承,静态方法
2022-06-30 09:50:00 【全栈程序员站长】
最简单的类
function Person() {
this.name = 'lisi';
this.age = 20;
}
var p = new Person();
alert(p.name);
构造函数和原型链里增加方法
function Person() {
this.name = 'lisi';
this.age = 20;
this.run = function() {
alert(this.name + '在运动');
}
}
Person.prototype.sex = '男';
Person.prototype.work = function() {
alert(this.name + '在工作');
}
var p = new Person();
alert(p.name);
p.run();
p.work()
类里的静态方法
function Person() {
this.name = 'lisi';
this.age = 20;
this.run = function() {
alert(this.name + '在运动');
}
}
Person.getInfo = function() {
alert('我是静态方法')
}
Person.prototype.sex = '男';
Person.prototype.work = function() {
alert(this.name + '在工作');
}
var p = new Person();
// p.work();
Person.getInfo()
es5里面的继承,对象冒充实现继承
只有一个弹框,说明没有继承到prototype上的方法
function Person() {
this.name = 'lisi';
this.age = 20;
this.run = function() {
alert(this.name + '在运动');
}
}
Person.prototype.sex = '男';
Person.prototype.work = function() {
alert(this.name + '在工作');
}
//Web类继承Person类
function Web() {
Person.call(this);
}
var w = new Web();
w.run();
w.work();
es5里面的继承,原型链实现继承
function Person() {
this.name = 'lisi';
this.age = 20;
this.run = function() {
alert(this.name + '在运动');
}
}
Person.prototype.sex = '男';
Person.prototype.work = function() {
alert(this.name + '在工作');
}
//Web类继承Person类
function Web() {
}
Web.prototype = new Person();
var w = new Web();
w.run();
w.work();
原型链实现继承的问题
function Person(name, age) {
this.name = name;
this.age = age;
this.run = function() {
alert(this.name + '在运动');
}
}
Person.prototype.sex = '男';
Person.prototype.work = function() {
alert(this.name + '在工作');
}
function Web(name, age) {
}
Web.prototype = new Person();
var w = new Web('lisi', 20)
w.run()
w.work()
原型链+对象冒充的组合继承模式
function Person(name, age) {
this.name = name;
this.age = age;
this.run = function() {
alert(this.name + '在运动');
}
}
Person.prototype.sex = '男';
Person.prototype.work = function() {
alert(this.name + '在工作');
}
function Web(name, age) {
Person.call(this, name, age);
}
Web.prototype = new Person();
var w = new Web('lisi', 20)
w.run()
w.work()
原型链+对象冒充的另一种方式
function Person(name, age) {
this.name = name;
this.age = age;
this.run = function() {
alert(this.name + '在运动');
}
}
Person.prototype.sex = '男';
Person.prototype.work = function() {
alert(this.name + '在工作');
}
function Web(name, age) {
Person.call(this, name, age);
}
Web.prototype = Person.prototype;
var w = new Web('lisi', 20)
w.run()
w.work()
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/101123.html原文链接:https://javaforall.cn
边栏推荐
- "Kunming City coffee map" was opened again, and coffee brought the city closer
- Get through the supply chain Shenzhen gift show helps cross-border e-commerce find ways to break the situation
- Notes on numerical calculation - iterative solution of linear equations
- South China Industrial Group launched digital economy and successfully held the city chain technology conference
- Leetcode question brushing (III) -- binary search (go Implementation)
- Yixian e-commerce released its first quarterly report: adhere to R & D and brand investment to achieve sustainable and high-quality development
- Turn to cartoon learning notes
- Implementation of iterative method for linear equations
- ArcGIS Pro脚本工具(5)——排序后删除重复项
- mysql数据库基础:视图、变量
猜你喜欢
[email protected]在oled上控制一条狗的奔跑"/>
技能梳理[email protected]在oled上控制一条狗的奔跑
Yixian e-commerce released its first quarterly report: adhere to R & D and brand investment to achieve sustainable and high-quality development
Arm新CPU性能提升22%,最高可组合12核,GPU首配硬件光追,网友:跟苹果的差距越来越大了...
[email protected]基于51系列单片机的智能仪器教具"/>
技能梳理[email protected]基于51系列单片机的智能仪器教具
Deployment of efficient and versatile clusters lvs+kept highly available clusters
透过华为军团看科技之变(五):智慧园区
长城数艺数字藏品平台发布创世徽章
Didn't receive robot state (joint angles) with recent timestamp within 1 seconds
“昆明城市咖啡地图”活动再度开启
Compétences Comb 27 @ Body sense Manipulator
随机推荐
Getting started with X86 - take over bare metal control
Questions about cookies and sessions
光明行动:共同呵护好孩子的眼睛——广西实施光明行动实地考察调研综述
Detailed explanation of commissioning methods and techniques
What is the real performance of CK5, the king machine of CKB?
MIT-6874-Deep Learning in the Life Sciences Week5
Dyson design award, changing the world with sustainable design
GD32 RT-Thread DAC驱动函数
June training (day 30) - topology sorting
mysql数据库基础:约束、标识列
Skill combing [email protected] control a dog's running on OLED
那个程序员,被打了。
MySQL advanced SQL statement of database (1)
Skill sorting [email protected]+ Alibaba cloud +nbiot+dht11+bh1750+ soil moisture sensor +oled
华南产业集团发力数字经济,城链科技发布会成功召开
Apple's 5g chip was revealed to have failed in research and development, and the QQ password bug caused heated discussion. Wei Lai responded to the short selling rumors. Today, more big news is here
最新SCI影响因子公布:国产期刊最高破46分!网友:算是把IF玩明白了
[ark UI] implementation of the startup page of harmoniyos ETS
Harvester ch1 of CKB and HNS, connection tutorial analysis
1033 To Fill or Not to Fill