Reputation: 472
I'm trying to access the selectItems array from inside the functions in selectable object, but not sure if there is a way to do this without passing the _Multiselect object back into the function as a parameter. Are there other ways?
function _MultiSelect() {
}
_MultiSelect.prototype = {
selectedItems: [],
selectable: {
myFunc: function(){
//how can I access selectedItems from here
}
}
}
Upvotes: 0
Views: 506
Reputation: 10458
you can store the context of this in that refer to the following code
_MultiSelect.prototype = {
selectedItems: [],
selectable: function() {
// store the context of this in that
var that = this;
return {
myFunc: function(){
// is accessible
console.log(that.selectedItems);
}
}
}
}
Upvotes: 1
Reputation: 36511
One option is to make selectable
a function that returns an object containing myFunc
and whatever else. This allows you to capture the _MultiSelect
context in a closure and use it in the methods you are exposing.
_MultiSelect.prototype = {
selectedItems: [],
selectable: function() {
var context = this;
return {
myFunc: function(){
console.log(context.selectedItems);
//how can I access selectedItems from here
}
}
}
}
Usecase:
(new _MultiSelect).selectable().myFunc();
Upvotes: 2