Skip to content

Commit ffe5825

Browse files
committed
Merge pull request vuejs#1816 from kazupon/fix/utility
fix: ocurred error in 'set' and 'delete' utility functions
2 parents 7578178 + 2b79ebe commit ffe5825

File tree

13 files changed

+56
-22
lines changed

13 files changed

+56
-22
lines changed

src/compiler/compile-props.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ function makePropsLinkFn (props) {
185185

186186
function getDefault (vm, options) {
187187
// no default, return undefined
188-
if (!options.hasOwnProperty('default')) {
188+
if (!_.hasOwn(options, 'default')) {
189189
// absent boolean value defaults to false
190190
return options.type === Boolean
191191
? false

src/directives/internal/class.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ function stringToObject (value) {
6767
function contains (value, key) {
6868
return _.isArray(value)
6969
? value.indexOf(key) > -1
70-
: value.hasOwnProperty(key)
70+
: _.hasOwn(value, key)
7171
}

src/directives/public/el.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
}
1212
var id = this.id = _.camelize(this.arg)
1313
var refs = (this._scope || this.vm).$els
14-
if (refs.hasOwnProperty(id)) {
14+
if (_.hasOwn(refs, id)) {
1515
refs[id] = this.el
1616
} else {
1717
_.defineReactive(refs, id, this.el)

src/directives/public/for.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ module.exports = {
8787
var item = data[0]
8888
var convertedFromObject = this.fromObject =
8989
isObject(item) &&
90-
item.hasOwnProperty('$key') &&
91-
item.hasOwnProperty('$value')
90+
_.hasOwn(item, '$key') &&
91+
_.hasOwn(item, '$value')
9292

9393
var trackByKey = this.params.trackBy
9494
var oldFrags = this.frags
@@ -373,7 +373,7 @@ module.exports = {
373373
}
374374
} else {
375375
id = this.id
376-
if (value.hasOwnProperty(id)) {
376+
if (_.hasOwn(value, id)) {
377377
if (value[id] === null) {
378378
value[id] = frag
379379
} else {
@@ -430,7 +430,7 @@ module.exports = {
430430
var index = scope.$index
431431
// fix #948: avoid accidentally fall through to
432432
// a parent repeater which happens to have $key.
433-
var key = scope.hasOwnProperty('$key') && scope.$key
433+
var key = _.hasOwn(scope, '$key') && scope.$key
434434
var primitive = !isObject(value)
435435
if (trackByKey || key || primitive) {
436436
var id = trackByKey

src/instance/state.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ exports._initData = function () {
5454
this._data = optionsData
5555
for (var prop in propsData) {
5656
if (process.env.NODE_ENV !== 'production' &&
57-
optionsData.hasOwnProperty(prop)) {
57+
_.hasOwn(optionsData, prop)) {
5858
_.warn(
5959
'Data field "' + prop + '" is already defined ' +
6060
'as a prop. Use prop default value instead.'
6161
)
6262
}
6363
if (this._props[prop].raw !== null ||
64-
!optionsData.hasOwnProperty(prop)) {
64+
!_.hasOwn(optionsData, prop)) {
6565
_.set(optionsData, prop, propsData[prop])
6666
}
6767
}
@@ -105,7 +105,7 @@ exports._setData = function (newData) {
105105
i = keys.length
106106
while (i--) {
107107
key = keys[i]
108-
if (!this.hasOwnProperty(key)) {
108+
if (!_.hasOwn(this, key)) {
109109
// new property
110110
this._proxy(key)
111111
}

src/observer/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Observer.create = function (value, vm) {
4848
}
4949
var ob
5050
if (
51-
Object.prototype.hasOwnProperty.call(value, '__ob__') &&
51+
_.hasOwn(value, '__ob__') &&
5252
value.__ob__ instanceof Observer
5353
) {
5454
ob = value.__ob__

src/util/lang.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111

1212
exports.set = function set (obj, key, val) {
13-
if (obj.hasOwnProperty(key)) {
13+
if (exports.hasOwn(obj, key)) {
1414
obj[key] = val
1515
return
1616
}
@@ -43,7 +43,7 @@ exports.set = function set (obj, key, val) {
4343
*/
4444

4545
exports.delete = function (obj, key) {
46-
if (!obj.hasOwnProperty(key)) {
46+
if (!exports.hasOwn(obj, key)) {
4747
return
4848
}
4949
delete obj[key]
@@ -62,6 +62,18 @@ exports.delete = function (obj, key) {
6262
}
6363
}
6464

65+
var hasOwn = Object.prototype.hasOwnProperty
66+
/**
67+
* Check whether the object has the property.
68+
*
69+
* @param {Object} obj
70+
* @param {String} key
71+
* @return {Boolean}
72+
*/
73+
exports.hasOwn = function (obj, key) {
74+
return hasOwn.call(obj, key)
75+
}
76+
6577
/**
6678
* Check if an expression is a literal value.
6779
*

src/util/options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function mergeData (to, from) {
2525
for (key in from) {
2626
toVal = to[key]
2727
fromVal = from[key]
28-
if (!to.hasOwnProperty(key)) {
28+
if (!_.hasOwn(to, key)) {
2929
_.set(to, key, fromVal)
3030
} else if (_.isObject(toVal) && _.isObject(fromVal)) {
3131
mergeData(toVal, fromVal)
@@ -326,7 +326,7 @@ exports.mergeOptions = function merge (parent, child, vm) {
326326
mergeField(key)
327327
}
328328
for (key in child) {
329-
if (!(parent.hasOwnProperty(key))) {
329+
if (!(_.hasOwn(parent, key))) {
330330
mergeField(key)
331331
}
332332
}

test/unit/specs/api/data_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ describe('Data API', function () {
5757
it('$delete', function () {
5858
vm._digest = jasmine.createSpy()
5959
vm.$delete('a')
60-
expect(vm.hasOwnProperty('a')).toBe(false)
61-
expect(vm._data.hasOwnProperty('a')).toBe(false)
60+
expect(_.hasOwn(vm, 'a')).toBe(false)
61+
expect(_.hasOwn(vm._data, 'a')).toBe(false)
6262
expect(vm._digest).toHaveBeenCalled()
6363
// reserved key should not be deleted
6464
vm.$delete('_data')

test/unit/specs/instance/state_spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var Vue = require('../../../../src/vue')
2+
var _ = require('../../../../src/util')
23

34
describe('Instance state initialization', function () {
45

@@ -38,7 +39,7 @@ describe('Instance state initialization', function () {
3839
el: document.createElement('div'),
3940
props: ['c']
4041
})
41-
expect(vm.hasOwnProperty('c')).toBe(true)
42+
expect(_.hasOwn(vm, 'c')).toBe(true)
4243
})
4344

4445
it('should use default prop value if prop not provided', function () {
@@ -98,7 +99,7 @@ describe('Instance state initialization', function () {
9899
// proxy new key
99100
expect(vm.b).toBe(2)
100101
// unproxy old key that's no longer present
101-
expect(vm.hasOwnProperty('a')).toBe(false)
102+
expect(_.hasOwn(vm, 'a')).toBe(false)
102103
})
103104
})
104105

0 commit comments

Comments
 (0)