如何使用对象作为参数之一的 javascript 的调用函数?
How to use call function of javascript with object as one of the arguments?
我正在尝试使用 Javascript 的 classic .call
方法。我有一个接受对象作为参数的函数定义。
当我尝试使用 .call() 调用此函数时,我需要提供上下文 'this' 和对象参数。我不能这样做。有人可以帮忙吗?
这是代码:
//options is the object param
var setupSomething = function(options) {
//function definition goes here
}
此函数是classMyView
的成员函数。
我有另一个 class GuestView
我需要从中调用这个函数
提供 GuestView 的上下文(this
).
代码如下:
MyView.prototype.setupSomething.call(this, options);
问题是当解析器命中 setupSomething
的定义时,应该属于 GuestView
的上下文 this
不是那个。相反,它是 MyView
的上下文。任何建议我做错了什么。
更多代码:
//This is instantiation of GuestView
var guestView = new GuestView({model: model});
guestView.render();
//This is declaration of GuestView where guestView.render() hits after invocation
var GuestView = Backbone.View.extend( {
initialize: function() {
//setting of default variables, methods etc goes here
},
render: function() {
var options = {key1: value1, key2: value2}
this.someMemberFunc1();
this.someMemberFunc2();
MyView.prototype.setupSomething(this, options);//MyView is declared and defined in some other file, that's why it's prototype is used here.
}
})
`
传递对象和其他任何东西(函数、数组,你有什么)不是问题。我的猜测是您正在从无法访问 this
变量的上下文中调用 setupSomething
。以下方法都有效:
使用调用:
function myView(){}
myView.prototype.setupSomething = function(options){
console.log(options.greeting + ' ' + this.name);
};
function guestView(name){
this.name = name;
}
guestView.prototype.setupSomething = function(options){
myView.prototype.setupSomething.call(this, options);
}
var guest = new guestView('Yogita');
guest.setupSomething({greeting:"hello"}); // "hello Yogita"
使用绑定:
function myView(){}
myView.prototype.setupSomething = function(options){
console.log(options.greeting + ' ' + this.name);
};
function guestView(name){
this.name = name;
this.setupSomething = myView.prototype.setupSomething.bind(this);
}
var guest = new guestView('Yogita');
guest.setupSomething({greeting : "namaste"}); // "namaste Yogita"
第二个选项允许您在 guestView
构造函数上创建相同的函数,并能够使用 guestView
创建的 this
直接调用它。请注意,它需要在构造函数内部定义,以便访问它绑定到的 this
值。
编辑
我刚看到你添加的代码。我不熟悉 Backbone 或您如何调用 render
函数(问题实际上出在那里),所以上面的内容可能用处不大,但我会留在这里。
我正在尝试使用 Javascript 的 classic .call
方法。我有一个接受对象作为参数的函数定义。
当我尝试使用 .call() 调用此函数时,我需要提供上下文 'this' 和对象参数。我不能这样做。有人可以帮忙吗?
这是代码:
//options is the object param
var setupSomething = function(options) {
//function definition goes here
}
此函数是classMyView
的成员函数。
我有另一个 class GuestView
我需要从中调用这个函数
提供 GuestView 的上下文(this
).
代码如下:
MyView.prototype.setupSomething.call(this, options);
问题是当解析器命中 setupSomething
的定义时,应该属于 GuestView
的上下文 this
不是那个。相反,它是 MyView
的上下文。任何建议我做错了什么。
更多代码:
//This is instantiation of GuestView
var guestView = new GuestView({model: model});
guestView.render();
//This is declaration of GuestView where guestView.render() hits after invocation
var GuestView = Backbone.View.extend( {
initialize: function() {
//setting of default variables, methods etc goes here
},
render: function() {
var options = {key1: value1, key2: value2}
this.someMemberFunc1();
this.someMemberFunc2();
MyView.prototype.setupSomething(this, options);//MyView is declared and defined in some other file, that's why it's prototype is used here.
}
})
`
传递对象和其他任何东西(函数、数组,你有什么)不是问题。我的猜测是您正在从无法访问 this
变量的上下文中调用 setupSomething
。以下方法都有效:
使用调用:
function myView(){}
myView.prototype.setupSomething = function(options){
console.log(options.greeting + ' ' + this.name);
};
function guestView(name){
this.name = name;
}
guestView.prototype.setupSomething = function(options){
myView.prototype.setupSomething.call(this, options);
}
var guest = new guestView('Yogita');
guest.setupSomething({greeting:"hello"}); // "hello Yogita"
使用绑定:
function myView(){}
myView.prototype.setupSomething = function(options){
console.log(options.greeting + ' ' + this.name);
};
function guestView(name){
this.name = name;
this.setupSomething = myView.prototype.setupSomething.bind(this);
}
var guest = new guestView('Yogita');
guest.setupSomething({greeting : "namaste"}); // "namaste Yogita"
第二个选项允许您在 guestView
构造函数上创建相同的函数,并能够使用 guestView
创建的 this
直接调用它。请注意,它需要在构造函数内部定义,以便访问它绑定到的 this
值。
编辑
我刚看到你添加的代码。我不熟悉 Backbone 或您如何调用 render
函数(问题实际上出在那里),所以上面的内容可能用处不大,但我会留在这里。