js面向对象之继承-原型继承
//animal 父类 超类
var Animal = function(name)
{
this.name = name;
this.sayhello = function()
{
alert("HI,我是" + this.name + ",你愿意和我做朋友吗?");
};
};
Animal.prototype.shout = function()
{
alert(this.name + ",正在叫!");
};
Animal.prototype.game = function()
{
alert(this.name + ",正在玩耍!");
}; var Dog = function(name)
{
//this.name = name;
this.name = name;
this.shout = function()//重写父类的函数
{
alert(this.name + ",正在汪汪叫!");
};
}; var Cat = function(name)
{
this.name = name;
this.shout = function()
{
alert(this.name + ",正在喵喵叫!");
};
}; //原型继承
Dog.prototype = Cat.prototype = new Animal(); var xh = new Dog("小黑");
xh.sayhello();
xh.shout();
xh.game(); var xm = new Cat("小咪");
xm.sayhello();
xm.shout();
xm.game();
封装一个函数后:
var inherit = function (subclass, superclass) {
subclass.prototype = new superclass();
};
//animal 父类 超类
var Animal = function (name) {
this.name = name;
this.sayhello = function () {
alert("HI,我是" + this.name + ",你愿意和我做朋友吗?");
};
};
Animal.prototype.shout = function () {
alert(this.name + ",正在叫!");
};
Animal.prototype.game = function () {
alert(this.name + ",正在玩耍!");
};
var Dog = function (name) {
//this.name = name;
this.name = name;
this.shout = function () //重写父类的函数
{
alert(this.name + ",正在汪汪叫!");
};
};
var Cat = function (name) {
this.name = name;
this.shout = function () {
alert(this.name + ",正在喵喵叫!");
};
};
//原型继承
//Dog.prototype = Cat.prototype = new Animal();
inherit(Dog, Animal);
inherit(Cat, Animal);
var xh = new Dog("小黑");
xh.sayhello();
xh.shout();
xh.game();
var xm = new Cat("小咪");
xm.sayhello();
xm.shout();
xm.game();
给函数添加extends方法
Function.prototype.extends = function (superclass) {
this.prototype = new superclass();
};
//animal 父类 超类
var Animal = function (name) {
this.name = name;
this.sayhello = function () {
alert("HI,我是" + this.name + ",你愿意和我做朋友吗?");
};
};
Animal.prototype.shout = function () {
alert(this.name + ",正在叫!");
};
Animal.prototype.game = function () {
alert(this.name + ",正在玩耍!");
};
var Dog = function (name) {
this.name = name;
this.shout = function () //重写父类的函数
{
alert(this.name + ",正在汪汪叫,叫的很开心!");
};
};
Dog.extends(Animal);
var Cat = function (name) {
this.name = name;
this.shout = function () {
alert(this.name + ",正在喵喵叫!");
};
};
Cat.extends(Animal);
//原型继承
//Dog.prototype = Cat.prototype = new Animal();
/*inherit(Dog, Animal);
inherit(Cat, Animal);*/
//Dog.extends(Animal);
//Cat.extends(Animal);
/*var xh = new Dog("小黑");
xh.sayhello();
xh.shout();
xh.game();
var xm = new Cat("小咪");
xm.sayhello();
xm.shout();
xm.game();*/
var Husky = function (name, color, sex) {
this.name = name;
this.color = color;
this.sex = sex;
this.sayhello = function () {
alert("Hello,我是一条小" + this.sex + "狗,有一个非常好听的名字,叫:“" + this.name + "”,你愿意和我做朋友吗?");
};
this.showcolor = function () {
alert(this.color);
};
/*this.shout = function()//重写父类的函数
{
alert(this.name + ",哼哼叫!");
};*/
};
Husky.extends(Dog);
var xh = new Husky("小哈", "黑白", "公");
xh.sayhello();
xh.shout();
xh.game();
xh.showcolor();
js面向对象之继承-原型继承的更多相关文章
- Js 面向对象之封装,继承,原型,原型链
封装 ,继承 ,原型, 原型链 封装 ? 面向对象有三大特性,封装.继承和多态.对于ES5来说,没有class(类)的概念,并且由于JS的函数级作用域(函数内部的变量在函数外访问不到),所以我们就可以 ...
- js面向对象设计之class继承
EcmaScript 2015 (又称ES6)通过一些新的关键字,使类成为了JS中一个新的一等公民.但是目前为止,这些关于类的新关键字仅仅是建立在旧的原型系统上的语法糖,所以它们并没有带来任何的新特性 ...
- JS面向对象(封装,继承)
在六月份找工作中,被问的最多的问题就是: js面向对象,继承,封装,原型链这些,你了解多少? 额,,,我怎么回答呢, 只能说,了解一些,不多不少,哈哈哈哈,当然,这是玩笑话. 不过之前学过java,来 ...
- JS面向对象(二)---继承
一.面向对象的继承 1.解析:在原有对象的基础上,略作修改,得到一个新的对象,并且不影响原有对象的功能 2.如何添加继承---拷贝继承 属性:call 方法: for in /* 继承:子类不影响父类 ...
- js面向对象(构造函数与继承)
深入解读JavaScript面向对象编程实践 Mar 9, 2016 面向对象编程是用抽象方式创建基于现实世界模型的一种编程模式,主要包括模块化.多态.和封装几种技术. 对JavaScript而言,其 ...
- js原生设计模式——2面向对象编程之继承—原型继承(类式继承的封装)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- js面向对象的程序设计 --- 下篇 继承启蒙
继承是oo语言中一个最为人津津乐道的概念.ECMAScript支持实现继承,而且实现继承只要是靠原型链来实现的 ·原型链 其基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法. 简单回顾一 ...
- JS继承,原型继承,构造函数的继承,非构造函数"的继承
a.原型继承 一.new运算符的缺点 用构造函数生成实例对象,有一个缺点,那就是无法共享属性和方法.比如,在DOG对象的构造函数中,设置一个实例对象的共有属性species. function DOG ...
- JS面向对象,创建,继承
很开心,最近收获了很多知识,而且发现很多东西,以前理解的都是错的,或者是肤浅的,还以为自己真的就get到了精髓,也很抱歉会影响一些人往错误的道路上走,不过这也告诉了我们,看任何一篇文章都不能盲目的去相 ...
随机推荐
- python基础-UDP、进程、进程池、paramike模块
1 基于UDP套接字1.1 介绍 udp是无连接的,是数据报协议,先启动哪端都不会报错 udp服务端 import socket sk = socket() #创建一个服务器的套接字 sk.bind( ...
- Codeforces 749E Gosha is hunting 二分+DP
很神奇的一题 看完题解不由惊叹 题意:$n$个神奇宝贝 $a$个普通球 $b$个高级球 普通球抓住$i$神奇宝贝的概率为$u[i]$ 高级球为$p[i]$ 一起用为$u[i]+p[i]-u[i]*p[ ...
- javascript 正则限制文本输入框只允许输入数字,简单实现。
<input type="text" id="memberId" lay-verify="title" autocomplete=&q ...
- Regex.Split
private static List<int> GetThemeIds(string themeList) { const string split = "!===!" ...
- 使用 IntraWeb (20) - 基本控件之 TIWGrid
TIWGrid 最终通过 Html Table 呈现; 其每个 Cell 都是一个 TIWGridCell 对象, Cell 对象的 Control 属性非常好, 可以非常方便地嵌入其他控件. TIW ...
- HDU 4818 RP problem (高斯消元, 2013年长春区域赛F题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4818 深深地补一个坑~~~ 现场赛坑在这题了,TAT.... 今天把代码改了下,过掉了,TAT 很明显 ...
- ZOJ 2702 Unrhymable Rhymes 贪心
贪心.能凑成一组就算一组 Unrhymable Rhymes Time Limit: 10 Seconds Memory Limit: 32768 KB Special Judge ...
- HDU 4568 SPFA + TSP
这道题是长沙邀请赛的题,当时是道签到题. 这种题还是很常见的,讲一下思路. 首先是预处理出每个宝藏之间的距离,还有到边的距离,直接对每个宝藏进行一次SPFA就可以了. 然后就是经典的求TSP的过程. ...
- delphi 使用工控机控件 iThreadTimes 出现问题, 导致主程序创建页面的时候, 阻塞消息, 不能正常执行。
delphi 使用工控机控件 iThreadTimes 出现问题, 导致主程序创建页面的时候, 阻塞消息, 不能正常执行. 使用这个控件需要小心 function Tfrm_MainIPC.Open ...
- 在简历中使用STAR法则
一.什么是STAR法则? The STAR (Situation, Task, Action, Result) format is a job interview technique used by ...