function Point(x, y, color)
{
this.xLabel = x;
this.yLabel = y;
this.color = color;
this.DrawPoint() = function(x,y,color){
this.xLabel = x;
this.yLabel = y;
this.color = color;
document.write('<div style="position:absolute;top:'+ this.yLabel +'px;left:'+ this.xLabel +'px;width:1px;height:1px;background:' +this.color+';font-size:1px;visibility:visible"></div>');
return;
} var pt =new Point();
为什么会执行到this.DrawPoint() = function(x,y,color)里。这是个正常现象吗?
{
this.xLabel = x;
this.yLabel = y;
this.color = color;
this.DrawPoint() = function(x,y,color){
this.xLabel = x;
this.yLabel = y;
this.color = color;
document.write('<div style="position:absolute;top:'+ this.yLabel +'px;left:'+ this.xLabel +'px;width:1px;height:1px;background:' +this.color+';font-size:1px;visibility:visible"></div>');
return;
} var pt =new Point();
为什么会执行到this.DrawPoint() = function(x,y,color)里。这是个正常现象吗?
不过上面这个会报错,
this.DrawPoint() 加了括号等于是调用函数,而不是定义函数,但是这个函数貌似不存在
楼上的说法: this.DrawPoint() = function(x,y,color) 会运行我认为是有问题的,赋值语句的语法要求左边是一个变量,或是数组的一个元素,或是对象的一个属性,而this.DrawPoint()不符合任何条件,这句话语法就是错误的,所以不会运行,会出错
this.DrawPoint()就運行this.DrwaPoint函數你現在做的是定義對像和它的方法,this.DrawPoint都還沒定義(function...在後面),就被執行了(緊接的一對問題),第1個錯誤是不能給函數的結果(加上兩對括號得到的就是結果)賦值,第2個(隱含)的錯誤就是在那行之前都沒有this.DrawPoint(定義),哪能有this.DrawPoint()? 把緊接的兩個括號去掉就OK了。