javascript常用继承方式.
2024-10-10 00:56:25
//原型链继承
function Parent() {
this.name = 'per';
}
function Child() {
this.age = 20;
}
Child.prototype = new Parent();
var child = new Child();
console.log(child.name + " " + child.age)//per 20
//构造函数继承
function Parent(age) {
this.name = 'person';
this.age = age;
}
function Child(age) {
Parent.call(this, age)
}
var child = new Child(20)
console.log(child.name + " " + child.age)//per 20
// 组合继承
function Parent(age) {
this.name = 'per';
this.age = age;
}
Parent.prototype.sayAge = function () {
return this.name + ' age is ' + this.age;
}
function Child(age) {
Parent.call(this, age)
}
Child.prototype = new Parent();
var child = new Child(21);
console.log(child.sayAge())//per age is 21
最新文章
- MVP社区巡讲-云端基础架构:12月5日北京站 12月12日上海站
- jee websocket搭建总结
- An invalid character [32] was present in the Cookie value
- 使用Ant构建struts2 web工程,自动编译,打包成war
- 移动Web 开发中的一些前端知识收集汇总
- java list三种遍历方法性能比較
- java笔记之类和对象
- MegaCli监控RAID磁盘健康信息
- 文件操作2 cp mv rm
- Mysql Innodb体系结构
- 自动生成proto Js语句
- 爬取IP
- 获取当前人IP地址
- 如何用JS获取页面上的所有标签
- Docker命令分类及使用场景分布(脑图)
- Python拷贝文件脚本
- 【python】网络编程-UDP协议套接字
- MBIST:用于嵌入式存储器的可测试设计技术
- SQL Server 2014与TFS 2013的错误(TF53001:管理员已取消数据库操作)
- js的一些妙用