I am trying to use Javascript's classic .call method. I have a function definition which accepts object as parameter.
When I try to invoke this function using .call(), I need to provide the context 'this' and the object param. I am not able to do so. Can someone help ?
Here is the code:
//options is the object param
var setupSomething = function(options) {
//function definition goes here
}
This function is a member function of class MyView.
I have another class GuestView from which I need to invoke this function by
providing GuestView's context(this).
Here is the code:
MyView.prototype.setupSomething.call(this, options);
The problem is when the parser hits the definition of setupSomething, the context this which should belong to GuestView is not the one. Instead, it is the context of MyView. Any suggestions what I am doing wrong.
More code:
//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.
}
})
`
thisdefinitely what you expect it to be on the line where you invokesetupSomething.call(...?GuestViewcalls one of the member functions inside where setupSomething is invoked and hencethisrefers to context of GuestView