Skip to content

Commit

Permalink
Fixed issue 1 and removed version numbers these are static feature sets.
Browse files Browse the repository at this point in the history
Also, everything now checks out on JSLint.
  • Loading branch information
eligrey committed Mar 21, 2010
1 parent de339af commit 67246c6
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 66 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
*Version 0.0.5*
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.

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.

**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) `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.
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 if set
to `false`. 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
----------------

Xccessors Legacy implements the legacy accessor methods (object.__define[GS]etter__ and object.__lookup[GS]etter__) in browsers that support the standard ECMAScript 5 accessor methods.
Xccessors Legacy implements the legacy accessor methods (`object.__define[GS]etter__`
and `object.__lookup[GS]etter__`) in browsers that support the standard
ECMAScript 5 accessor methods.
49 changes: 30 additions & 19 deletions xccessors-legacy.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
/*
* 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__
* in browsers that have ECMAScript 5 accessor support but not the legacy methods.
*
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
* Xccessors Legacy: Cross-browser legacy non-standard accessors
* http://github.com/eligrey/Xccessors
*
* 2010-03-21
*
* By Elijah Grey, http://eligrey.com
*
* A shim that implements __defineGetter__, __defineSetter__, __lookupGetter__,
* and __lookupSetter__
* in browsers that have ECMAScript 5 accessor support but not the legacy methods.
*
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/

/*global Element, Window, HTMLDocument */

/*jslint white: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true,
strict: true, newcap: true, immed: true, maxlen: 90 */

"use strict";

(function () {
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
// 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",

extendMethod = function (method, fun) {
var i = len;
if (!(method in {}))
while (i--)
if (!(method in {})) {
while (i--) {
extend[i][proto][method] = fun;
}
}
}
};

if (defineProp) {
extendMethod(methods[0], function (prop, fun) { // __defineGetter__
Expand All @@ -55,4 +66,4 @@
getProp(this.constructor[proto], prop).set; // look in prototype too
});
}
})();
}());
99 changes: 57 additions & 42 deletions xccessors-standard.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,75 @@
/*
* Xccessors Standard v0.0.5: Cross-browser ECMAScript 5 accessors
* http://code.eligrey.com/xccessors/standard/
*
* 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 COPYING.md
* Xccessors Standard: Cross-browser ECMAScript 5 accessors
* http://github.com/eligrey/Xccessors
*
* 2010-03-21
*
* 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 COPYING.md
*/

/*jslint white: true, onevar: true, undef: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */
/*jslint white: true, onevar: true, undef: true, eqeqeq: true, plusplus: true,
bitwise: true, regexp: true, strict: true, newcap: true, immed: true, maxlen: 90 */

"use strict";

(function () {
var ObjectProto = Object.prototype,
hasOwnProp = function (obj, prop) {
return ObjectProto.hasOwnProperty.call(obj, prop);
},
True = true,
True = !0,
False = !1,
defineGetter = ObjectProto.__defineGetter__,
defineSetter = ObjectProto.__defineSetter__,
lookupGetter = ObjectProto.__lookupGetter__,
lookupSetter = ObjectProto.__lookupGetter__;
lookupSetter = ObjectProto.__lookupGetter__,
hasOwnProp = ObjectProto.hasOwnProperty;

if (defineGetter && defineSetter && lookupGetter && lookupSetter) {

if (!Object.defineProperty) {
Object.defineProperty = function (obj, prop, descriptor) {
if (arguments.length < 3) { // all arguments required
throw new TypeError("Argument not optional");
throw new TypeError("Arguments not optional");
}

prop += ""; // convert prop to string

if (hasOwnProp(descriptor, "value")) {
!lookupGetter.call(obj, prop) &&
!lookupSetter.call(obj, prop) &&
// data property defined and no pre-existing accessors
(obj[prop] = descriptor.value);
if (hasOwnProp.call(descriptor, "value")) {
if (!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"))) {
if ((hasOwnProp.call(descriptor, "get") ||
hasOwnProp.call(descriptor, "set")))
{
// descriptor has a value prop but accessor already exists
throw new TypeError("Object doesn't support this action");
throw new TypeError("Cannot specify an accessor and a value");
}
}

// can't implement these features so throw a RangeError if any are true
if (descriptor.writable || descriptor.enumerable || descriptor.configurable) {
throw new RangeError("This implementation of Object.defineProperty does not support configurable, enumerable, or writable.");
// can't switch off these features in ECMAScript 3
// so throw a TypeError if any are false
if (descriptor.writable === False || descriptor.enumerable === False ||
descriptor.configurable === False)
{
throw new TypeError(
"This implementation of Object.defineProperty does not support" +
"false for configurable, enumerable, or writable."
);
}

descriptor.get &&
if (descriptor.get) {
defineGetter.call(obj, prop, descriptor.get);

descriptor.set &&
}
if (descriptor.set) {
defineSetter.call(obj, prop, descriptor.set);
}

return obj;
};
Expand All @@ -67,8 +78,9 @@
if (!Object.getOwnPropertyDescriptor) {
Object.getOwnPropertyDescriptor = function (obj, prop) {
if (arguments.length < 2) { // all arguments required
throw new TypeError("Argument not optional");
throw new TypeError("Arguments not optional");
}

prop += ""; // convert prop to string

var descriptor = {
Expand All @@ -79,23 +91,26 @@
getter = lookupGetter.call(obj, prop),
setter = lookupSetter.call(obj, prop);

if (!hasOwnProp(obj, prop)) { // property doesn't exist or is inherited
if (!hasOwnProp.call(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;
}

// there is an accessor, remove descriptor.writable; populate descriptor.get and descriptor.set
// there is an accessor, remove descriptor.writable;
// populate descriptor.get and descriptor.set
delete descriptor.writable;
descriptor.get = descriptor.set = undefined;

getter &&
(descriptor.get = getter);

setter &&
(descriptor.set = setter);
if (getter) {
descriptor.get = getter;
}
if (setter) {
descriptor.set = setter;
}

return descriptor;
};
Expand All @@ -104,7 +119,7 @@
if (!Object.defineProperties) {
Object.defineProperties = function (obj, props) {
for (var prop in props) {
if (hasOwnProp(props, prop)) {
if (hasOwnProp.call(props, prop)) {
Object.defineProperty(obj, prop, props[prop]);
}
}
Expand Down

0 comments on commit 67246c6

Please sign in to comment.