From 1a92565439d0293dc4c764a1ee750910427408a9 Mon Sep 17 00:00:00 2001 From: Aonghus O Nia Date: Mon, 21 May 2018 16:45:24 -0400 Subject: [PATCH 1/2] add example for functions with ES6 default arguments --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 2f0dd74..4cec6e8 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,22 @@ handleResponse('some', 'args') // TypeError: Callback is required Note the __vargs.strict__ call. +With ES6 default parameters: +vargs-callback uses Function.length to find the correct position for the callback. The length parameter only includes parameters before the first one with a default value so you will need to set the lenght parameter for functions with default arguments for vargs-callback to work. + +```js +var vargs = require('vargs-callback') + +function openTheDoor(door, options = {speed: 1} , callback) { + // actual function code using parameters + // options will be {speed: 1} if only door and callback given + if (door.isOpen) return + // ... +} + +Object.defineProperty(openTheDoor, 'length', { value: 3 }) +openTheDoor = vargs(openTheDoor) // Decorate global function +``` ## The Rules From a247ff082871cf25b5e63b2e248fabe404f1be04 Mon Sep 17 00:00:00 2001 From: Aonghus O Nia Date: Mon, 21 May 2018 16:46:50 -0400 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4cec6e8..2c1ea68 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ handleResponse('some', 'args') // TypeError: Callback is required Note the __vargs.strict__ call. With ES6 default parameters: -vargs-callback uses Function.length to find the correct position for the callback. The length parameter only includes parameters before the first one with a default value so you will need to set the lenght parameter for functions with default arguments for vargs-callback to work. +vargs-callback uses Function.length to find the correct position for the callback. The length parameter only includes parameters before the first one with a default value so you will need to set the length parameter for functions with default arguments for vargs-callback to work. ```js var vargs = require('vargs-callback')