+ -
当前位置:首页 → 问答吧 → js给看段代码,Div拖动

js给看段代码,Div拖动

时间:2011-10-20

来源:互联网

var aa={
say:function(){alert(12)}
}
aa.prototype={
say1:function(){
alert(125)
}
}
aa.say();
aa.say1();---这里说没有这个方法

作者: wang_137   发布时间: 2011-10-20

JScript code

var aa=function(){
 this.say=function(){alert(12)}
}
aa.prototype.say1=function(){
alert(125)
}
var b=new aa();
b.say();
b.say1()

作者: BLUE_LG   发布时间: 2011-10-20

该回复于2011-10-20 13:58:28被管理员删除

  • 对我有用[0]
  • 丢个板砖[0]
  • 引用
  • 举报
  • 管理
  • TOP
#3楼 得分:0回复于:2011-10-20 13:38:07
引用 1 楼 blue_lg 的回复:

JScript code

var aa=function(){
this.say=function(){alert(12)}
}
aa.prototype.say1=function(){
alert(125)
}
var b=new aa();
b.say();
b.say1()

我那样写该怎么改啊,因为我有好多方法要加,我不想每次都写,prototype

作者: bustersword   发布时间: 2011-10-20

var aa={};
aa.say=function(){};
aa.say1=function(){};
aa.say2=function(){};

作者: wang_137   发布时间: 2011-10-20

JScript code

function aa(){
    this.say=function(){alert(12)}
}
aa.prototype={
constructor:aa,
say1:function(){
alert(125)
}
}
var wori=new aa()
wori.say();
wori.say1();

作者: ifandui   发布时间: 2011-10-20

楼主理解错prototype了。。只有构造函数才有prototype原型
刚才你
aa.prototype={
say1:function(){
alert(125)
}

只是给aa添加了一个名为prototype的成员变量
var aa={
say:function(){alert(12)}
}

如果想添加say1方法,只需添加一个名为say1的成员变量就可以

aa.say1 = functino(){
  //dosth
}

作者: ycmjh2010   发布时间: 2011-10-20

JScript code
var moveBox={
        version:1.0,
        source:null,
        movable:false,//movable
        mousePosition:null,
        mpSource:null,//mouse position to source
        Client:function(s){//bound element
            moveBox.source=$("#"+s);
            this.register(this);
        }
    }
    moveBox.Client.prototype ={
        getMPSource:function(e){//mouse position to source
            return {
                     rx:e.offsetX?e.offsetX:e.layerX,
                     ry:e.offsetY?e.offsetY:e.layerY
                    }
        },
        getMousePosition:function(e){
            if(e.pageX || e.pageY){
                 return {x:e.pageX, y:e.pageY};
              }
              return {
                   x:e.clientX + document.body.scrollLeft - document.body.clientLeft,
                   y:e.clientY + document.body.scrollTop  - document.body.clientTop
               }
        },
        register:function(o){
            moveBox.source.mousedown(function(e){
                moveBox.movable=true;
                moveBox.mpSource=o.getMPSource(e);
            });
            moveBox.source.mouseup(function(e){
                moveBox.movable=false;
                moveBox.mpSource=null;
            });
            moveBox.source.mousemove(function(e){
                if(moveBox.movable){
                    moveBox.mousePosition=o.getMousePosition(e);
                    moveBox.source.css("left",moveBox.mousePosition.x-moveBox.mpSource.rx);
                    moveBox.source.css("top",moveBox.mousePosition.y-moveBox.mpSource.ry);
                }
            });
            $(document).mouseup(function(){
                if(moveBox.movable)
                    moveBox.movable=false;
            });
        }
    }
var client=new moveBox.Client("move");

终于完成了,兼容浏览器,但感觉写的有点别扭
贴出来只是希望大牛们能改的优雅些

作者: liangws   发布时间: 2011-10-20

楼主的代码里面aa是一个实例,并不是一个构造函数。有两个方法可以改正楼主的代码:
JScript code

var aa={
 say:function(){alert(12);}
};
aa.__proto__.say1=function()
{
    alert(125);
}
aa.say();
aa.say1();


或者
JScript code

function aa()
{
    this.say = function(){alert(12);}
}
aa.prototype={
 say1:function(){
 alert(125);
 }
};
var a = new aa();
a.say();
a.say1();


具体楼主可以看看关于原型的内容。

作者: wang_137   发布时间: 2011-10-20

aa 是对象 没有prototype指针吧
只有方法才有这个

作者: Legend1988   发布时间: 2011-10-20