- Sniffer function can or can't run.
- can do lazy run until function is loaded.
- Modern browsers and Internet Explorer 6+
IE / Edge |
Firefox |
Chrome |
Safari |
Opera |
Electron |
---|---|---|---|---|---|
IE6+, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions | last 2 versions |
Using npm
npm install --save function-sniffer
node environment:
var Sniffer = require('function-sniffer');
webpack or similar environment:
import Sniffer from 'function-sniffer';
requirejs environment:
requirejs(['node_modules/function-sniffer/dist/function-sniffer.cjs.js'], function (base) {
// do something...
})
browser environment:
<script src="node_modules/function-sniffer/dist/ifunction-sniffer.min.js"></script>
This is a Object Tool
has a function do
.
// tool.js
(function(Tool, undefined){
Tool.do = function(thing){
console.log('try to do '+ thing);
}
})(window.Tool || (window.Tool = {});
Now, Sniffer.run
can execute function just like Tool.do
.
- Sniffer.run
- base
default: window, base is not required, if object is base on window.
- name
execute
base.namefunction. is required.
- prompt
prompting when function can't be found and execute. not required.
- showPromptFn
custom prompt function. default is alert. not required.
- subscribe
if you want to lazy run function which is not exist now.Sniffer memory it.When function loaded, try Sniffer.trigger to run it. not required.
- base
import Sniffer from 'function-sniffer';
Sniffer.run({
name: 'Tool.do',
prompt: 'Tool.do is not exist.',
showPromptFn: (str)=>{ console.log(str);},
subscribe: true
});
Sniffer.trigger
can run the memory list of function.
- Sniffer.trigger
- base
default: window, base is not required, if object is base on window.
- name
execute
base.namefunction. is required.
- base
import Sniffer from 'function-sniffer';
Sniffer.run({
name: 'Tool.walk',
subscribe: true
});
// walk() is not exist in Tool.And Sniffer memory it, Because `subscribe:true`.
Tool.walk = function(person){
console.log(person + ' is walking.');
}
// walk loaded.And trigger to run it.
Sniffer.trigger({
base: Tool,
name: 'walk'
}, 'leon')
// => leon is walking.
MIT