Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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 对象方法传递对象方法what';语法是什么?_Javascript - Fatal编程技术网

Javascript 对象方法传递对象方法what';语法是什么?

Javascript 对象方法传递对象方法what';语法是什么?,javascript,Javascript,使用KineticJS,我知道了如何将函数作为Gameboard函数传递,但所有这些都在Gameboard函数中,我想现在是对象获得了函数:( 功能游戏板(){ //..为舞台层和所有对象创建占位符 this.dice_layer=新的dynamic.layer(); this.rolldice=函数(){ 警报(this.toString()); //…改变形象 这个.dice_layer.draw();//问题是这条线: this.doSomething=function(fncti

使用KineticJS,我知道了如何将函数作为Gameboard函数传递,但所有这些都在Gameboard函数中,我想现在是对象获得了函数:(

功能游戏板(){
//..为舞台层和所有对象创建占位符
this.dice_layer=新的dynamic.layer();
this.rolldice=函数(){
警报(this.toString());
//…改变形象

这个.dice_layer.draw();//问题是这条线:

    this.doSomething=function(fnction){
您将
doSomething
声明为一个带有单个参数的函数,
fnction
,但当您调用它时,您传递的是两个参数—一个字符串和一个函数

    this.doSomething=function(str, fnction){
将按照你的期望行事


根据您对第二个问题的“解决方案”,您似乎希望使用ES5。它允许您为特定函数调用指定
this
,因为JavaScript实际上没有“方法”,您必须指定它们操作的对象

 this.barfoo.doSomething(this.doBar.bind(this));

一个代码可以与之相比较。

也许你的简化并没有显示出真正的问题。我想下面类似的东西会更类似于你的问题:

function foo(){
    this.doSomething = function(fnction){
        fnction();
   };
}

function bar(){
    this.myField = "Buzz"
    this.barfoo = new foo();
    this.doBar = function(){
        alert(this.myField);
    };
    this.barfoo.doSomething(this.doBar); // tried this
    //this.barfoo.doSomething(this.doBar());  also tried this  
    //this.barfoo.doSomething(bar.doBar);  also tried this  
    //this.barfoo.doSomething(bar.doBar());  also tried this  
}
在这里,您可以注意到访问此
相关属性时出现的问题

如果这确实是问题所在,那么您应该能够通过使用
foo
doSomething
方法中的
call
apply
来解决问题:

function foo() {
  this.doSomething = function (obj, fn) {
    fn.call(obj);
  };
}
这就是您在
栏中使用它的方式:

function bar() {
  this.myField = "Buzz";
  this.barfoo = new foo();
  this.doBar = function () {
    alert(this.myField);
  };
  this.barfoo.doSomething(this, this.doBar);
}

var myBar = new bar();

检查。

同时阅读有关此
:。请澄清什么“不起作用”意思-控制台中有错误吗?我的问题已经解决了!!谢谢Dennis和Nadir。我来自.NET世界和Java,所以用函数代替方法需要一点思维分离,但在你们两位的帮助下,我想我已经向前迈出了一大步。我没有投票的名声。对不起,可能会的需要另一个问题,但我很好奇,将var obj=this//Gameboard作为Gameboard变量还是在函数中声明它更好?它是否存在内存问题?是否
circle.on…
包含在另一个函数中?函数.on来自Kinetic.Node的KineticJS框架。on circle在l处是Kinetic.Node.Shape.circle从我对OOP的理解来看,很抱歉,这是一个输入错误,原始对象有一个字符串对象“click”。我想我只是简单地重新创建了这个问题。如果没有全局函数,我无法获得bar.foo.doSomething(fnction)来获取bar.doSomething就是this.barfoo.doSomething(this.doBar);正确的语法,应该按照预期的方式运行?由于某种原因,它使所有其他bar对象都变得无用。this.barfoo.doSomething(bar.doSomething.bind(this));give me doSomething没有定义使问题更清楚,因为您提供了明显有效的示例我正在使用KineticJS我有一个Stage->Layer->Circle Gameboard是实例的对象,当我单击圆圈时,我想发送Gameboad函数this.rolldice(),但我在没有全局函数的情况下失败。Circle.on(“单击”,this.rolldice);不起作用,circle.on也不起作用(“单击”,this.rolldice.bind(this));bar是构造函数。bar.doSomething不存在,因为doSomething仅用于
foo
的实例。感谢您的帮助不幸的是,foo是框架的一部分,我担心修改它会困难得多。这可能意味着我只能像现在这样使用全局。我希望找到更好的解决方案。我正在使用KineticJS我有一个Stage->Layer->Circle Gameboard是带有实例的对象,当我单击该圆圈时,我想发送Gameboad函数this.rolldice(),但是没有全局函数,我失败了。Circle.on(“click”,this.rolldice);Circle.on(“click”,this.rolldice.bind(this));正如丹尼斯所建议的那样,
circle.on(“click”,function(){this.rolldice();})
circle.on(“click”,function(){this.rolldice.call(this);})
一旦进入循环,就可以工作了{这是你认为的还是你知道的?因为这是一个闭包,
这个
应该绑定到它创建的对象(即
)。我不知道javascript我知道它不是指游戏板lol对不起,新的javascript。我让它通过var obj=this;circle.on(“单击”,obj.rolldice)转到函数;刚才它没有看到游戏板的层。
function bar() {
  this.myField = "Buzz";
  this.barfoo = new foo();
  this.doBar = function () {
    alert(this.myField);
  };
  this.barfoo.doSomething(this, this.doBar);
}

var myBar = new bar();