js继承实例
来源:互联网 发布:求生之路2不同网络 编辑:程序博客网 时间:2024/05/22 10:44
1.js原型(prototype)实现继承
function Person(name, age){
this.name = name;
this.age = age;
}
//给它的原型直接添加属性或方法
Person.prototype.love = "alin";
Person.prototype.sayHello = function(){
console.log(this.name);//lilinwei
}
var per = new Person("lilinwei", 2);
per.sayHello();
console.log(per.love);//alin
function Student(){ }
Student.prototype = new Person("Leeon", 22);
Student.prototype.grade = 4;
Student.prototype.intr = function(){
console.log(this.grade);
}
var stu = new Student();
stu.sayHello();//Leeon
stu.intr();//4
2.构造函数继承
function Parent(name){
this.name = name;
this.sayName = function(){
console.log("Parent:" + this.name);
}
}
function Child(name, age){
this.tempMethod = Parent;
this.tempMethod(name);
this.age = age;
this.sayName = function(){
console.log("Child" + this.name + "age" + this.age);
}
}
var parent = new Parent("zzl");
parent.sayName();//zzl
var child = new Child("llw", 2);
child.sayName();//llw 2
3.call/apply
function Person(name, age){
this.name = name;
this.age = age;
this.say = function(){
console.log("姓名:" + name);
}
}
//call
function Student1(name, age){
Person.call(this, name, age);
}
//apply
function Student2(name, age){
Person.apply(this, [name, age]);
//Person.apply(this, arguments);等价于上一句
}
var per = new Student1("zzl", 88);
per.say();//姓名:zzl
0 0
- js继承实例
- JS原型继承实例
- js继承实例
- js继承实例
- js 继承 实例图示详解
- js原型继承机制实例详解
- js原型继承机制实例详解
- js面向对象的继承实例-call
- js对象继承之原型链继承实例
- js实现继承的几种方式实例讲解
- js中对prototype对象继承的一个实例
- 继承实例
- 继承实例
- 继承实例
- JS继承--组合继承
- JS继承-类继承
- Js继承。
- js继承
- MySQl中的空间扩展
- jenkins+git+gradle构建方案配置
- ngrok安装使用
- 表单和表格序列化demo struts2 json(Gson) response.getWriter.write() ajax
- IntelliJ IDEA下web项目自动部署
- js继承实例
- 如何避免ibdata1文件大小暴涨
- discuz论坛forum_thread表的displayorder字段解释
- 黄绿色——三色配色篇
- Android —— 联系人索引效果
- webuploader.js谷歌浏览器本地调用点击上传文件选择框会延迟几秒才会显示
- Naive Bayes算法(NB算法---朴素贝叶斯)
- Executor框架线程池任务执行全过程原理
- Fragment滑动切换Bar动画