Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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中找到实例对象的初始创建者?_Javascript_Oop - Fatal编程技术网

如何在JavaScript中找到实例对象的初始创建者?

如何在JavaScript中找到实例对象的初始创建者?,javascript,oop,Javascript,Oop,假设我有以下代码: var Foo = (function () { //constructor var Foo = function (callbackFunction) { this.callbackFunction = callbackFunction; }; //method Foo.prototype = { run: function () { if (typeof(this.call

假设我有以下代码:

var Foo = (function () {

    //constructor
    var Foo = function (callbackFunction) {
        this.callbackFunction = callbackFunction;
    };

    //method
    Foo.prototype = {
        run: function () {
            if (typeof(this.callbackFunction) === 'function') {
                this.callbackFunction.call(???); //??? should be the object that created this Foo instance.
            }
        }   
    };
    return Foo;
})();
var Bar = (function () {

    //constructor
    var Bar = function (v1, v2) {
        this.v1 = v1;
        this.v2 = v2;
    };

    Bar.prototype.callback = function() {
        //'this' should be the instance of Bar
        console.log('value of v1 is ' + this.v1 + ' value of v2 is ' + this.v2);
    }

    Bar.prototype.callFoo = function() {
        this.foo = new Foo(this.callback);
        this.foo.run();
    }

    return Bar;
})();

var bar1 = new Bar('apple', 'orange');
bar1.callFoo();
var bar2 = new Bar('grape', 'banana');
bar2.callFoo();
这保存在foo.js中

我还有以下代码:

var Foo = (function () {

    //constructor
    var Foo = function (callbackFunction) {
        this.callbackFunction = callbackFunction;
    };

    //method
    Foo.prototype = {
        run: function () {
            if (typeof(this.callbackFunction) === 'function') {
                this.callbackFunction.call(???); //??? should be the object that created this Foo instance.
            }
        }   
    };
    return Foo;
})();
var Bar = (function () {

    //constructor
    var Bar = function (v1, v2) {
        this.v1 = v1;
        this.v2 = v2;
    };

    Bar.prototype.callback = function() {
        //'this' should be the instance of Bar
        console.log('value of v1 is ' + this.v1 + ' value of v2 is ' + this.v2);
    }

    Bar.prototype.callFoo = function() {
        this.foo = new Foo(this.callback);
        this.foo.run();
    }

    return Bar;
})();

var bar1 = new Bar('apple', 'orange');
bar1.callFoo();
var bar2 = new Bar('grape', 'banana');
bar2.callFoo();
同样,它保存在bar.js中

在Foo内部,我有一行代码:this.callbackFunction.call(???)


因此,为了实现这一点,我必须将创建Foo实例的对象传递给call函数,但是如何传递呢?

我建议使用
函数.bind()
方法

Mozilla开发者网络:

bind()方法创建一个新函数,该函数在调用时具有 此关键字设置为提供的值,并具有给定的 调用新函数时,将在任何函数前面提供参数

可能很难找到将Bar传递到Foo.callbackFunction的方法,但如果Bar传入
this.callbackFunction.bind(this)
,那么Foo可以只调用
this.callbackFunction()
,而不传递参数(或使用
调用


还有一些JavaScript库允许您在旧浏览器中执行此操作,因为
bind()
是一项相对较新的功能。例如,在Dojo中,它被称为
hitch()

我的建议是使用
函数.bind()
方法

Mozilla开发者网络:

bind()方法创建一个新函数,该函数在调用时具有 此关键字设置为提供的值,并具有给定的 调用新函数时,将在任何函数前面提供参数

可能很难找到将Bar传递到Foo.callbackFunction的方法,但如果Bar传入
this.callbackFunction.bind(this)
,那么Foo可以只调用
this.callbackFunction()
,而不传递参数(或使用
调用


还有一些JavaScript库允许您在旧浏览器中执行此操作,因为
bind()
是一项相对较新的功能。例如,在Dojo中,它被称为
hitch()

,我认为您应该删除
prototype
。使用
Bar.callback
will。您不能在
this.foo.run(this)中将
this
作为参数传递吗?@basilikum,我想我可以,但现在Bar创建Foo,我不知道让Foo和Bar相互指向是否是个好主意。那是我担心的问题。我不知道你说的“互相指指点点”是什么意思。到目前为止,
Bar
知道
Foo
,但不是相反。如果您将
作为通用上下文传递,则仍然是这样。您还可以在创建
Foo
实例时传递上下文,并将其安全保存在
Foo
对象中。但你必须这样做。在您当前的设置中,无法在
run
函数中获取
Bar
实例。顺便说一句,
typeof
是一个关键字,而不是一个函数,您可以省略括号:
typeof this.callbackFunction
。我认为您应该删除
prototype
。使用
Bar.callback
will。您不能在
this.foo.run(this)中将
this
作为参数传递吗?@basilikum,我想我可以,但现在Bar创建Foo,我不知道让Foo和Bar相互指向是否是个好主意。那是我担心的问题。我不知道你说的“互相指指点点”是什么意思。到目前为止,
Bar
知道
Foo
,但不是相反。如果您将
作为通用上下文传递,则仍然是这样。您还可以在创建
Foo
实例时传递上下文,并将其安全保存在
Foo
对象中。但你必须这样做。在您当前的设置中,无法在
run
函数中获取
Bar
实例。顺便说一句,
typeof
是一个关键字,而不是函数,您可以省略括号:
typeof this.callbackFunction
。我想您的意思是
this.callback.bind(this)
?好主意!谢谢,这样更好。我已经替换了行:this.foo=newfoo(this.callback);使用this.foo=newfoo(this.callback.bind(this));然后在Foo内部我可以调用这个.callbackFunction.call();甚至是这个;我想你的意思是
这个.callback.bind(这个)
?好主意!谢谢,这样更好。我已经替换了行:this.foo=newfoo(this.callback);使用this.foo=newfoo(this.callback.bind(this));然后在Foo内部我可以调用这个.callbackFunction.call();甚至是这个;