论坛首页 Web前端技术论坛

JavaScript内核系列 第11章 客户端JavaScript

浏览 8190 次
精华帖 (2) :: 良好帖 (3) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-05-11  
跟着LZ的笔记上来的,收益非浅啊,感谢LZ奉献出自己的智慧帮助其他人~~
0 请登录后投票
   发表时间:2011-05-12  
问楼主一个困扰我很久的问题
var User=function(arguObj){
    this.id=arguObj.id;
    this.name=arguObj.name;
}

user.prototype.showUserInfo=function(id){
return "";
}

user.prototype.getDate=function(){
    $.ajax({
            url: "user.spr?action=getDate",
            type: 'get',
            data: null,
            success: function(date){
                  //这里怎么调用user的showUserInfo(id);
            })
        })
  }

//这里怎么调用user的showUserInfo(id);
在ajax内如何调用外部引用他的对象
0 请登录后投票
   发表时间:2011-05-12  
fywxin 写道
问楼主一个困扰我很久的问题
var user=function(arguObj){
    this.id=arguObj.id;
    this.name=arguObj.name;
}

user.prototype.showUserInfo=function(id){
return "";
}

user.prototype.getDate=function(){
    $.ajax({
            url: "user.spr?action=getDate",
            type: 'get',
            data: null,
            success: function(date){
                  //这里怎么调用user的showUserInfo(id);
            })
        })
  }

//这里怎么调用user的showUserInfo(id);
在ajax内如何调用外部引用他的对象


这些方法为什么要写在user.prototype上呢?user.getData() is just fine.如果要使用OO,建议使用base包。
0 请登录后投票
   发表时间:2011-05-12  
base包指的是什么呀?
0 请登录后投票
   发表时间:2011-05-12  
guoqingcun 写道
base包指的是什么呀?


http://dean.edwards.name/weblog/2006/03/base/

在“内核”第八章讨论过。很不错的OO包装。
0 请登录后投票
   发表时间:2011-05-13  
abruzzi 写道
fywxin 写道
问楼主一个困扰我很久的问题
var user=function(arguObj){
    this.id=arguObj.id;
    this.name=arguObj.name;
}

user.prototype.showUserInfo=function(id){
return "";
}

user.prototype.getDate=function(){
    $.ajax({
            url: "user.spr?action=getDate",
            type: 'get',
            data: null,
            success: function(date){
                  //这里怎么调用user的showUserInfo(id);
            })
        })
  }

//这里怎么调用user的showUserInfo(id);
在ajax内如何调用外部引用他的对象


这些方法为什么要写在user.prototype上呢?user.getData() is just fine.如果要使用OO,建议使用base包。



var user=function(arguObj){
    this.id=arguObj.id;
    this.name=arguObj.name;
}

user.prototype={
    showUserInfo : function(){
         ....
    },

    getDate : function(){
        .....
    },

    N多方法。。。
}

这样写在结构上是不是会好些,将方法放到直接对象之下如:user.getData() 和放到prototype原型之下如:user.prototype.getData(),两种方式楼主可以做个详细的比较吗,如性能,方法调用是否有区别,哪种更好等方面介绍下,可以吗?

还有之前的那个问题,在javascript对象里的方法中的ajax方法体内如何调用对象的方法,this关键字不行,总不能用new出来的对象去调用那方法吧。

user.prototype.getDate=function(){
    $.ajax({
            url: "user.spr?action=getDate",
            type: 'get',
            data: null,
            success: function(date){
                  //这里怎么调用user的showUserInfo(id);
            })
        })
  }

呵呵,javascript只知道皮毛怎么用,但不知道为什么要那样,希望楼主指点迷津~
0 请登录后投票
   发表时间:2011-05-23  
,断续关注.
0 请登录后投票
   发表时间:2011-06-26  
fywxin 写道
问楼主一个困扰我很久的问题
var User=function(arguObj){
    this.id=arguObj.id;
    this.name=arguObj.name;
}

user.prototype.showUserInfo=function(id){
return "";
}

user.prototype.getDate=function(){
    $.ajax({
            url: "user.spr?action=getDate",
            type: 'get',
            data: null,
            success: function(date){
                  //这里怎么调用user的showUserInfo(id);
            })
        })
  }

//这里怎么调用user的showUserInfo(id);
在ajax内如何调用外部引用他的对象

根据楼主的第十章解释:
this与执行期上下文有关,那么这个success方法是谁调用的呢?是$.ajax调用的,那么它有showUserInfo这个方法吗?没有,所以你调用不了,
要调用怎么办?不是有scope chain吗,让$.ajax的活动对象可以找到showUserInfo这个自由变量就可以了啊,而$.ajax的活动对象会指向User的getDate的活动对象,因此你在getDate内加一个变量var showUserInfo = this.showUserInfo();这里的this是指向user的,就OK了,这样在success里面直接调用showUserInfo()

方法都写在User.prototype里面才对啊,User的对象就可以自动找到这些方法了
0 请登录后投票
   发表时间:2011-06-30  
写这么篇文章的目的何在呢?
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics