I use AngularJS, and i have a factory, called storage to share some data between controllers and directives.
ell.on('contextmenu', function(event){
scope.$apply(function(){
event.preventDefault();
storage.contextmenu.isOpen = true;
storage.contextmenu.top = 100;
storage.contextmenu.left = 200;
});
});
The problem is, at the first time the contextmenu is undefined. But i don't want to set here, because the contextmenu may have additional properties and they have reference.
Yes, i know, i can solve this problem if i use if statement like this:
ell.on('contextmenu', function(event){
scope.$apply(function(){
event.preventDefault();
if(typeof storage.contextmenu == 'undefined'){
storage.contextmenu = {}
}
storage.contextmenu.isOpen = true;
storage.contextmenu.top = 100;
storage.contextmenu.left = 200;
});
});
But i want to know is there a getter overload solution to do this as default. So when i set the value of isOpen at the first time and the contextmenu is undefined, instead of the js throw the error, it set the contextmenu to an object and set isOpen to true?
In php __get() function do the magic, is there someting same in js?