网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 JavaScript的继承
内容
    prototype 属性的作用:
    利用prototype 属性提供对象的类的一组基本功能。对象的新实例“继承”赋予该对象原型的操作。
    prototype 属性的功能:
    所有JavaScript 内部对象都有只读的prototype 属性。可以为内部对象的原型添加功能,但该对象不能被赋予不同的原型。
    然而,用户定义的对象可以被赋给新的原型。
    constructor 属性的作用:
    constructor 表示创建对象的函数。
    constructor 属性的功能:
    constructor 属性是所有具有 prototype 的对象的成员。它们包括除 Global 和 Math 对象以外的所有 JavaScript 内部对象。
    constructor 属性保存了对构造特定对象实例的函数的引用。
    A 利用prototype 添加对象的属性 [ 方式一]
    示例:
    <script type="text/javascript">
    //方式一
    var myObj = function(){
    this.study = "JavaScript";
    }
    myObj.prototype.hobby = function()
    {
    this.hobby = "See girl";
    }
    var newObj = new myObj();
    for ( var attr in newObj )
    {
    document.write( attr +"<br/>" );
    }
    </script>
    B 利用prototype 添加对象的属性 [ 方式二]
    示例:
    <script type="text/javascript">
    //方式二
    var superObj = { name:"xugang" };
    var subObj = { age:20 };
    function extend(superObj,subObj){
    //获得父对象的原型对象
    subObj.getSuper = superObj.prototype;
    //将父对象的属性给子对象
    for(var i in superObj){
    subObj[i] = superObj[i];
    }
    }
    extend(superObj,subObj);
    for ( var s in subObj )
    {
    document.write( s +"<br/>" ); //遍历子对象的属性
    }
    </script>
    C 利用prototype 继承父类的原型属性
    示例:
    <script>
    function Person(_name){
    this.name = _name;
    }
    //创建对象(用于更改 prototype 原型对象)
    function addSex(_sex){
    this.sex = _sex;
    }
    //更改原型对象
    Person.prototype = new addSex('男');
    var p = new Person('xugang');
    alert("p 的原型为:" + p.constructor);
    //打印所有属性
    for(var i in p){
    //alert(p[i]);
    }
    // ================= 继承 =================
    //创建子对象 Student
    function Student(_study){
    this.study = _study;
    }
    // 通过 prototype 让 Student 继承 Person
    Student.prototype = new Person('刘德华');
    var stu1 = new Student('JS');
    alert("stu1 的原型为:" + stu1.constructor);
    for(var i in stu1){
    alert(stu1[i]);
    }
    </script>
    因为Student 对象的原型更改为Person 对象,而Person 对象的原型更改为addSex ,所以,Student 对象的原型为addSex 。
    注意:一个对象的原型是在 new 对象的那一刻确定的,如果在 new 对象以后更改无效!
    D 如何设置对象的原型对象和构造函数
    示例:
    <script type="text/javascript">
    function B(){
    this.name = "刘德华";
    return "B方法";
    }
    function C(){
    this.age = 42;
    return "C方法";
    }
    B.prototype = new C();
    var b = new B();
    b.constructor = B; //重写b的构造方法为B本身
    document.write("b 的构造方法:");
    document.write(b.constructor() + "<br/>");
    document.write("b 的原型对象的构造方法:");
    document.write(b.constructor.prototype.constructor() + "<br/>");
    for ( var m in b )
    {
    document.write("属性:" + m );
    document.write(" 值:" + b[m] +"<br/>");
    }
    </script>
    结果如下:
    b 的构造方法:B方法
    b 的原型对象的构造方法:C方法
    属性:age 值:42
    属性:name 值:刘德华
    E 对象中用来保存原型的 __proto__ 变量
    示例:
    <script type="text/javascript">
    function myObject(){}
    var my = new myObject();
    //任何对象都会有一个隐藏的 __proto__ 变量用来保存原型
    document.write(my.__proto__ + "<br/>");
    //在 Internet Explorer 下显示为:undefined
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/21 3:10:11