js对象冒充实现的继承
//人类
function Person(name) {
this.name = name;
this.showName = function () {
console.log("my name is " + name);
}
this.eat = function () {
console.log("人是铁饭是钢...");
}
}
//白人
function WhitePerson(name) {
this.temp = Person;
this.temp(name);
delete this.temp;
this.color = function () {
console.log("我们皮肤是偏白色的");
}
}
//黑人
function BlackPerson(name) {
Person.call(this, name);//这个时候Person中的this指向的是BlackPerson的对象了
this.color = function () {
console.log("我们皮肤是偏黑色的");
}
}
var wPreson = new WhitePerson("tom");
wPreson.showName();
var bPerson = new BlackPerson("john");
bPerson.showName();
以对象冒充的方式来实现js的继承
js对象冒充实现的继承的更多相关文章
- 【JavaScript】类继承(对象冒充)和原型继承__深入理解原型和原型链
JavaScript里的继承方式在很多书上分了很多类型和实现方式,大体上就是两种:类继承(对象冒充)和原型继承. 类继承(对象冒充):在函数内部定义自身的属性的方法,子类继承时,用call或apply ...
- 关于JS对象原型prototype与继承,ES6的class和extends · kesheng's personal blog
传统方式:通过function关键字来定义一个对象类型 1234567891011 function People(name) { this.name = name}People.prototype. ...
- JavaScript学习(二)——深入学习js对象的原型与继承
了解对象 什么是对象? …… 这个就不说了 对象的声明的两种方式 var person = new Object(); person.name="linchen"; pers ...
- Javascript 对象继承 原型链继承 对象冒充 call 混合方式
一.原型链继承 function ClassA() {} ClassA.prototype.color = "blue"; ClassA.prototype.sayColor = ...
- Typescript中的类 Es5中的类和静态方法和继承(原型链继承、对象冒充继承、原型链+对象冒充组合继承)
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- JS面向对象,原型,继承
ECMAScript有两种开发模式:1.函数式(过程化),2.面向对象(OOP).面向对象的语言有一个标志,那就是类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.但是,ECMAScript ...
- js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。
js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } Class ...
- js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。
js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } ClassA ...
- 对于js原型和原型链继承的简单理解(第二种,对象冒充)
关键代码 this.parent = Cat; this.parent.apply(this); function Cat(){ this.climb = function(){ aler ...
随机推荐
- PAT (Advanced Level) Practice 1027 Colors in Mars (20 分)
People in Mars represent the colors in their computers in a similar way as the Earth people. That is ...
- 04-SV连接设计和测试平台
1.验证一个设计的步骤: 生成输入激励,捕捉输出响应,决定对错和进度 2.连接设计和测试平台 信号连接:SystemVerilog已经扩展了传统的reg类型,可以像wire那样用来连接模块,它的新名字 ...
- C语言 switch
C语言 switch 功能:获取到值对应成立不同表达式. 优点:switch 语句执行效率比if语句要快,switch是通过开关选择的方式执行,而if语句是从开头判断到结尾. 缺点:不能判断多个区间. ...
- C++中public、protected、private的区别(转载)
转载:https://blog.csdn.net/vanturman/article/details/79393317 首先记住一点:派生类能且只能访问基类的public和protected成员! ...
- PP: Neural ordinary differential equations
Instead of specifying a discrete sequence of hidden layers, we parameterize the derivative of the hi ...
- layui table 超出自动换行
个人博客 地址:http://www.wenhaofan.com/article/20181120180507 layui 的table的的cell默认是超出hidden的,如果希望超出长度自动换行便 ...
- HttpModule介绍
https://cloud.tencent.com/developer/article/1347498 引言 Http 请求处理流程 和 Http Handler 介绍 这两篇文章里,我们首先了解了H ...
- 02 Django虚拟环境搭建
01 创建虚拟环境目录 该目录用于存放所有虚拟环境. cd / mkdir venv cd venv 02 创建当前项目的虚拟环境 virtualenv --python=/usr/bin/pytho ...
- vue老项目升级vue-cli3.0
第一步我们卸载全局的vue2.0然后: 打开命令行 输入npm install -g @vue/cli-init 这个就是会安装全局的vue3.0版本.安装好之后我们也可以vue -V查看当前vu ...
- 运筹学学报-运行问题之新版TeX系统支持修改
<运筹学学报>的LaTeX模板基本上是CCT的典型而且是停留在LaTeX2.09 的时代,故而很多用户下载其模板无法在新TeX系统里使用,这里提供以下解决方案.源文件中的前几行:\docu ...