]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Avoid obj.hasOwnProperty in favor of Object.prototype.hasOwnPrototype 1874/head
authorIskren Chernev <iskren.chernev@gmail.com>
Wed, 20 Aug 2014 22:10:18 +0000 (01:10 +0300)
committerIskren Chernev <iskren.chernev@gmail.com>
Wed, 20 Aug 2014 22:10:18 +0000 (01:10 +0300)
Reimplement #1818, with replacing all hasOwnPrototype and adding a test.

moment.js
test/moment/is_moment.js

index 1b6457da0e8d9f519c682d233d54bd2937153d9e..99dde3c1526d5475c587094839d2fa94ca49912e 100644 (file)
--- a/moment.js
+++ b/moment.js
@@ -15,6 +15,7 @@
         globalScope = typeof global !== 'undefined' ? global : this,
         oldGlobalMoment,
         round = Math.round,
+        hasOwnProperty = Object.prototype.hasOwnProperty,
         i,
 
         YEAR = 0,
         }
     }
 
+    function hasOwnProp(a, b) {
+        return hasOwnProperty.call(a, b);
+    }
+
     function defaultParsingFlags() {
         // We need to deep clone this object, and es5 standard is not very
         // helpful.
 
     function extend(a, b) {
         for (var i in b) {
-            if (b.hasOwnProperty(i)) {
+            if (hasOwnProp(b, i)) {
                 a[i] = b[i];
             }
         }
 
-        if (b.hasOwnProperty('toString')) {
+        if (hasOwnProp(b, 'toString')) {
             a.toString = b.toString;
         }
 
-        if (b.hasOwnProperty('valueOf')) {
+        if (hasOwnProp(b, 'valueOf')) {
             a.valueOf = b.valueOf;
         }
 
             prop;
 
         for (prop in inputObject) {
-            if (inputObject.hasOwnProperty(prop)) {
+            if (hasOwnProp(inputObject, prop)) {
                 normalizedProp = normalizeUnits(prop);
                 if (normalizedProp) {
                     normalizedInput[normalizedProp] = inputObject[prop];
 
         ret = new Duration(duration);
 
-        if (moment.isDuration(input) && input.hasOwnProperty('_locale')) {
+        if (moment.isDuration(input) && hasOwnProp(input, '_locale')) {
             ret._locale = input._locale;
         }
 
     // compare moment object
     moment.isMoment = function (obj) {
         return obj instanceof Moment ||
-            (obj != null &&  obj.hasOwnProperty('_isAMomentObject'));
+            (obj != null && hasOwnProp(obj, '_isAMomentObject'));
     };
 
     // for typechecking Duration objects
     }
 
     for (i in unitMillisecondFactors) {
-        if (unitMillisecondFactors.hasOwnProperty(i)) {
+        if (hasOwnProp(unitMillisecondFactors, i)) {
             makeDurationGetter(i.toLowerCase());
         }
     }
index 1ff80bbbb22a8d0f19ad51e8ba3d5fdf279afb67..3dd61e446926ba942401402c79fe37a60c5825c6 100644 (file)
@@ -38,6 +38,17 @@ exports.isMoment = {
         test.ok(!moment.isMoment(null), 'null is not moment object');
         test.ok(!moment.isMoment(undefined), 'undefined is not moment object');
 
+        test.done();
+    },
+
+    'is moment with hacked hasOwnProperty': function (test) {
+        var obj = {};
+        // HACK to suppress jshint warning about bad property name
+        obj['hasOwnMoney'.replace('Money', 'Property')] = function () {
+            return true;
+        };
+
+        test.ok(!moment.isMoment(obj), 'isMoment works even if passed object has a wrong hasOwnProperty implementation (ie8)');
         test.done();
     }
 };