From c5344d53366294f0236732233ba559a13eb8cb63 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 2 Mar 2016 07:54:27 -0800 Subject: [PATCH] Ensure `_.has` returns `false` for nested inherited properties. [closes #2073] --- lodash.js | 14 ++++++++++---- test/test.js | 11 +++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lodash.js b/lodash.js index 2aa231b17..a0f9b3f29 100644 --- a/lodash.js +++ b/lodash.js @@ -5086,10 +5086,16 @@ var result = hasFunc(object, path); if (!result && !isKey(path)) { path = baseCastPath(path); - object = parent(object, path); - if (object != null) { - path = last(path); - result = hasFunc(object, path); + + var index = -1, + length = path.length; + + while (object != null && ++index < length) { + var key = path[index]; + if (!(result = hasFunc(object, key))) { + break; + } + object = object[key]; } } var length = object ? object.length : undefined; diff --git a/test/test.js b/test/test.js index dc8aa1705..1705c83fb 100644 --- a/test/test.js +++ b/test/test.js @@ -7096,6 +7096,17 @@ }); }); + QUnit.test('`_.' + methodName + '` should return `' + (isHas ? 'false' : 'true') + '` for nested inherited properties', function(assert) { + assert.expect(2); + + function Foo() {} + Foo.prototype.a = { 'b': 1 }; + + lodashStable.each(['a.b', ['a', 'b']], function(path) { + assert.strictEqual(func(new Foo, path), !isHas); + }); + }); + QUnit.test('`_.' + methodName + '` should return `true` for index values within bounds for arrays, `arguments` objects, and strings', function(assert) { assert.expect(1);