forked from xiexiaojuan/201504
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.util.js
More file actions
42 lines (40 loc) · 1.76 KB
/
Copy path2.util.js
File metadata and controls
42 lines (40 loc) · 1.76 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
var util = require('util');
//声明一个构造函数
function Parent(){
this.name = 'father';//定义自己的私有属性name
this.age = 6;//定义自己的私有属性age
this.say = function(){//定义自己的私有属性say
console.log('hello '+this.name);
}
}
//在原型上定义一个方法
Parent.prototype.showName = function(){
console.log(this.name);
}
//定义子类
function Child(){
Parent.call(this);//以child作为this调用父类的构造函数,得到父类的私有属性
this.name = 'child';//定义自己的私有属性name 会覆盖父类的私有属性
}
//util.inherits(Child,Parent);//让子类继承父类
Child.prototype = new Parent();//这是原理
var p = new Parent();//创建父类对象
p.showName();//father 调用父类的showName方法,输出父类的名称 因为是用p来调用的,所以this就是p
p.say();//hello father 调用父类的say方法,输出父类的名称
var c = new Child();//创建子类对象
c.showName();//child 调用子类的showName方法,输出子类的名称 因为是用p来调用的,所以this就是p
c.say();//hello child 调用子类的say方法,输出子类的名称
function Person(){ //构造函数
this.name = 'zfpx'; //私有属性 name
this.toString = function(){//私有属性
return this.name;
}
}
var p = new Person();//创建Person对象
console.log(p.toString());//输出对象描述
// legacy: obj, showHidden, depth, colors*/
console.log(util.inspect(p,false));//返回一个对象的描述信息
console.log(util.isArray([]));//判断是否一个数组
console.log(util.isRegExp('dd'));//判断是否是一个正则
console.log(util.isDate(new Date()));//判断是否是一个日期
console.log(util.isError(new Error));//判断是否是一个错误