Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5b5e313

Browse files
authored
Required Function Params
1 parent3ed5b5f commit5b5e313

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

‎README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
|31|[Preventing paste into an input field](#Preventing-paste-into-an-input-field)|
3838
|32|[The void operator](#The-void-operator)|
3939
|33|[replaceAll](#replaceAll)|
40-
40+
|34|[Required Function Params](#Required-Function-Params)|
4141

4242

4343
**[⬆ Back to Top](#table-of-contents)**
@@ -711,6 +711,28 @@ const updatedStr = str.replaceAll('example', 'snippet'); //'this is a JSsnippets
711711
```
712712
713713
714+
**[⬆ Back to Top](#table-of-contents)**
715+
### Required Function Params
716+
Expanding on the default parameter technique, we can mark a parameter as mandatory
717+
718+
```javascript
719+
const isRequired = () => {
720+
throw new Error( 'This is a mandatory parameter.' );
721+
}
722+
723+
724+
const getPage = ( pageName = 'Jssnippets', url = isRequired() ) => {
725+
return `${pageName} ${url}`;
726+
}
727+
728+
console.log(getPage());
729+
730+
//In the above code, url will be undefined and that will try to set the default value for it which is the isRequired() function. It will throw an error as,
731+
732+
//Uncaught error: This is a mandatory parameter.
733+
//at isRequired
734+
735+
```
714736
715737
716738

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp