//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. 获取设备IP地址

    腾讯的IP地址API接口地址:http://fw.qq.com/ipaddress返回的是数据格式为: 1 var IPData = new Array(“58.218.198.205″,”" ...

  2. AutoFac简单入门

    AutoFac是.net程序下一个非常灵活易用,且功能强大的DI框架,本文这里简单的介绍一下使用方法. 安装: Install-Package Autofac 简单的示例: static void M ...

  3. JS删除String里某个字符的方法

    关于JS删除String里的字符的方法,一般使用replace()方法.但是这个方法只会删除一次,如果需要将string里的所以字符都删除就要用到正则. 1 2 3 4 var str = " ...

  4. OpenAI Gym

    https://blog.openai.com/openai-gym-beta/ https://openai.com/

  5. Plan Explorer数据库

    Plan Explorer数据库 https://www.sentryone.com/platform/sql-server-performance-monitoring

  6. PHP扩展迁移为PHP7扩展兼容性问题记录

    PHP7扩展编写的时候,提供的一些内核方法和之前的PHP之前的版本并不能完全兼容.有不少方法参数做了调整.下面是在迁移过程中遇到的一些问题.记录下来,避免大家再踩坑. add_assoc_string ...

  7. C#写的COM组件注册问题兼论微软Regasm注册的BUG

    工作中自己用C#写了专门读写EXCEL(不需要OFFICE环境,直接读原始文件,速度快)的COM组件,在使用过程中,发现原先的注册程序是有问题的.网上也有同样的网友碰到这个问题,但都没找到合适的解决办 ...

  8. Quartz.NET简介

    Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...

  9. Android 之窗口小部件高级篇--App Widget 之 RemoteViews

    Android 之窗口小部件高级篇--App Widget 之 RemoteViews 在之前的一篇博文(Android 之窗口小部件详解--App Widget)中,已经介绍了App Widget的 ...

  10. C#编程(五十四)----------Lookup类和有序字典

    原文链接: http://blog.csdn.net/shanyongxu/article/details/47071607 Lookup类 Dictionary<Tkey,TValue> ...