频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

javascript---类的继承

来源:qqread.com 作者:佚名 出处:巧巧读书 2008-07-09 进入讨论组
下一页 1 2 

    1.第一种方式,冒充对象的方式.(利用js里的每一个方法名都是一个Function对象) Java代码
    function Parent(username){
        this.username = username;

        this.say = function(){
            alert(this.username);
        }
    }

    function Child(username,password){

        this.temp = Parent;//temp指向Parent所指向的地方 。 利用js里的每一个方法名都是一个Function对象,指向一个方法。
        this.temp(username);//初始化方法里的内容
        delete this.temp;//temp没有用了。可以直接删除掉.this不可以丢了


        //Parent(username);//这样写表面看起来是正确的,其实是错误的。因为只有new出来的对象才有this,所以调用Parent里的this就没有值了


        this.password = password;
        this.hello = function(){
            alert(this.password);
        }
    }

    var parent = new Parent("zhangsan");
    parent.say();//zhangsan

    var child = new Child("lisi","123456");
    child.say();//lisi
    child.hello();//123456

    //第一种方式,冒充对象的方式.(利用js里的每一个方法名都是一个Function对象)
    function Parent(username){
        this.username = username;
        this.say = function(){
            alert(this.username);
        }
    }

    function Child(username,password){

        this.temp = Parent;//temp指向Parent所指向的地方 。 利用js里的每一个方法名都是一个Function对象,指向一个方法。
        this.temp(username);//初始化方法里的内容
        delete this.temp;//temp没有用了。可以直接删除掉.this不可以丢了


        //Parent(username);//这样写表面看起来是正确的,其实是错误的。因为只有new出来的对象才有this,所以调用Parent里的this就没有值了


        this.password = password;
        this.hello = function(){
            alert(this.password);
        }
    }

    var parent = new Parent("zhangsan");
    parent.say();//zhangsan

    var child = new Child("lisi","123456");
    child.say();//lisi
    child.hello();//123456

    2.第二种方式:call()函数方式 call()函数是Function对象的一个函数
具体用法如下 Java代码
    //call()函数是Function对象的一个函数
    //具体用法如
    function test(str){
        alert(this.username + ","  + str);
    }

    var o = new Object();
    o.username = "zhangsan";
    test.call(o,"123456");//zhangsan,123456 .因为每个Function 对象都有一个call()方法,而函数名就是一个Function对象.call()函数的第一个参数是test函数里的this.

    //call()函数是Function对象的一个函数
    //具体用法如
    function test(str){
        alert(this.username + ","  + str);
    }

    var o = new Object();
    o.username = "zhangsan";
 

进入讨论组讨论。
下一页 1 2 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章