JavaScript检测之basevalidate.js

来源:互联网 发布:js对象循环引用 编辑:程序博客网 时间:2024/05/17 00:58

basevalidate.js 包含 14个独立检测方法 和 1个综合检测方法,示例代码如下:

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>basevalidate test</title>  <script type="text/javascript" src="basevalidate.js"></script>  <script>    var Person = function(){      this.name = 'stone';      this.age = 30;    }    var person = new Person();    var nums = [123, 456, 789];    // 14个独立检测方法    console.log(baseValidate.isString(null));    console.log(baseValidate.isNumber(null));    console.log(baseValidate.isBoolean(null));    console.log(baseValidate.isUndefined(null));    console.log(baseValidate.isNull(null));    console.log(baseValidate.isObject(null));    console.log(baseValidate.instanceOf(null));    console.log(baseValidate.isFunction(null));    console.log(baseValidate.isArray(null));    console.log(baseValidate.isProperty(null));    console.log(baseValidate.isOwnProperty(null));    console.log(baseValidate.isDomProperty(null));    console.log(baseValidate.isBomProperty(null));    console.log(baseValidate.isEmpty(null));    // 1个综合检测方法 baseValidate(value, object),等价于 baseValidate.validateAll(value ,object)    console.log(baseValidate('123'));    console.log(baseValidate(123));    console.log(baseValidate(true));    console.log(baseValidate(person, Person));    console.log(baseValidate(nums));    console.log(baseValidate('age', person));    console.log(baseValidate('name', person));    console.log(baseValidate(alert));    console.log(baseValidate(document.getElementById));    // 以下皆为 isEmpty() 方法为 false 的情况    console.log(baseValidate()); // 不传参数,参数默认为 undefined    console.log(baseValidate(null));    console.log(baseValidate(''));    console.log(baseValidate(0));    console.log(baseValidate(false));    console.log(baseValidate({}));    console.log(baseValidate([]));    console.log(baseValidate(NaN));  </script></head><body></body></html>
  • 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
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 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
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

测试结果如下:

不知道大家有没有发现,其中一个结果好像不太正确,console.log(baseValidate('name', person)) 为什么会输出 isBomProperty: true,这是因为window 对象中也有 name 属性,所以 name 也被认为是 BOM 的属性。

0 0
原创粉丝点击