Sean Wilkinson avatar Sean Wilkinson committed fd2e8b8

Revisited "hiding" with ES5 property attributes

Comments (0)

Files changed (3)

         "./src/quanah.js"
     ],
     "name": "quanah",
-    "version": "0.1.2"
+    "version": "0.1.3"
 }
         "mocha": "1.8.1"
     },
     "engines": {
-        "node": ">= 0.8.x",
-        "npm": ">= 1.1.2"
+        "node": ">= 0.6.x",
+        "npm": ">= 1.1.x"
     },
     "license": "Apache License 2.0",
     "main": "src/quanah.js",
     "scripts": {
         "test": "./node_modules/mocha/bin/mocha"
     },
-    "version": "0.1.2"
+    "version": "0.1.3"
 }
 //  This file is also available from git.io/q.js and bit.ly/quanahjs :-P
 //
 //                                                      ~~ (c) SRW, 14 Nov 2012
-//                                                  ~~ last updated 18 Mar 2013
+//                                                  ~~ last updated 22 Mar 2013
 
 (function () {
     'use strict';
 
     /*properties
         Q, add_to_queue, apply, avar, call, can_run_remotely, comm, concat,
-        def, done, epitaph, exit, exports, f, fail, hasOwnProperty, key,
-        length, on, onerror, prototype, push, queue, random, ready, revive,
-        run_remotely, shift, slice, stay, toString, unshift, val, valueOf,
-        when, x
+        configurable, def, defineProperty, done, epitaph, exit, exports, f,
+        fail, hasOwnProperty, key, length, on, onerror, prototype, push, queue,
+        random, ready, revive, run_remotely, shift, slice, stay, toString,
+        unshift, val, value, valueOf, when, writable, x
     */
 
  // Prerequisites
 
  // Out-of-scope definitions
 
-    Object.prototype.Q = function (f) {
+    (function (method_Q) {
+     // This function uses ES5.1 property descriptors if they are available in
+     // order to hide "Method Q" from `for .. in` loops. I did this originally
+     // as an attempt to reduce conflicts with jQuery et al., but it also has
+     // security implications that I will need to revisit in the near future.
+        if (Object.hasOwnProperty('defineProperty')) {
+            Object.defineProperty(Object.prototype, 'Q', {
+             // NOTE: ES5.1 property descriptors' values default to `false`.
+                configurable: true,
+                //enumerable: false,
+                writable: true,
+                value: method_Q
+            });
+        } else {
+            Object.prototype.Q = method_Q;
+        }
+        return;
+    }(function method_Q(f) {
      // This function acts as a "namespace" for Quanah as well as its most
      // important syntactic sugar -- "Method Q". This function can be used as
      // a method of any native value except `null` or `undefined`. It expects
      // its argument to be a function of a single variable or else an avar with
      // such a function as its `val`.
+        if (Object.prototype.Q !== method_Q) {
+            throw new Error('"Method Q" is not available.');
+        }
         var x = (this instanceof AVar) ? this : avar({val: this});
         x.comm({'add_to_queue': f});
         return x;
-    };
+    }));
 
     Object.prototype.Q.avar = avar;
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.