Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Commit

Permalink
Remove the try…catch block from the getUTC* feature test. Rename …
Browse files Browse the repository at this point in the history
…`hasOwnProperty` to `isPropertyOf` to avoid a shadowing bug in the BlackBerry browser.
  • Loading branch information
Kit Cambridge committed Apr 15, 2012
1 parent da4db29 commit 8d6db76
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions lib/json3.js
Expand Up @@ -9,7 +9,7 @@
} }
})(this, function (exports) { })(this, function (exports) {
// Convenience aliases. // 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` // Feature tests to determine whether the native `JSON.stringify` and `parse`
// implementations are spec-compliant. Based on work by Ken Snyder. // implementations are spec-compliant. Based on work by Ken Snyder.
Expand All @@ -19,9 +19,9 @@
var serialized = '{"result":[1,true,false,null,"\\u0000\\b\\n\\f\\r\\t"]}', value = new Date(-3509827334573292), toJSON, original; 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 // The `Date#getUTC{FullYear, Month, Date}` methods return nonsensical
// results for certain dates in Opera >= 10.54. // results for certain dates in Opera >= 10.54.
try { getUTCSupported = typeof value.getUTCFullYear == "function" && value.getUTCFullYear() == -109252 &&
getUTCSupported = value.getUTCFullYear() == -109252 && value.getUTCMonth() === 0 && value.getUTCDate() == 1; typeof value.getUTCMonth == "function" && value.getUTCMonth() === 0 &&
} catch (exception) {} typeof value.getUTCDate == "function" && value.getUTCDate() == 1;
value = null; value = null;
// Test `JSON.stringify`. // Test `JSON.stringify`.
if (stringifySupported) { if (stringifySupported) {
Expand Down Expand Up @@ -119,21 +119,21 @@
} }
})(); })();


// **hasOwnProperty** determines if a property is a direct property of the // Internal: Determines if a property is a direct property of the given
// given object. Delegates to the native `Object#hasOwnProperty` method if // object. Delegates to the native `Object#hasOwnProperty` method if
// available. // available.
if (typeof hasOwnProperty != "function") { if (typeof isPropertyOf != "function" || typeof isPropertyOf.call != "function") {
hasOwnProperty = (function () { isPropertyOf = (function () {
// Capture a reference to the top-level `Object` constructor. // 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__ = { if ((members.__proto__ = null, members.__proto__ = {
// The internal *proto* property cannot be set multiple times in recent // The internal *proto* property cannot be set multiple times in recent
// versions of Mozilla Firefox and SeaMonkey. // versions of Mozilla Firefox and SeaMonkey.
"toString": 1 "toString": 1
}, members).toString != getClass) { }, members).toString != getClass) {
// Safari <= 2.0.3 doesn't implement `Object#hasOwnProperty`, but // Safari <= 2.0.3 doesn't implement `Object#hasOwnProperty`, but
// supports the mutable *proto* property. // 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 // Capture and break the object's prototype chain (see section 8.6.2
// of the ES 5.1 spec). The parenthesized expression prevents an // of the ES 5.1 spec). The parenthesized expression prevents an
// unsafe transformation by the Closure Compiler. // unsafe transformation by the Closure Compiler.
Expand All @@ -145,17 +145,17 @@
} else { } else {
// Use the `constructor` property to simulate `Object#hasOwnProperty` in // Use the `constructor` property to simulate `Object#hasOwnProperty` in
// other environments. // other environments.
hasOwnProperty = function hasOwnProperty(property) { isPropertyOf = function isPropertyOf(property) {
var parent = (this.constructor || constructor).prototype; var parent = (this.constructor || constructor).prototype;
return property in this && !(property in parent && this[property] === parent[property]); return property in this && !(property in parent && this[property] === parent[property]);
}; };
} }
members = null; 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. // environments. Each enumerated key is yielded to a `callback` function.
forEach = (function () { forEach = (function () {
var members, property, forEach, size = 0; var members, property, forEach, size = 0;
Expand All @@ -172,7 +172,7 @@
members = new Properties(); members = new Properties();
for (property in members) { for (property in members) {
// Ignore all other properties inherited from `Object.prototype`. // Ignore all other properties inherited from `Object.prototype`.
if (hasOwnProperty.call(members, property)) { if (isPropertyOf.call(members, property)) {
size += 1; size += 1;
} }
} }
Expand All @@ -189,14 +189,14 @@
for (property in object) { for (property in object) {
// Gecko <= 1.0 enumerates the `prototype` property of functions under // Gecko <= 1.0 enumerates the `prototype` property of functions under
// certain conditions; IE does not. // certain conditions; IE does not.
if (!(isFunction && property == "prototype") && hasOwnProperty.call(object, property)) { if (!(isFunction && property == "prototype") && isPropertyOf.call(object, property)) {
callback(property); callback(property);
} }
} }
// Manually invoke the callback for each non-enumerable property. // Manually invoke the callback for each non-enumerable property.
for (length = members.length; length--;) { for (length = members.length; length--;) {
property = members[length]; property = members[length];
if (hasOwnProperty.call(object, property)) { if (isPropertyOf.call(object, property)) {
callback(property); callback(property);
} }
} }
Expand All @@ -210,7 +210,7 @@
// Store each property name to prevent double enumeration. The // Store each property name to prevent double enumeration. The
// `prototype` property of functions is not enumerated due to cross- // `prototype` property of functions is not enumerated due to cross-
// environment inconsistencies. // 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); callback(property);
} }
} }
Expand All @@ -220,13 +220,13 @@
forEach = function forEach(object, callback) { forEach = function forEach(object, callback) {
var isFunction = getClass.call(object) == "[object Function]", property, isConstructor; var isFunction = getClass.call(object) == "[object Function]", property, isConstructor;
for (property in object) { 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); callback(property);
} }
} }
// Manually invoke the callback for the `constructor` property due to // Manually invoke the callback for the `constructor` property due to
// cross-environment inconsistencies. // cross-environment inconsistencies.
if (isConstructor || hasOwnProperty.call(object, "constructor")) { if (isConstructor || isPropertyOf.call(object, "constructor")) {
callback("constructor"); callback("constructor");
} }
}; };
Expand Down Expand Up @@ -310,7 +310,7 @@
function serialize(property, object, callback, properties, whitespace, indentation, stack) { function serialize(property, object, callback, properties, whitespace, indentation, stack) {
var value = object[property], className, year, month, day, date, results, element, index, length, prefix, any; var value = object[property], className, year, month, day, date, results, element, index, length, prefix, any;
if (typeof value == "object" && value) { 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) { if (value > -1 / 0 && value < 1 / 0) {
// Dates are serialized according to the `Date#toJSON` method // 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 // specified in ES 5.1 section 15.9.5.44. See section 15.9.1.15
Expand Down

0 comments on commit 8d6db76

Please sign in to comment.