function Animal(){}
Animal.prototype={
name:"animal",
toString:function(){
console.log(this.name);
}
};
Animal.prototype.constructor=Animal;
function Dog(){}
//用于打破对象的引用传递,防止修改子类属性对父类产生影响
var F=function(){}
F.prototype=Animal.prototype
Dog.prototype=new F();
Dog.prototype.constructor=Dog;
Dog.prototype.name="dog";
var d=new Dog();
d.toString(); //打印子类name dog
var a=new Animal();
a.toString();//打印父类name animal

上面代码通过实例化子类和父类,分别调用toString()实现了继承的关系。

这个时候有这样的需求;不实例化父类,直接通过子类完完整整的调用父类的方法或属性。

实现代码如下

function Animal(){}
Animal.prototype={
name:"animal",
toString:function(){
console.log(this.name);
}
};
Animal.prototype.constructor=Animal;
function Dog(){}
//用于打破对象的引用传递,防止修改子类属性对父类产生影响
var F=function(){}
F.prototype=Animal.prototype
Dog.prototype=new F();
Dog.prototype.constructor=Dog;
Dog.prototype.name="dog";
Dog.uber=Animal.prototype;
var d=new Dog();
d.toString(); //打印子类name dog
//var a=new Animal();
//a.toString();//打印父类name animal /**
* Dog.prototype.constructor===d.constructor
*/
Dog.prototype.constructor.uber.toString();
//打印animal(方式1)
d.constructor.uber.toString(); //打印animal(方式2)

通过面简单的三行红色代码就实现了子类访问父类成员的需求。

本来想模仿java的使用super访问父类,后来想想super是javascript的关键字。

javascript中uber实现子类访问父类成员的更多相关文章

  1. Java子类访问父类的私有成员变量

    /**子类会继承父类所有的属性和方法. * 但是根据不同的权限标识符,子类不可见父类的私有变量,但可以通过父类的公共方法访问私有变量 * 所以对于重名变量,子类和父类都各有一份. * 对于子类和父类中 ...

  2. 转:C++:从子类访问父类的私有函数

    众所周知,c和c++的数组都是不安全的,因为无论c还是c++都不提供数组边界检查功能,这使得数组溢出成为可能.从某个意义上说,c和c++是一种缺少监督的语言,然而这也正是其魅力所在.c++给予程序员更 ...

  3. super关键字访问父类成员

    1.super只能出现在子类的方法和构造方法中: 2.super调用构造方法时只能是第一句: 3.super不能访问父类的private成员.

  4. c++子类和父类成员函数重名

    子类和父类返回值参数相同,函数名相同,有virtual关键字,则由对象的类型决定调用哪个函数. 子类和父类只要函数名相同,没有virtual关键字,则子类的对象没有办法调用到父类的同名函数,父类的同名 ...

  5. public、potected 、private继承下的子类对父类成员的访问情况

    #include<iostream> #include<string> using namespace std; class parent{ protected: int m_ ...

  6. 【C++】子类访问父类typedef的问题

    class A { public: typedef int* pointer; }; class B :public A { public: pointer b; }; 这段代码运行没有问题,子类继承 ...

  7. 子类覆写的变量被private隐藏,强制转换方式通过子类访问父类的被覆写变量:

    import static java.lang.System.*; public class SuperParent{ public static void main(String[] args){ ...

  8. JavaScript中的数据属性和访问器属性

    在学习JavaScript原型(prototype)和原型链(prototype chain)知识的时候,发现数据属性和访问器属性的重要性,通过不断的查找相关知识,浅显理解如下,若有差错,希望不吝赐教 ...

  9. JavaScript中Get和Set访问器的实现

    我们常用的实现方法可能是这样的: function Field(val){ var value = val; this.getValue =function(){ return value; }; t ...

随机推荐

  1. 数据库备份还原——mysqlbackup与mysqldump对比测试

    1      环境描述 1.1      硬件环境 服务器类型:华为RH5885 IP: 10.148.128.100 内存: 64G 物理CPU个数:4 CPU核数:8 逻辑CPU个数:64 Int ...

  2. 04_Spring AOP两种代理方法

    什么是AOP?           AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是O ...

  3. 延迟对象deferred

    Twisted 官方称,“Twisted is event-based, asynchronous framework ”.这个“异步”功能的代表就是 deferred. deferred 的作用类似 ...

  4. 省际联动distpicker插件的使用讲解

    1.在使用input页面加载script的引用 <script src="js/distpicker/distpicker.data.js"></script&g ...

  5. Linux 命令之head, tail, tr, sort, uniq, grep

    head [filename] head -n 11 [filename] -> First 11 lines head -c 20 [filename] -> First 20 char ...

  6. Java中gson的使用

    转https://www.cnblogs.com/qinxu/p/9504412.html

  7. TZOJ 5962 Happy Matt Friends(计数DP)

    描述 Matt hzs N friends. They are playing a game together. Each of Matt’s friends has a magic number. ...

  8. KMP模板题 Number Sequence HDU1711

    模板...嗯 #include <iostream> #include <cstdio> #include <string.h> #pragma warning ( ...

  9. Django项目:CRM(客户关系管理系统)--22--14PerfectCRM实现King_admin分页的省略显示

    {#table_data_list.html#} {## ————————08PerfectCRM实现King_admin显示注册表的字段表头————————#} {% extends 'king_m ...

  10. bzoj 2935 [Poi1999]原始生物——欧拉回路思路!

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2935 有向图用最小的路径(==总点数最少)覆盖所有边. 完了完了我居然连1999年的题都做不 ...