如何使用javascript的调用函数,并将object作为参数之一?
我正在尝试使用Javascript的经典如何使用javascript的调用函数,并将object作为参数之一?,javascript,backbone.js,Javascript,Backbone.js,我正在尝试使用Javascript的经典.call方法。我有一个函数定义,它接受对象作为参数 当我尝试使用.call()调用此函数时,我需要提供上下文“this”和对象参数。我不能这样做。有人能帮忙吗 代码如下: //options is the object param var setupSomething = function(options) { //function definition goes here } 此函数是类MyView的成员函数 我有另一个类GuestView
.call
方法。我有一个函数定义,它接受对象作为参数
当我尝试使用.call()调用此函数时,我需要提供上下文“this”和对象参数。我不能这样做。有人能帮忙吗
代码如下:
//options is the object param
var setupSomething = function(options) {
//function definition goes here
}
此函数是类MyView
的成员函数
我有另一个类GuestView
,我需要通过它调用这个函数
提供访客视图的上下文(此
)
代码如下:
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"
使用绑定:
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
直接调用它。请注意,它需要在构造函数中定义,以便访问它绑定到的值
编辑
我刚看到你添加的代码。我不熟悉主干网,也不熟悉如何调用render
函数(问题实际上就在那里),因此上面的内容可能没有多大用处,但我将把它放在这里。在调用setupSomething.call时,这肯定是您所期望的(…
?您是否可以从该代码段访问GuestView实例?@PhilVarg,@James Thorpe:是的,GuestView的实例调用其中一个成员函数,在其中调用setupSomething,因此此引用GuestView@PhilVarg:MDN中call的文档说明call接受t他的参数和个人参数。是否因为传递对象参数是不可接受的?这里确实没有足够的代码来提供帮助…能否在guestView的上下文中发布更多关于调用.call函数的代码?