Permalink
Browse files

Remove the `try…catch` block from the `getUTC*` feature test. Rename …

…`hasOwnProperty` to `isPropertyOf` to avoid a shadowing bug in the BlackBerry browser.
  • Loading branch information...
kitcambridge committed Apr 15, 2012
1 parent da4db29 commit 8d6db7640e83b81bd4b2f962be8e161bc31b79a0
Showing with 20 additions and 20 deletions.
  1. +20 −20 lib/json3.js
View
@@ -9,7 +9,7 @@
}
})(this, function (exports) {
// Convenience aliases.
- var getClass = {}.toString, hasOwnProperty = {}.hasOwnProperty, getUTCSupported = false, stringifySupported, parseSupported, forEach;
+ var getClass = {}.toString, isPropertyOf = {}.hasOwnProperty, getUTCSupported = false, stringifySupported, parseSupported, forEach;
// Feature tests to determine whether the native `JSON.stringify` and `parse`
// implementations are spec-compliant. Based on work by Ken Snyder.
@@ -19,9 +19,9 @@
var serialized = '{"result":[1,true,false,null,"\\u0000\\b\\n\\f\\r\\t"]}', value = new Date(-3509827334573292), toJSON, original;
// The `Date#getUTC{FullYear, Month, Date}` methods return nonsensical
// results for certain dates in Opera >= 10.54.
- try {
- getUTCSupported = value.getUTCFullYear() == -109252 && value.getUTCMonth() === 0 && value.getUTCDate() == 1;
- } catch (exception) {}
+ getUTCSupported = typeof value.getUTCFullYear == "function" && value.getUTCFullYear() == -109252 &&
+ typeof value.getUTCMonth == "function" && value.getUTCMonth() === 0 &&
+ typeof value.getUTCDate == "function" && value.getUTCDate() == 1;
value = null;
// Test `JSON.stringify`.
if (stringifySupported) {
@@ -119,21 +119,21 @@
}
})();
- // **hasOwnProperty** determines if a property is a direct property of the
- // given object. Delegates to the native `Object#hasOwnProperty` method if
+ // Internal: Determines if a property is a direct property of the given
+ // object. Delegates to the native `Object#hasOwnProperty` method if
// available.
- if (typeof hasOwnProperty != "function") {
- hasOwnProperty = (function () {
+ if (typeof isPropertyOf != "function" || typeof isPropertyOf.call != "function") {
+ isPropertyOf = (function () {
// Capture a reference to the top-level `Object` constructor.
- var hasOwnProperty, members = {}, constructor = members.constructor;
+ var isPropertyOf, members = {}, constructor = members.constructor;
if ((members.__proto__ = null, members.__proto__ = {
// The internal *proto* property cannot be set multiple times in recent
// versions of Mozilla Firefox and SeaMonkey.
"toString": 1
}, members).toString != getClass) {
// Safari <= 2.0.3 doesn't implement `Object#hasOwnProperty`, but
// supports the mutable *proto* property.
- hasOwnProperty = function hasOwnProperty(property) {
+ isPropertyOf = function isPropertyOf(property) {
// Capture and break the object's prototype chain (see section 8.6.2
// of the ES 5.1 spec). The parenthesized expression prevents an
// unsafe transformation by the Closure Compiler.
@@ -145,17 +145,17 @@
} else {
// Use the `constructor` property to simulate `Object#hasOwnProperty` in
// other environments.
- hasOwnProperty = function hasOwnProperty(property) {
+ isPropertyOf = function isPropertyOf(property) {
var parent = (this.constructor || constructor).prototype;
return property in this && !(property in parent && this[property] === parent[property]);
};
}
members = null;
- return hasOwnProperty;
+ return isPropertyOf;
})();
}
- // **forEach** normalizes the `for...in` iteration algorithm across
+ // Internal: Normalizes the `for...in` iteration algorithm across
// environments. Each enumerated key is yielded to a `callback` function.
forEach = (function () {
var members, property, forEach, size = 0;
@@ -172,7 +172,7 @@
members = new Properties();
for (property in members) {
// Ignore all other properties inherited from `Object.prototype`.
- if (hasOwnProperty.call(members, property)) {
+ if (isPropertyOf.call(members, property)) {
size += 1;
}
}
@@ -189,14 +189,14 @@
for (property in object) {
// Gecko <= 1.0 enumerates the `prototype` property of functions under
// certain conditions; IE does not.
- if (!(isFunction && property == "prototype") && hasOwnProperty.call(object, property)) {
+ if (!(isFunction && property == "prototype") && isPropertyOf.call(object, property)) {
callback(property);
}
}
// Manually invoke the callback for each non-enumerable property.
for (length = members.length; length--;) {
property = members[length];
- if (hasOwnProperty.call(object, property)) {
+ if (isPropertyOf.call(object, property)) {
callback(property);
}
}
@@ -210,7 +210,7 @@
// Store each property name to prevent double enumeration. The
// `prototype` property of functions is not enumerated due to cross-
// environment inconsistencies.
- if (!(isFunction && property == "prototype") && !hasOwnProperty.call(members, property) && (members[property] = 1) && hasOwnProperty.call(object, property)) {
+ if (!(isFunction && property == "prototype") && !isPropertyOf.call(members, property) && (members[property] = 1) && isPropertyOf.call(object, property)) {
callback(property);
}
}
@@ -220,13 +220,13 @@
forEach = function forEach(object, callback) {
var isFunction = getClass.call(object) == "[object Function]", property, isConstructor;
for (property in object) {
- if (!(isFunction && property == "prototype") && hasOwnProperty.call(object, property) && !(isConstructor = property === "constructor")) {
+ if (!(isFunction && property == "prototype") && isPropertyOf.call(object, property) && !(isConstructor = property === "constructor")) {
callback(property);
}
}
// Manually invoke the callback for the `constructor` property due to
// cross-environment inconsistencies.
- if (isConstructor || hasOwnProperty.call(object, "constructor")) {
+ if (isConstructor || isPropertyOf.call(object, "constructor")) {
callback("constructor");
}
};
@@ -310,7 +310,7 @@
function serialize(property, object, callback, properties, whitespace, indentation, stack) {
var value = object[property], className, year, month, day, date, results, element, index, length, prefix, any;
if (typeof value == "object" && value) {
- if (getClass.call(value) == "[object Date]" && !hasOwnProperty.call(value, "toJSON")) {
+ if (getClass.call(value) == "[object Date]" && !isPropertyOf.call(value, "toJSON")) {
if (value > -1 / 0 && value < 1 / 0) {
// Dates are serialized according to the `Date#toJSON` method
// specified in ES 5.1 section 15.9.5.44. See section 15.9.1.15

0 comments on commit 8d6db76

Please sign in to comment.