Repository navigation
Expand file tree
/
Copy pathtriplet.js
More file actions
56 lines (47 loc) · 1.56 KB
/
Copy pathtriplet.js
File metadata and controls
56 lines (47 loc) · 1.56 KB
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
/**
* A simple container as an example of the ArrayLike spec
*
* triplet objects support length, index access, forEach, reverse, toString, and join.
*/
function triplet(a, b, c) {
if(!(this instanceof triplet)) return new triplet(a, b, c);
this["0"] = a;
this["1"] = b;
this["2"] = c;
}
// ** marker that we implement Array methods **
triplet.prototype.__ArrayLike = true;
// we define length with a getter/setter to ensure that it remains "3"
Object.defineProperty(triplet.prototype, "length", {
enumerable: true,
configurable: true,
get: function() { return 3; },
set: function(v) {
if(v != 3) {
throw "cannot set length to " + v + ", triplet must be length 3";
}
}
});
triplet.prototype.forEach = function(fn) {
fn(this["0"]);
fn(this["1"]);
fn(this["2"]);
}
triplet.prototype.join = function(sep) {
var s = sep || ",";
return "" + this["0"] + s + this["1"] + s + this["2"];
}
triplet.prototype.toString = function() {
return this.join();
}
// length + splice is often used for duck-typing, so it's good to implement
// (eg: implementing splice changes js/console output to look like an array)
triplet.prototype.splice = function() {
throw "cannot splice triplet - must be length 3";
}
// The EcmaScript standard specifically allows array-like objects
// to use the Array prototype methods. For more info, see
// http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf
triplet.prototype.reverse = function(args) {
return Array.prototype.reverse.apply(this, arguments);
}