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到了精髓,也很抱歉会影响一些人往错误的道路上走,不过这也告诉了我们,看任何一篇文章都不能盲目的去相 ...
随机推荐
- 代理设置。 安卓工作室配置用http代理。gradle可能需要这些http代理设置去访问互联网。例如下载依赖。 你想要复制ide的代理配置到这个项目的gradle属性文件吗?
代理设置. 安卓工作室配置用http代理.gradle可能需要这些http代理设置去访问互联网.例如下载依赖. 你想要复制ide的代理配置到这个项目的gradle属性文件吗? 查看更多细节,请参阅开发 ...
- Codechef September Challenge 2018 游记
Codechef September Challenge 2018 游记 Magician versus Chef 题目大意: 有一排\(n(n\le10^5)\)个格子,一开始硬币在第\(x\)个格 ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心
D. Generating Sets 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set ...
- android - Animation详解
Drawable 最强大的功能是:显示Animation.AndroidSDK介绍了2种Animation: Tween Animation(渐变动画):通过对场景里的对象不断做图像变换(平移.缩放. ...
- 推荐一个文献翻译软件--Deja Vu X
首先我的这篇博客推荐的软件并非你觉得翻译精确度有多高的软件,假设是这种话就不用往下看了,免得浪费时间,仅仅是一个对于翻译文献非常方便的工具,方面在哪请看下文. 我是不会告诉你凡事用过这个软件的人都说好 ...
- linux后台开发核心技术
3. 常用STL的使用 3.1. string (1)string类的实现(使用strlen.strcpy.strcat.strcmp等,注意判NULL). (2)C++字符串和C字符串的转换:dat ...
- ECSHOP商城网站建设之自定义调用广告方法(二)
原文地址:http://www.cnblogs.com/zgzy/p/3598991.html 使用ecshop进行商城网站建设时,ecshop默认的很多功能对于我们个性化设计之后不太使用.今天我们主 ...
- smartsvn学习(二)如何在Xcode下使用SVN
1.Xcode4中苹果有自带的SVN软件------>Organizer------>Repositories 2.SVN checkout到本地后,删除本地file,对服务器有影响吗 ...
- oracle监听1067错误的处理
一,oracle监听1067错误的处理修改oracle安装目录D:\DataBase\oracle\product\10.1.0\Db_1\NETWORK\ADMIN\下的 listener.ora和 ...
- sendto 和 recvfrom 函数
sendto recvfrom