FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
basic-programs/javascript/method-overloading.js at master · Scarlet-Coder/basic-programs · GitHub
Scarlet-Coder
/
basic-programs
Public
forked from
utkarsh-shekhar/basic-programs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
basic-programs
/
javascript
/
method-overloading.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
76 lines (67 loc) · 2.15 KB
Breadcrumbs
basic-programs
/
javascript
/
method-overloading.js
Copy path
File metadata and controls
76 lines (67 loc) · 2.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* An example of method overloading with JavaScript, also known as 'monkey patching'.
* JavaScript as a language does not inherently support method overloading, but we can
* implement our own solution as we have access to the array of arguments supplied to the function.
* This gives us substantially more control over the method overloading interface.
*/
class
Adder
{
/**
* Entry function to handle addition with different arguments
*
*
@params
{int} a First integer
*
@params
{int} b Second integer
* Or;
*
@params
{int} a First integer
*
@params
{int} b Second integer
*
@params
{int} c Third integer
* Or;
*
@params
{Array} a Array of integers
*
*
@returns
{
int
} Sum of arguments
*
@throws
{
error
} If arguments do not match one of the above params
*/
add
(
a
,
b
)
{
const
argArray
=
Array
.
prototype
.
slice
.
call
(
arguments
)
;
// If the length is 2, we have `a` and `b` as arguments
if
(
argArray
.
length
===
2
)
{
return
a
+
b
;
}
// If the length is 3, we have `a`, `b`, and `c` as arguments
if
(
argArray
.
length
===
3
)
{
return
this
.
addThreeArgs
.
apply
(
this
,
arguments
)
;
}
// If `a` is an Array, we can sum using `addArray`
if
(
Array
.
isArray
(
a
)
)
{
return
this
.
addArray
.
apply
(
this
,
arguments
)
;
}
// If none of the above match, we were given invalid input
throw
new
Error
(
'Please use one of the following formats: add(1, 2), add(1, 2, 3) or add([1, 2, 3]).'
)
;
}
/**
* Sum three numbers
*
*
@params
{int} a First integer
*
@params
{int} b Second integer
*
@params
{int} c Third integer
*
@returns
{
int
} Sum of a, b, and c
*/
addThreeArgs
(
a
,
b
,
c
)
{
return
a
+
b
+
c
;
}
/**
* Reduce array values to a single sum value of array items
*
*
@params
{Array} a Array of integers
*
@returns
{
int
} Sum of items in array
*/
addArray
(
arr
)
{
return
arr
.
reduce
(
(
acc
,
cur
)
=>
acc
+
cur
)
;
}
}
const
adder
=
new
Adder
(
)
;
const
addWithTwo
=
adder
.
add
(
1
,
1
)
;
// 2
console
.
log
(
addWithTwo
)
;
const
addWithThree
=
adder
.
add
(
1
,
2
,
3
)
;
// 6
console
.
log
(
addWithThree
)
;
const
addWithArray
=
adder
.
add
(
[
1
,
2
,
3
,
4
]
)
;
// 10
console
.
log
(
addWithArray
)
;
Back
|
FazBrowse Home
|
New Git URL