I have the following code where I tried to replace createModal with a class called Modal. However when I tried this typescript gives me errors and says that “link does not exist in the current scope”:
module Admin.MyAccount.Access {
export function createModal(link: Link) {
link.Modal.$Modal = $.modal({
resizeOnLoad: true
});
link.Modal.$Modal.applyTemplateSetup()
}
export class Modal {
link: Link;
constructor (link: Link) {
this.link = link;
}
create() {
link.Modal.$Modal = $.modal({ // < Error here
resizeOnLoad: true
});
link.Modal.$Modal.applyTemplateSetup() // < Error here
}
}
}
When I am using the function I call the function like this:
createModal(link);
Am I doing something wrong here? Why is it that I cannot access link inside of the create() ? Also could I do this with a static function. Would that be easier as I would not have to call the new to make a new instance of Modal ?
You will need to add
thisto the call, since you need to access the class scope.As far as choosing wether or not to use
staticcomes down to how you use the object. Do you have multiple instances of the object, but only always needs 1 copy of it? If so, usestatic.This means that all of your modal’s will be linked together, and there can always only be one.
Using static in javascript