Pārlūkot izejas kodu

fix Path.set: only add when key not found in prototype chain

Evan You 11 gadi atpakaļ
vecāks
revīzija
c738015420
2 mainītis faili ar 17 papildinājumiem un 8 dzēšanām
  1. 1 1
      src/parse/path.js
  2. 16 7
      test/unit/specs/parse/path_spec.js

+ 1 - 1
src/parse/path.js

@@ -291,7 +291,7 @@ exports.set = function (obj, path, val) {
     }
     }
   }
   }
   key = path[i]
   key = path[i]
-  if (obj.hasOwnProperty(key)) {
+  if (key in obj) {
     obj[key] = val
     obj[key] = val
   } else {
   } else {
     add(obj, key, val)
     add(obj, key, val)

+ 16 - 7
test/unit/specs/parse/path_spec.js

@@ -97,16 +97,25 @@ describe('Path Parser', function () {
     expect(obj.a.b.c).toBe(12345)
     expect(obj.a.b.c).toBe(12345)
   })
   })
 
 
-  it('set invalid', function () {
-    var res = Path.set({}, 'ab[c]d', 123)
-    expect(res).toBe(false)
-  })
-
-  it('force set', function () {
+  it('set non-existent', function () {
     var target = {}
     var target = {}
-    var res = Path.set(target, 'a.b.c', 123, true)
+    var res = Path.set(target, 'a.b.c', 123)
     expect(res).toBe(true)
     expect(res).toBe(true)
     expect(target.a.b.c).toBe(123)
     expect(target.a.b.c).toBe(123)
   })
   })
 
 
+  it('set on prototype chain', function () {
+    var parent = { a: {} }
+    var target = Object.create(parent)
+    var res = Path.set(target, 'a.b.c', 123)
+    expect(res).toBe(true)
+    expect(target.hasOwnProperty('a')).toBe(false)
+    expect(parent.a.b.c).toBe(123)
+  })
+
+  it('set invalid', function () {
+    var res = Path.set({}, 'ab[c]d', 123)
+    expect(res).toBe(false)
+  })
+
 })
 })