//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面向对象之继承-原型继承的更多相关文章

  1. Js 面向对象之封装,继承,原型,原型链

    封装 ,继承 ,原型, 原型链 封装 ? 面向对象有三大特性,封装.继承和多态.对于ES5来说,没有class(类)的概念,并且由于JS的函数级作用域(函数内部的变量在函数外访问不到),所以我们就可以 ...

  2. js面向对象设计之class继承

    EcmaScript 2015 (又称ES6)通过一些新的关键字,使类成为了JS中一个新的一等公民.但是目前为止,这些关于类的新关键字仅仅是建立在旧的原型系统上的语法糖,所以它们并没有带来任何的新特性 ...

  3. JS面向对象(封装,继承)

    在六月份找工作中,被问的最多的问题就是: js面向对象,继承,封装,原型链这些,你了解多少? 额,,,我怎么回答呢, 只能说,了解一些,不多不少,哈哈哈哈,当然,这是玩笑话. 不过之前学过java,来 ...

  4. JS面向对象(二)---继承

    一.面向对象的继承 1.解析:在原有对象的基础上,略作修改,得到一个新的对象,并且不影响原有对象的功能 2.如何添加继承---拷贝继承 属性:call 方法: for in /* 继承:子类不影响父类 ...

  5. js面向对象(构造函数与继承)

    深入解读JavaScript面向对象编程实践 Mar 9, 2016 面向对象编程是用抽象方式创建基于现实世界模型的一种编程模式,主要包括模块化.多态.和封装几种技术. 对JavaScript而言,其 ...

  6. js原生设计模式——2面向对象编程之继承—原型继承(类式继承的封装)

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  7. js面向对象的程序设计 --- 下篇 继承启蒙

    继承是oo语言中一个最为人津津乐道的概念.ECMAScript支持实现继承,而且实现继承只要是靠原型链来实现的 ·原型链 其基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法. 简单回顾一 ...

  8. JS继承,原型继承,构造函数的继承,非构造函数"的继承

    a.原型继承 一.new运算符的缺点 用构造函数生成实例对象,有一个缺点,那就是无法共享属性和方法.比如,在DOG对象的构造函数中,设置一个实例对象的共有属性species. function DOG ...

  9. JS面向对象,创建,继承

    很开心,最近收获了很多知识,而且发现很多东西,以前理解的都是错的,或者是肤浅的,还以为自己真的就get到了精髓,也很抱歉会影响一些人往错误的道路上走,不过这也告诉了我们,看任何一篇文章都不能盲目的去相 ...

随机推荐

  1. 4951: [Wf2017]Money for Nothing 决策单调性 分治

    Bzoj4951:决策单调性 分治 国际惯例题面:一句话题面:供应商出货日期为Ei,售价为Pi:用户收购截止日期为Si,收购价格为Gi.我们要求max((Si-Ej)*(Gi-Pj)).显然如果我们把 ...

  2. HDU.5985.Lucky Coins(概率DP)

    题目链接 \(Description\) 有n(n<=10)种硬币,已知每种硬币的数量和它抛一次正面朝上的概率pi.进行如下过程:每次抛一次所有硬币,将正面朝下的硬币去掉.重复该过程直到只剩一种 ...

  3. 洛谷P1133 教主的花园

    题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树 ...

  4. Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装

    阅读目录 1. 通过HttpServletRequest获得请求参数和数据 2. 处理方法形参名==请求参数名 3. 如果形参名跟请求参数名不一样怎么办呢?用@RequestParam注解 4. 用实 ...

  5. LPC43xx SGPIO Camera interface design

    AN11196: Camera interface design using SGPIO

  6. STM32F4 SPI with DMA

    STM32F4 SPI with DMA A few people have requested code, so I thought I’d post the code showing how I’ ...

  7. JTAG TAP Controller

    The TAP controller is a synchronous finite state machine that responds to changes at the TMS and TCK ...

  8. STM32 UART DMA实现未知数据长度接收

    串口通信是经常使用到的功能,在STM32中UART具有DMA功能,并且收发都可以使用DMA,使用DMA发送基本上大家不会遇到什么问题,因为发送的时候会告知DMA发送的数据长度,DMA按照发送的长度直接 ...

  9. JTAG – A technical overview and Timing

    This document provides you with interesting background information about the technology that underpi ...

  10. LINUX 内核守护进程

    http://alfred-sun.github.io/blog/2015/06/18/daemon-implementation/