forked from parmanoir/jscocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path54 custom indexed access.js
103 lines (73 loc) · 2.77 KB
/
54 custom indexed access.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Use Javascript bracket notation to get and set objects, whether as arrays or dictionaries.
var a = customIndexAccess[i].x
customIndexAccess[i].x = 123
Add get and set methods in a class to have it behave like an array or a hash in Javascript.
Behave like an array
get - (id)objectAtIndex:(int)index
set - (void)replaceObjectAtIndex:(int)index withObject:(id)anObject
length - (int)count
Then use object[index] to get and set.
Behave like a dictionary
get - (id)objectForKey:(NSString*)key
set - (void)setObject:(id)anObject forKey:(id)aKey
Then use object[key] or object.key to get and set.
*/
var globalObject
var globalIndex
@implementation CustomIndexedAccess : NSObject
- (id)objectAtIndex:(int)index
{
if (index == 0) return 'Hello'
if (index == 1) return 'World'
if (index == 2) return 'Test'
}
- (int)count
{
return 3
}
- (void)replaceObjectAtIndex:(int)index withObject:(id)anObject
{
globalIndex = index
globalObject = anObject
// log('replaceObjectAtIndex=' + index + ' withObject=' + anObject)
}
@end
@implementation CustomKeyedAccess : NSObject
- (id)objectForKey:(NSString*)key
{
if (key == 'first') return 'Test'
if (key == 'second') return 'Bonjour'
if (key == 'third') return 'Monde'
}
- (void)setObject:(id)anObject forKey:(id)aKey
{
globalIndex = aKey
globalObject = anObject
// log('setObject=' + anObject + ' forKey=' + aKey)
}
@end
var o = [CustomIndexedAccess instance]
if (o[0] != 'Hello') throw 'Custom indexed access failed (1)'
if (o[1] != 'World') throw 'Custom indexed access failed (2)'
if (o[2] != 'Test') throw 'Custom indexed access failed (3)'
if (o.length != 3) throw 'Custom indexed access failed (4)'
o[0] = 'prim'
if (globalIndex != 0 && globalIndex != 'prim') throw 'Custom indexed access failed (5)'
o[1] = 'prox'
if (globalIndex != 1 && globalIndex != 'prox') throw 'Custom indexed access failed (6)'
o[2] = 'dern'
if (globalIndex != 2 && globalIndex != 'dern') throw 'Custom indexed access failed (7)'
var o = [CustomKeyedAccess instance]
if (o['first'] != 'Test') throw 'Custom indexed access failed (8)'
if (o['second'] != 'Bonjour') throw 'Custom indexed access failed (9)'
if (o['third'] != 'Monde') throw 'Custom indexed access failed (10)'
o['first'] = 'prim'
if (globalIndex != 'first' && globalIndex != 'prim') throw 'Custom indexed access failed (11)'
o['second'] = 'prox'
if (globalIndex != 'second' && globalIndex != 'prox') throw 'Custom indexed access failed (12)'
o['third'] = 'dern'
if (globalIndex != 'third' && globalIndex != 'dern') throw 'Custom indexed access failed (13)'
o = null
globalIndex = null
globalObject = null