FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
javascript/object-oriented/Introduction/Interface.js at master · wchaowu/javascript · GitHub
wchaowu
/
javascript
Public
Notifications
You must be signed in to change notification settings
Fork
312
Star
682
Code
Issues
3
Pull requests
1
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
javascript
/
object-oriented
/
Introduction
/
Interface.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
72 lines (55 loc) · 2.15 KB
Breadcrumbs
javascript
/
object-oriented
/
Introduction
/
Interface.js
Copy path
File metadata and controls
72 lines (55 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
// Constructor.
var
Interface
=
function
(
name
,
methods
)
{
if
(
arguments
.
length
!=
2
)
{
throw
new
Error
(
"Interface constructor called with "
+
arguments
.
length
+
"arguments, but expected exactly 2."
)
;
}
this
.
name
=
name
;
this
.
methods
=
[
]
;
for
(
var
i
=
0
,
len
=
methods
.
length
;
i
<
len
;
i
++
)
{
if
(
typeof
methods
[
i
]
!==
'string'
)
{
throw
new
Error
(
"Interface constructor expects method names to be "
+
"passed in as a string."
)
;
}
this
.
methods
.
push
(
methods
[
i
]
)
;
}
}
;
// Static class method.
Interface
.
ensureImplements
=
function
(
object
)
{
if
(
arguments
.
length
<
2
)
{
throw
new
Error
(
"Function Interface.ensureImplements called with "
+
arguments
.
length
+
"arguments, but expected at least 2."
)
;
}
for
(
var
i
=
1
,
len
=
arguments
.
length
;
i
<
len
;
i
++
)
{
var
interface
=
arguments
[
i
]
;
if
(
interface
.
constructor
!==
Interface
)
{
throw
new
Error
(
"Function Interface.ensureImplements expects arguments "
+
"two and above to be instances of Interface."
)
;
}
for
(
var
j
=
0
,
methodsLen
=
interface
.
methods
.
length
;
j
<
methodsLen
;
j
++
)
{
var
method
=
interface
.
methods
[
j
]
;
if
(
!
object
[
method
]
||
typeof
object
[
method
]
!==
'function'
)
{
throw
new
Error
(
"Function Interface.ensureImplements: object "
+
"does not implement the "
+
interface
.
name
+
" interface. Method "
+
method
+
" was not found."
)
;
}
}
}
}
;
/*
// Example usage:
// Interfaces.
var Composite = new Interface('Composite', ['add', 'remove', 'getChild']);
var FormItem = new Interface('FormItem', ['save']);
// CompositeForm class
var CompositeForm = function(id, method, action) { // implements Composite, FormItem
...
};
...
function addForm(formInstance) {
Interface.ensureImplements(formInstance, Composite, FormItem);
// This function will throw an error if a required method is not implemented, halting execution.
// All code beneath this line will be executed only if the checks pass.
...
}
*/
Back
|
FazBrowse Home
|
New Git URL