7
7
* MIT Licensed
8
8
*/
9
9
10
- var type = require ( 'type-detect' ) ;
11
-
12
10
/**
13
11
* ### .hasProperty(object, name)
14
12
*
15
- * This allows checking whether an object has
16
- * named property or numeric array index .
13
+ * This allows checking whether an object has own
14
+ * or inherited from prototype chain named property .
17
15
*
18
16
* Basically does the same thing as the `in`
19
- * operator but works properly with natives
20
- * and null/undefined values .
17
+ * operator but works properly with null/undefined values
18
+ * and other primitives .
21
19
*
22
20
* var obj = {
23
21
* arr: ['a', 'b', 'c']
@@ -39,31 +37,20 @@ var type = require('type-detect');
39
37
* hasProperty(obj.arr, 3); // false
40
38
*
41
39
* @param {Object } object
42
- * @param {String|Number } name
40
+ * @param {String|Symbol } name
43
41
* @returns {Boolean } whether it exists
44
42
* @namespace Utils
45
43
* @name hasProperty
46
44
* @api public
47
45
*/
48
46
49
- var literals = {
50
- 'number' : Number ,
51
- 'string' : String ,
52
- } ;
53
47
function hasProperty ( obj , name ) {
54
- var objType = type ( obj ) ;
55
- // Bad Object, obviously no props at all
56
- if ( objType === 'null' || objType === 'undefined' ) {
48
+ if ( typeof obj === 'undefined' || obj === null ) {
57
49
return false ;
58
50
}
59
51
60
- // The `in` operator does not work with certain literals
61
- // box these before the check
62
- if ( literals [ objType ] && typeof obj !== 'object' ) {
63
- obj = new literals [ objType ] ( obj ) ;
64
- }
65
-
66
- return name in obj ;
52
+ // The `in` operator does not work with primitives.
53
+ return name in Object ( obj ) ;
67
54
}
68
55
69
56
/* !
0 commit comments