Chinaunix首页 | 论坛 | 博客
  • 博客访问: 528245
  • 博文数量: 855
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-16 19:08
文章分类

全部博文(855)

文章存档

2011年(1)

2008年(854)

我的朋友

分类:

2008-10-16 19:25:13

    1.第一种方式,冒充对象的方式.(利用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

    //第一种方式,冒充对象的方式.(利用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对象的一个函数
具体用法如下 代码
    //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]  

【责编:landy】

--------------------next---------------------

阅读(376) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~