-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisPrototype.js
More file actions
48 lines (45 loc) · 1.11 KB
/
isPrototype.js
File metadata and controls
48 lines (45 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import isConstructor from './isConstructor'
/**
* 判断是否为 prototype 对象
* ========================================================================
* @method isPrototype
* @since 0.2.0
* @category Function
* @param {*} val - 要检测的数据
* @returns {Boolean} 'val' 是 prototype 对象,返回 true,否则返回 false
* @example
*
* const Yao= {
* age: 40,
* career: 'programmer'
* }
*
* const Programmer = function(name, age) {
* this.name = name
* this.age = age
* this.isDead = false
*
* return this
* }
*
* Programmer.prototype.career = 'programmer'
* Programmer.prototype.getWorkDone = function() {
* this.isDead = true
* return this
* }
*
* isPrototype(null) // => false
*
* isPrototype(Object) // => false
* isPrototype(Object.prototype) // => true
*
* isPrototype(Programmer) // => false
* isPrototype(Yao.__proto__) // => true
*/
const isPrototype = (val) => {
const OP = Object.prototype
const Ctor = val ? val.constructor : null
const proto = (isConstructor(Ctor) && Ctor.prototype) || OP
return val === proto
}
export default isPrototype