我想知道记录参数(这是一个对象)及其所有属性的正确方法是什么。
- 如果我理解正确方括号的意思是“可选”,对吗?
- 我用来描述对象属性的语法是否有效?
- 该
reqSettings.retryInterval
属性是一个数字,但我无法找到包含其单位的方法。有没有办法表明这个数字以毫秒为单位?
编码:
/**
* used as a facade for the 'callServer' function.
* @param {object} reqSettings - a settings object to provide 'callServer'.
* @param {object} reqSettings.ajaxOpt - an object containing standard jquery ajax settings.
* @param {function} reqSettings.success - a success handler for 'fulfilled' promises.
* @param {function} reqSettings.failure="failResponse" - a failure handler for 'rejected' promises.
* @param {number} [reqSettings.retries=0] - maximum retries allowed per cycle
* @param {number} [reqSettings.retryInterval=1500] - interval to use between retries (ms)
* @param {number} [reqSettings.attempted=1] - a counter used to count total attempts
*/
function gateKeeper(reqSettings) {
if ( !reqSettings.retries ) { reqSettings.retries = 0 }
if ( !reqSettings.retryInterval ) { reqSettings.retryInterval = 1500 }
if ( !reqSettings.attempted ) { reqSettings.attempted = 1 }
reqSettings.retries--
if ( !reqSettings.success || !reqSettings.ajaxOpt.url ) {
throw new TypeError("success handler or ajax url property is missing")
}
return callServer(reqSettings)
}