我正在设法使我了解如何使用可选的回调参数制作jasvascript函数
我不确定要达到的技术术语,因此很难找到解决方案,但这可能对其他新手很有用
即,用法如下
MyFunction({
start: function() {
},
end: function() {
}
});
这是一个带有示例的小提琴:http : //jsfiddle.net/0csnrv7p/
代码:
function myFunction(options) {
// You need something like this line if you want to handle the case
// where there are no arguments at all. If options is undefined then
// trying to access a property within it results in an error.
options = options || {};
if (typeof options.start === 'function') {
options.start();
}
// ...
// do stuff
// ...
if (typeof options.end === 'function') {
options.end();
}
}
myFunction({
start: function() {
console.log('start');
},
end: function() {
console.log('end');
}
});
这是更多信息的链接:http : //javascriptissexy.com/understand-javascript-callback-functions-and-use-them/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句