Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.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_Function - Fatal编程技术网

Javascript 将对象函数传递给对象构造函数

Javascript 将对象函数传递给对象构造函数,javascript,function,Javascript,Function,为标题道歉,但没有简洁的表达方式。我正在编写以下代码,旨在将一组计数器链接在一起,形成一个大计数器。建造一个时钟或其他什么 function subcounter(max, name, trigger) { this.index = 0; this.trigger = trigger; this.name = name; this.tick = function() { this.index++; if (this.index==

为标题道歉,但没有简洁的表达方式。我正在编写以下代码,旨在将一组计数器链接在一起,形成一个大计数器。建造一个时钟或其他什么

function subcounter(max, name, trigger) {
    this.index = 0;
    this.trigger = trigger;
    this.name = name;

    this.tick = function() {
        this.index++;
        if (this.index==max) {
            this.index=0;
            this.trigger();
        }
    }

    this.show = function() {
        alert(this.name+' triggered');
    }
}

y = new subcounter(2,'y',function(){alert('finished')});
x = new subcounter(2,'x',y.tick);

for (var index = 0; index < 12; index++) {
    alert ([x.index, y.index]);
    x.tick();
}
发现显示的是“x触发”而不是“y触发”,这是我所期望的。这是怎么回事?(在Firefox中试用)


感谢您的回答或向我指出有关此的文档。然而,我的大脑仍然无法理解一个作用域为一个对象实例的函数:“y.show”如何解析为另一个对象实例上的函数

答案似乎是:

x = new subcounter(2,'x',function() {y.tick();});
但我还是想真正理解为什么原作没有按预期工作。

应该是这样的

function subcounter(max, name, trigger) {
    var that = this;
    this.index = 0;
    this.trigger = trigger;
    this.name = name;

    this.tick = function() {
        that.index++;
        if (that.index==max) {
            that.index=0;
            that.trigger();
        }
    }

    this.show = function() {
        alert(that.name+' triggered');
    }
}
否则,javascript的本地作用域将在内部函数中包含对外部上下文
this
的引用(即在您的情况下为
x.this


是一篇详细介绍javascript本地作用域功能的文章,但这只是我得到的第一个结果,这是一个非常常见的问题。

javascript在从另一个对象上下文调用方法时更改了
的作用域。看看这篇文章:


据我所见,它与函数中“this”的值有关

在函数中,“this”将是从中调用函数的对象的值

当您调用this.trigger()时,这就是对象“x”。所以在触发函数中,比如“show”

this.name will be same as x.name
要获取y对象值,请传递“y”对象本身并从该对象调用show函数

function subcounter(max, name, trigger, methodName) {
    this.index = 0;
    this.trigger = trigger;
    this.name = name;

    this.tick = function() {
        this.index++;
        if (this.index==max) {
            this.index=0;
            this.trigger[methodName]();
        }
    }

    this.show = function() {
        console.log(this.name+' triggered');
    }
}

y = new subcounter(2,'y',function(){alert('finished')});
x = new subcounter(2,'x',y, "show");

阅读此
:我愿意接受这个答案。但它不起作用。我仍然得到“x触发”。非常奇怪:我用修改后的代码创建了一个小提琴(并且用
console.log
s替换了
alert
s),它似乎在工作。我发现它是正确的。但是Firefox/IE都“不正确”地工作。我看到它在FF中也记录了“y触发”:(这是有效的。但是它是不灵活的。正如您所看到的,我希望能够传递任何要触发的函数。@guillermo phillips我已经更新了我的答案。现在您可以传递要调用的对象和方法名称。
function subcounter(max, name, trigger, methodName) {
    this.index = 0;
    this.trigger = trigger;
    this.name = name;

    this.tick = function() {
        this.index++;
        if (this.index==max) {
            this.index=0;
            this.trigger[methodName]();
        }
    }

    this.show = function() {
        console.log(this.name+' triggered');
    }
}

y = new subcounter(2,'y',function(){alert('finished')});
x = new subcounter(2,'x',y, "show");