js 中继承的几种方式
2024-08-29 00:23:33
继承的方式一共有三种:
一、原型继承
通过prototype 来实现继承。
function Person(name,age) {
this.name=name;
this.age=age;
} Person.prototype.sayHello=function(){
alert (''使用原型得到Name:'' + this.name); } var per = new Person("马小倩",21);
per.sayHello();//输出:使用原型得到Name:马小倩 function Student(){} Student.prototype=new Person("洪如彤",21); //实现原型继承 var stu = new Student(); Student.prototype.grade=5; Student.prototype.intr=function(){
alert(this.grade);
} stu.sayHello();//输出:使用原型得到Name:洪如彤
stu.intr();//输出:5
二、构造函数实现继承
function Person(name,age) {
this.name=name;
this.age=age;
} Person.prototype.sayHello=function(){
alert (''使用原型得到Name:'' + this.name); } var per = new Person("马小倩",21);
per.sayHello();//输出:使用原型得到Name:马小倩
三、 通过call、apply 实现继承
最新文章
- 从零开始山寨Caffe·拾:IO系统(三)
- Linux-深入理解Socket异常
- phonegap(cordova)环境配置
- 内存管理tcmalloc
- EasyUI篇のDataGrid
- o2o
- easyui datagrid shift 多选
- Eclipse中代码提示框的背景色修改
- Java中正则表达式去除html标签
- okHttp基础用法
- Vue之Vuex
- jsonp跨域再谈
- (Alpha)个人总结
- js onmouseover与onmouseout用法
- 本人在CSDN上的技术博客访问量突破了10万次,特此截图留念
- jmeter 安装
- Tmutarakan Exams URAL - 1091(莫比乌斯函数 || 容斥)
- Linux Cluster 基础之LVS调度算法与集群类型
- java struts2入门学习---文件下载的二种方式
- HDFS Users Guide