Permalink
Browse files

Optimization and JSLint changes

  • Loading branch information...
1 parent 66491a7 commit de339afc5966c6db55541dc17a3167565b4e4465 Elijah Grey committed Sep 4, 2009
Showing with 105 additions and 75 deletions.
  1. +5 −3 README.md
  2. +15 −9 xccessors-legacy.js
  3. +85 −63 xccessors-standard.js
View
@@ -1,11 +1,13 @@
-Xccessors (cross-browser accessors) is an open source JavaScript shim that implements the legacy or standard methods for defining and looking up accessors (getters and setters) of objects.
+*Version 0.0.5*
-**Please note**: Internet Explorer 8 only supports getters and setters on the DOM and it’s prototypes (document, Element.prototype, etc.) and the window object. As long as you are setting an accessor on the window object, a DOM element, or a DOM prototype, Xccessors should work fine on every browser.
+Xccessors (cross-browser accessors) is a JavaScript shim that implements the legacy or standard methods for defining and looking up accessors (getters and setters) of objects.
+
+**Please note**: Internet Explorer 8 only supports getters and setters on the DOM, it's prototypes (document, Element.prototype, etc.), and the window object. As long as you are setting an accessor on the window object, a DOM element, or a DOM prototype, Xccessors should work fine in every browser.
Xccessors Standard
------------------
-Xcessors Standard implements ECMAScript 5’s (formerly 3.1) accessors standard (Object.defineProperty and Object.getOwnPropertyDescriptor) in browsers that support the legacy non-standard accessor methods. Everything behaves according to the ECMAScript 5 standard except the configurable, enumerable, and writable options. It is impossible to implement these options without native support for Object.defineProperty. The good news is that you are never going to use them if you are changing a JavaScript program from the legacy to standard methods.
+Xcessors Standard implements ECMAScript 5's (formerly 3.1) `Object.defineProperty`, `Object.defineProperties`, and `Object.getOwnPropertyDescriptor` in browsers that support the legacy non-standard accessor methods. Everything behaves according to the ECMAScript 5 standard except the configurable, enumerable, and writable options. It is impossible to implement these options without native support for Object.defineProperty. The good news is that you are never going to use them if you are changing a JavaScript program from the legacy to standard methods.
Xccessors Legacy
----------------
View
@@ -1,7 +1,9 @@
/*
-* Xccessors Legacy v0.0.4: Cross-browser legacy non-standard accessors
+* Xccessors Legacy v0.0.5: Cross-browser legacy non-standard accessors
* http://code.eligrey.com/xccessors/legacy/
*
+* 2009-09-04
+*
* By Elijah Grey, http://eligrey.com
*
* A shim that implements __defineGetter__, __defineSetter__, __lookupGetter__, and __lookupSetter__
@@ -11,21 +13,25 @@
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/
(function () {
- var defineProp = Object.defineProperty,
- getProp = Object.getOwnPropertyDescriptor,
+ var
+ defineProp = Object.defineProperty,
+ getProp = Object.getOwnPropertyDescriptor,
// methods being implemented
- methods = ["__defineGetter__", "__defineSetter__", "__lookupGetter__", "__lookupSetter__"],
+ methods = ["__defineGetter__", "__defineSetter__", "__lookupGetter__", "__lookupSetter__"],
// objects to implement legacy methods onto their prototypes
- extend = [Object, String, Array, Function, Boolean, Number,
- RegExp, Date, Error, Element, Window, HTMLDocument], // Object.prototype[method] doesn't work on everything for IE
+ // Object.prototype[method] doesn't work on everything for IE
+ extend = [Object, String, Array, Function, Boolean, Number,
+ RegExp, Date, Error, Element, Window, HTMLDocument],
+ len = extend.length,
- proto = "prototype";
+ proto = "prototype",
- function extendMethod(method, fun) {
+ extendMethod = function (method, fun) {
+ var i = len;
if (!(method in {}))
- for (var i=0; i<extend.length; i++)
+ while (i--)
extend[i][proto][method] = fun;
}
View
@@ -1,93 +1,115 @@
/*
-* Xccessors Standard v0.0.4: Cross-browser ECMAScript 5 accessors
+* Xccessors Standard v0.0.5: Cross-browser ECMAScript 5 accessors
* http://code.eligrey.com/xccessors/standard/
-*
-* 2009-06-16
-*
+*
+* 2009-09-04
+*
* By Elijah Grey, http://eligrey.com
-*
+*
* A shim that partially implements Object.defineProperty, Object.getOwnPropertyDescriptor, and Object.defineProperties
* in browsers that have legacy __(define|lookup)[GS]etter__ support.
-*
+*
* License: GNU GPL v3 and the X11/MIT license
-* See http://eligrey.com/blog/about/license
+* See COPYING.md
*/
+/*jslint white: true, onevar: true, undef: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */
-(function() {
+"use strict";
+
+(function () {
var ObjectProto = Object.prototype,
hasOwnProp = function (obj, prop) {
return ObjectProto.hasOwnProperty.call(obj, prop);
- };
+ },
+ True = true,
+ defineGetter = ObjectProto.__defineGetter__,
+ defineSetter = ObjectProto.__defineSetter__,
+ lookupGetter = ObjectProto.__lookupGetter__,
+ lookupSetter = ObjectProto.__lookupGetter__;
- if (ObjectProto.__defineGetter__ && ObjectProto.__defineSetter__ && ObjectProto.__lookupGetter__ && ObjectProto.__lookupSetter__) {
-
- if (!Object.defineProperty) Object.defineProperty = function (obj, prop, descriptor) {
- if (arguments.length < 3) // all arguments required
- throw new TypeError("Argument not optional");
+ if (defineGetter && defineSetter && lookupGetter && lookupSetter) {
- prop += ""; // convert prop to string
+ if (!Object.defineProperty) {
+ Object.defineProperty = function (obj, prop, descriptor) {
+ if (arguments.length < 3) { // all arguments required
+ throw new TypeError("Argument not optional");
+ }
+ prop += ""; // convert prop to string
- if (typeof descriptor == "object") { // check for descriptor object
if (hasOwnProp(descriptor, "value")) {
- if (!ObjectProto.__lookupGetter__.call(obj, prop) && !ObjectProto.__lookupSetter__.call(obj, prop))
- obj[prop] = descriptor.value; // data property defined and no pre-existing accessors
+ !lookupGetter.call(obj, prop) &&
+ !lookupSetter.call(obj, prop) &&
+ // data property defined and no pre-existing accessors
+ (obj[prop] = descriptor.value);
- if((hasOwnProp(descriptor, "get") || hasOwnProp(descriptor, "set"))) // descriptor has a value prop but accessor already exists
+ if ((hasOwnProp(descriptor, "get") || hasOwnProp(descriptor, "set"))) {
+ // descriptor has a value prop but accessor already exists
throw new TypeError("Object doesn't support this action");
+ }
}
// can't implement these features so throw a RangeError if any are true
- if (descriptor.writable === true || descriptor.enumerable === true || descriptor.configurable === true)
+ if (descriptor.writable || descriptor.enumerable || descriptor.configurable) {
throw new RangeError("This implementation of Object.defineProperty does not support configurable, enumerable, or writable.");
+ }
+
+ descriptor.get &&
+ defineGetter.call(obj, prop, descriptor.get);
+
+ descriptor.set &&
+ defineSetter.call(obj, prop, descriptor.set);
+
+ return obj;
+ };
+ }
- else if (typeof descriptor.get == "function")
- ObjectProto.__defineGetter__.call(obj, prop, descriptor.get);
-
- if (typeof descriptor.set == "function")
- ObjectProto.__defineSetter__.call(obj, prop, descriptor.set);
- }
- return obj;
- };
-
-
- if (!Object.getOwnPropertyDescriptor) Object.getOwnPropertyDescriptor = function (obj, prop) {
- if (arguments.length < 2) // all arguments required
- throw new TypeError("Argument not optional")
-
- prop += ""; // convert prop to string
-
- var descriptor = {configurable: true, enumerable: true, writable: true, value: undefined},
- getter = ObjectProto.__lookupGetter__.call(obj, prop),
- setter = ObjectProto.__lookupSetter__.call(obj, prop);
+ if (!Object.getOwnPropertyDescriptor) {
+ Object.getOwnPropertyDescriptor = function (obj, prop) {
+ if (arguments.length < 2) { // all arguments required
+ throw new TypeError("Argument not optional");
+ }
+ prop += ""; // convert prop to string
+
+ var descriptor = {
+ configurable: True,
+ enumerable : True,
+ writable : True
+ },
+ getter = lookupGetter.call(obj, prop),
+ setter = lookupSetter.call(obj, prop);
+
+ if (!hasOwnProp(obj, prop)) { // property doesn't exist or is inherited
+ return descriptor;
+ }
+ if (!getter && !setter) { // not an accessor so return prop
+ descriptor.value = obj[prop];
+ return descriptor;
+ }
- if (!hasOwnProp(obj, prop)) // property doesn't exist or is inherited
- return descriptor;
+ // there is an accessor, remove descriptor.writable; populate descriptor.get and descriptor.set
+ delete descriptor.writable;
+ descriptor.get = descriptor.set = undefined;
- if (!getter && !setter) { // not an accessor so return prop
- descriptor.value = obj[prop];
+ getter &&
+ (descriptor.get = getter);
+
+ setter &&
+ (descriptor.set = setter);
+
return descriptor;
- }
-
- // there is an accessor, remove descriptor.writable; populate descriptor.get and descriptor.set
- delete descriptor.writable;
- delete descriptor.value;
- descriptor.get = descriptor.set = undefined;
-
- if (getter)
- descriptor.get = getter;
-
- if (setter)
- descriptor.set = setter;
-
- return descriptor;
- };
+ };
+ }
- if (!Object.defineProperties) Object.defineProperties = function (obj, props) {
- for (var prop in props)
- if (hasOwnProp(props, prop))
- Object.defineProperty(obj, prop, props[prop]);
+ if (!Object.defineProperties) {
+ Object.defineProperties = function (obj, props) {
+ for (var prop in props) {
+ if (hasOwnProp(props, prop)) {
+ Object.defineProperty(obj, prop, props[prop]);
+ }
+ }
+ };
}
}
-})();
+}());

0 comments on commit de339af

Please sign in to comment.