-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extend.html
108 lines (85 loc) · 2.14 KB
/
Extend.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<html>
<head>
<script type ="text/javascript" src="http://code.jquery.com/jquery-1.7.js"></script>
<title></title>
</head>
<body>
<script type ="text/javascript">
//----------------------Simple extnding------------------
var animal= {
eat: function (){
console.log("I'm eating");
}
}
var dog = {
bark: function() {
console.log("I'm barking");
}
}
var cat = {
meow: function(){
console.log("meow")
}
}
dog.bark();
console.log(dog.eat);
$.extend(dog, animal);
dog.eat();
// after override function from source object
animal.eat= function () {
console.log("I'm still eating");
}
// the same function in the target object will not override and it will have old implementation
animal.eat(); // I'm still eating
dog.eat(); // I'm eating
//---------------------------------Extending from multiple objects----------------------
animal= {
eat: function (){
console.log("I'm eating");
}
}
dog = {
bark: function() {
console.log("I'm barking");
}
}
cat = {
meow: function(){
console.log("meow")
}
}
var catdog = {}
$.extend(catdog, dog, cat, animal);
catdog.eat(); //I'm eating
catdog.bark(); //I'm barking
catdog.meow(); // meow
///---------------------------------Deep Extending---------------
console.log("Deep Extending");
animal= {
actions:{
eat: function (){console.log("I'm eating");},
sit: function (){console.log("I'm sitting");}
}
}
dog = {
actions:{
bark: function (){console.log("I'm barking");},
dig: function (){console.log("I'm digging");}
}
}
dog.actions.bark(); //I'm barking
dog.actions.dig(); //I'm digging
$.extend(dog, animal);
dog.actions.eat(); //I'm eating
dog.actions.sit(); //I'm sitting
// In this case dog can't barking
console.log(dog.actions.bark); // undefined
$.extend(true, dog, animal);
dog.actions.eat(); //I'm eating
dog.actions.sit(); //I'm sitting
dog.actions.bark(); //I'm barking
dog.actions.dig(); //I'm digging
</script>
<h1>Extend demo</h1>
</body>
</html>