Make constructor parameter optional

This commit is contained in:
Alan Choi
2019-01-17 18:36:56 +09:00
parent 24b35dd991
commit 00fdefc080
2 changed files with 6 additions and 2 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ interface ExtendedKeyboardEvent extends KeyboardEvent {
interface MousetrapStatic {
(el: Element): MousetrapInstance;
new (el: Element): MousetrapInstance;
new (el?: Element): MousetrapInstance;
addKeycodes(keycodes: { [key: number]: string }): void;
stopCallback: (e: ExtendedKeyboardEvent, element: Element, combo: string) => boolean;
bind(keys: string|string[], callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): void;
+5 -1
View File
@@ -49,7 +49,11 @@ Mousetrap.reset();
// event handler to the form element only, instead of the entire document.
var element = document.querySelector('form');
var instance = new Mousetrap(element);
instance.bind('mod+s', function(){ console.log('Instance Saved'); });
instance.bind('mod+s', function () { console.log('Instance Saved'); });
// Test that we can create an instance of mousetrap without passing element to the constructor.
var documentInstance = new Mousetrap();
documentInstance.bind('mod+s', function () { console.log('documentInstance Saved'); });
// Test that the factory method works as well.
Mousetrap(element).bind('mod+s', function(){ console.log('Factory Saved'); });