+ -
当前位置:首页 → 问答吧 → ■■■ JS 传参 (大疑问) ■■■

■■■ JS 传参 (大疑问) ■■■

时间:2011-12-08

来源:互联网


■■■■■■■■■■■■■■■■ JS 传参 (大疑问) ■■■■■■■■■■■■■■■■■■■■■■■■

问题起因:
  以前,以过程方试写js 没有此类问题,
  自从以(类,对象)的方式来构造JS代码时,发现了一个大问题。
问题实例:
这里是动态事件代码 可以不看:
JScript code
////////////////这里是动态事件代码///////////////////////////////////////////////////
var AboutEvent = function(){
//增加 事件 function
    this.addEventHandler=
    function (oTarget, sEventType, fnHandler) {
        if (oTarget.addEventListener) {
            oTarget.addEventListener(sEventType, fnHandler, false);
        } else if (oTarget.attachEvent) {
            oTarget.attachEvent("on" + sEventType, fnHandler);
        } else {
            oTarget["on" + sEventType] = fnHandler;
        }
    };
    //移除 事件 function
    this.removeEventHandler = function (oTarget, sEventType, fnHandler) {
        if (oTarget.removeEventListener) {
            oTarget.removeEventListener(sEventType, fnHandler, false);
        } else if (oTarget.detachEvent) {
            oTarget.detachEvent("on" + sEventType, fnHandler);
        } else { 
            oTarget["on" + sEventType] = null;
        }
    };
    //用法如下
    //addEventHandler(document, "mousemove", f1);
    //addEventHandler(document, "mouseup", f2);
};

JScript code

////////////////////下面是我的问题所在////////////////////////////////////
var _Point = function(x, y) {
    this.x;
    this.y;

    this.F_Point = function() {
        var MoveEvent=new AboutEvent();
        MoveEvent.addEventHandler(document.body, "mouseup", this.DocumentMouseUp);

    };
    this.DocumentMouseUp=function(ev){
        ev = ev || window.event;         // 事件
        ////////下面的代码有问题:
        this.x=ev.clientX;
        this.y=ev.clientY;
        //本来我以为this 是指(_Point类 )的, 其实不是;
        //this 指的是 当前MouseUp 的对象,如果我的鼠标在 <div id="div1" Name="Jim"/> 处MouseUp,
        //那么this 就是指向 div1。
        //我可以如下取得
            alert(this.Name);
        //那么 此时 弹出一个对话框,里面的值是  Jim   
        
///////////////但是我想调用(_Point类 )中的东西,如下:
this.x ; //我想调用它 (这就是我问的问题)
this.y ; //我想调用它 (这就是我问的问题)
    };
}

结果语:
  在家是如何 调用 下面这个的?
JScript code
this.x ; //我想调用它 (这就是我问的问题)
this.y ; //我想调用它 (这就是我问的问题)


谢谢回复



■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

作者: hetengfei_   发布时间: 2011-12-08

_Point.x这样不能调用吗?

作者: zsx841021   发布时间: 2011-12-08

引用 1 楼 zsx841021 的回复:

_Point.x这样不能调用吗?

现在就试试。

作者: hetengfei_   发布时间: 2011-12-08

引用 1 楼 zsx841021 的回复:

_Point.x这样不能调用吗?

先var a=new _Point();

作者: zsx841021   发布时间: 2011-12-08

引用 2 楼 hetengfei_ 的回复:
引用 1 楼 zsx841021 的回复:
_Point.x这样不能调用吗?
现在就试试。

这样不行啊,

先var a=new _Point();  
然后 alert(a.x ); ?
因为 我声明 时 this.x
那么 this.x 就是Js 的私有对象。
所以,这个也是不行啊。

作者: hetengfei_   发布时间: 2011-12-08

JScript code
        var _Point = function(x, y) {
            //缓存this
            var _this = this;
            
            this.x;
            this.y;
        
            this.F_Point = function() {
                var MoveEvent=new AboutEvent();
                MoveEvent.addEventHandler(document.body, "mouseup", this.DocumentMouseUp);
        
            };
            
            // DocumentMouseUp只是个辅助函数,没必要暴露给外面
            function DocumentMouseUp(ev){
                ev = ev || window.event;         // 事件
                ////////下面的代码有问题:
                _this.x=ev.clientX;
                _this.y=ev.clientY;
                alert(_this.Name);
                _this.x ; //我想调用它 (这就是我问的问题)
                _this.y ; //我想调用它 (这就是我问的问题)
            };
        }

作者: axiheyhey   发布时间: 2011-12-08

楼上的行不行啊?

作者: semanwmj   发布时间: 2011-12-08