<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type=text/javascript charset=utf-8>
function extend(Child ,Parent){
var F = new Function();//创建一个函数F,跟写 function F(){};一样
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child ; //保存一下父类的原型对象: 一方面方便解耦 另一方面方便获得父类的原型对象
Child.superClass = Parent.prototype; //自定义一个子类的静态属性 保存父类的原型对象,写 Child.superClass = Parent也可以。保证子类中可以找到父类。
alert(Parent.prototype.constructor);
alert(Parent);
/*function Person( name , age){
this.name = name ;
this.age = age ;
}*/
alert(Parent.prototype.constructor === Parent);//true
//判断父类的原型对象的构造器 (加保险),Person.prototype = {中不写constructor: Person ,那么Parent.prototype.constructor就是Object
if(Parent.prototype.constructor == Object.prototype.constructor){
Parent.prototype.constructor = Parent ; //容错性,就是写出你不知道的情形。
}
} function Person( name , age){
this.name = name ;
this.age = age ;
} Person.prototype = {
sayHello: function(){
alert('hello world!');
}
};
Object.defineProperty(Person.prototype , 'constructor' , {
enumerable : false ,
value : Person
});
Person.prototype.say = function(){
alert('hello world!');
} function Boy(name , age , sex){
//call,子类就可以直接new Boy(传3个参数),否则只能通过父类实例对象来访问添加的属性。
//Person.call(this,name,age); Boy.superClass.constructor=Person,解耦,子类中不写父类的东西。
Boy.superClass.constructor.call(this , name , age);
this.sex = sex ;
}
extend(Boy , Person);
Boy.prototype.sayHello = function(){//覆盖父类的方法
alert('hi javascript!');
} var b = new Boy('张三' , 20 , '男');
alert(b.name); //张三
alert(b.sex);//男
b.sayHello();//hi javascript!
//调用父类方法
Boy.superClass.sayHello.call(b);//hello world!
b.__proto__.__proto__.sayHello();//hello world!
b.constructor.prototype.__proto__.sayHello()//hello world!
alert(Boy.superClass.constructor);
/*function Person( name , age){
this.name = name ;
this.age = age ;
}*/
</script>
</head>
<body>
</body>
</html>
第一,子类调用call方法。
第二,子类原型对象的构造函数要改变,原型对象和自己对象的构造器指向函数本身(函数本身的prorotype和对象的__proto__指向原型对象)。
第三,子类增加一个静态属性指向父类的原型对象,办证子类可以找到父类。
第四,子类原型对象新增属性要写在creat之后,因为creat之后子类才有了原型对象。 function F(){
this.name = "yw";
var age = 32;
sch = 890;
} var f = new F();
alert(f.name);//yw
alert(f.age);//undefined
alert(f.sch);//undefined
alert(window.name);//空
F();
alert(window.name);//yw var F = new Function();//创建一个函数F,跟写 function F(){};一样 //可以直接写constructor,prototype,不能写__proto__,_proto__是为了表示对象是怎么找到类的原型对象的属性方法的。

子类通过prototype访问父类原型对象的属性、方法。

子类通过call访问父类实例对象的属性、方法。不加call方法,访问不到父类实例对象的属性、方法。

父类和对象以静态方式加的属性、方法,子类对象访问不到。

js19--继承终极版本的更多相关文章

  1. HighCharts终极版本

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  2. day 50 jquary 终极版本

    jQuary 一.jquary对象和dom对象 jquary找到的标签对象成为-- jquary对象 原生js找到的标签对象成为 -- dom对象 dom对象只能使用dom对象的方法,不能使用jque ...

  3. 10maven依赖继承、统一版本/编码

    A >  B  >  C A依赖于B,B依赖于C,如果A想间接依赖C,那么B和C之间的依赖范围必须是compile,不然A依赖不了C 但是有点麻烦,因为每次A想依赖于C都要确认B和C之间的 ...

  4. c++多继承布局

    1:多重继承 对于一个继承了多个base class 的对象,将其地址指定给最左端(也就是第一个)base class的指针, 情况将和单一继承时相同,因为两者都指向相同的其实地址.至于第二个或者更后 ...

  5. 面向对象的JavaScript系列二,继承

    1.原型链 function SuperType(){ this.property = true; } SuperType.prototype.getSuperValue = function(){ ...

  6. 轻松搭建自己的Linux发行版本

    许多人想要搭建自己的Linux发行版本,可能是觉得有趣,也可能是为了学习更多的Linux知识,或者因为他们有很正式的问题要解决.但是秘密是:自己搭建完美的发行版本不是很困难的一件事.事实上,我们收集了 ...

  7. JF厂V8版本爱彼AP15703,黄家橡树离岸型,超越N厂神器

    根据调查的结果JF厂的爱彼AP15703几乎常年垄断了爱彼的市场,销量持续性的排在爱彼整个品牌中的第一位.JF厂这两年一直在攻克爱彼整个品牌,有了解的都知道 爱彼15703以前是N厂的五大复刻神器的代 ...

  8. 阿里正式发布《Java开发手册》终极版!

    摘要: 本文讲的是阿里正式发布<Java开发手册>终极版!,别人都说我们是码农,但我们知道,自己是个艺术家.也许我们不过多在意自己的外表和穿着,但我们不羁的外表下,骨子里追求着代码的美.质 ...

  9. 有关HTML版本

    先说说HTML的简史:从HTML1.0~2.0(1989~1991)>HTML3(1995)>HTML4(1998)>HTML4.01(1999)>XHTML1.0(2001) ...

随机推荐

  1. XML和Schema命名空间详解

    来源:https://blog.csdn.net/wanghuan203/article/details/9204337 XML和Schema具有无关平台,技术厂商,简单,规范统一等特点,极具开放性, ...

  2. 洛谷 P1302 可见矩形

    P1302 可见矩形 题目描述 给定平面上n个互不相交(指公共面积为零)的正方形,它们的顶点坐标均为整数.设坐标原点为O(0, 0).对于任一正方形R,如果可以找到R的边上2个不同的点A和B,使三角形 ...

  3. [Recompose] Refactor React Render Props to Streaming Props with RxJS and Recompose

    This lesson takes the concept of render props and migrates it over to streaming props by keeping the ...

  4. [LeetCode]Single Number 异或的妙用

    1.数组中仅仅有一个元素仅仅出现一次,其余出现偶数次. 利用异或中同样元素相互抵消的方式求解. 2.数组中仅仅有2个元素仅仅出现一次.其余出现偶数次. class Solution { public: ...

  5. nyoj--20--吝啬的国度(搜索dfs)

    吝啬的国度 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市,他有 ...

  6. Day2代码

    #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #defi ...

  7. PullToRefreshListView中嵌套ViewPager滑动冲突的解决

    PullToRefreshListView中嵌套ViewPager滑动冲突的解决 最近恰好遇到PullToRefreshListView中需要嵌套ViewPager的情况,ViewPager 作为头部 ...

  8. 洛谷—— P1328 生活大爆炸版石头剪刀布

    https://www.luogu.org/problem/show?pid=1328 题目描述 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负.在< ...

  9. jquery事件 【mousedown与mouseup ----keydown与keypress与keyup】focus--blur--orrer--pageX-pageY

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> ...

  10. tcp_tw_recycle检查tcp_timestamps的内核代码

    注意:本文档中的内核代码的版本号:linux-4.0.5 /************************************************* * Author : Samson * ...