tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Array-of-length-setter.js (662B)


      1 // Array.of calls a "length" setter if one is present.
      2 
      3 var hits = 0;
      4 var lastObj = null, lastVal = undefined;
      5 function setter(v) {
      6    hits++;
      7    lastObj = this;
      8    lastVal = v;
      9 }
     10 
     11 // when the setter is on the new object
     12 function Pack() {
     13    Object.defineProperty(this, "length", {set: setter});
     14 }
     15 Pack.of = Array.of;
     16 var pack = Pack.of("wolves", "cards", "cigarettes", "lies");
     17 assertEq(lastObj, pack);
     18 assertEq(lastVal, 4);
     19 
     20 // when the setter is on the new object's prototype
     21 function Bevy() {}
     22 Object.defineProperty(Bevy.prototype, "length", {set: setter});
     23 Bevy.of = Array.of;
     24 var bevy = Bevy.of("quail");
     25 assertEq(lastObj, bevy);
     26 assertEq(lastVal, 1);