-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_extend.js
More file actions
43 lines (38 loc) · 1022 Bytes
/
js_extend.js
File metadata and controls
43 lines (38 loc) · 1022 Bytes
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
//js继承
function Animal(name){
this.name = name
this.sleep = function() {
console.log(`${this.name} is sleeping....`)
}
}
Animal.prototype.eat = function(food) {
console.log(`${this.name} eat ${food}`)
}
//1.原型链继承 :子构造函数.prototype = new 父构造函数()
function Cat() {}
Cat.prototype = new Animal('cat')
Cat.prototype.construtor = Cat
console.log(cat.name)
cat.sleep()
cat.eat()
//end
//2. 构造函数继承 使用call和apply借用其他构造函数的成员, 可以解决给父构造函数传递参数的问题,
function Dog(name,leg) {
Animal.call(this, name)
this.name = name || 'erha'
this.leg = leg
}
let dog = new Dog('haha')
console.log(dog.leg)
dog.sleep()
dog.eat()
//3. 组合继承 借用构造函数 + 原型式继承
// 创建子构造函数
function Pig(name, leg) {
Animal.call(this, name)
this.leg = leg
}
Pig.prototype = new Animal('pig')
Pig.prototype.construtor = Pig
let pig = new Pig('peiqi', 4)
console.log(pig.leg)