Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么JavaScript自动允许我的函数接受额外的参数?_Javascript_Scope_Prototypal Inheritance - Fatal编程技术网

为什么JavaScript自动允许我的函数接受额外的参数?

为什么JavaScript自动允许我的函数接受额外的参数?,javascript,scope,prototypal-inheritance,Javascript,Scope,Prototypal Inheritance,我没有正确理解JavaScript的作用域和继承。我清楚地定义了一个函数来接受1个参数(我知道这是可选的,因为JavaScript是如何处理参数的),但是看起来我需要一个额外的参数来进行范围界定。为什么会这样 ShapeRenderable.prototype = new Renderable(); function ShapeRenderable() { this._line_join = 'round'; this._line_cap = 'round'; .

我没有正确理解JavaScript的作用域和继承。我清楚地定义了一个函数来接受1个参数(我知道这是可选的,因为JavaScript是如何处理参数的),但是看起来我需要一个额外的参数来进行范围界定。为什么会这样

ShapeRenderable.prototype = new Renderable();
function ShapeRenderable()
{
    this._line_join = 'round';
    this._line_cap = 'round';
    .
    .
} 

ShapeRenderable.prototype.setFillColor = function (fill_style)
{
    if (fill_style)
    {
        this._fill_style = fill_style;
        this.initialAlpha = fill_style._alpha;
    }
    else
    {
        this._fill_style = new RGBA(0,0,0,1.0);
    }
};



SelectionCube.prototype = new ShapeRenderable();

function SelectionCube()
{ 
    var self = this; // So we can access public methods and members from within
    // private ones.    

    self._visible = true;         
    self._scale = 1;
    self._stroke_style   = new RGBA(0,0,0,.1); // Lines solid black
    self._line_width = 7.0; // 1

    var platonicRenderable = new PlatonicRenderable(this);
    platonicRenderable.createCube(); 

    // surfaces mostly transparent white
    ShapeRenderable.prototype.setFillColor.call(this, new RGBA(255,255,255,1));
    // Why do I have to pass "this" to get the correct scope during 
    // execution of the above? If "this" isn't passed, it looks like JavaScript 
    // is scoping this for the parent, instead of for the child.
    // 
    // This is the correct behavior. My cube is now white.


}
如果我打电话:

ShapeRenderable.prototype.setFillColor.call(new RGBA(255,255,255,1));
然后我的renderable是黑色的(这是不正确的),看起来是这样的。我的SelectionCube从来没有正确设置过填充样式。为什么会这样?是否有一些秘密JavaScript可选(第一个)参数告诉函数在执行过程中使用什么范围?我遇到了上面的问题,我读到了关于通过这个问题的消息,所以我一直不明白为什么

以下是两种情况下的情况: 已损坏-未正确更新“this”


工作-正确更新“this”

您正在使用的
。错误调用
call
的正确语法是

fun.call(thisArg[, arg1[, arg2[, ...]]])

第一个参数是函数中的“scope”或
对象。后续参数将传递给函数的参数


但是在这种情况下,最好在评论中使用@elclars建议。

call/apply
将“thisArg”作为第一个参数。由于JavaScript中的方法不绑定到对象,而实际上只是“通过各种属性名已知”的函数(尽管请参见
bind
),因此必须手动指定。为什么需要这一行?您应该尝试
this.setFillColor(…)
,因为您已经设置了继承。:调用
的第一个参数是
this
,它只是作为参数传递给函数的其余参数。谢谢。我不需要那条线。我对JavaScript继承的了解还没有我想要的那么多。谢谢。我最终使用了这个.setFillColor(新的RGBA(255255,1));正如所建议的那样。我不知道为什么我以前没有使用它——很可能我在尝试时没有正确设置所有东西。