Skip to content

Commit 67c9f8c

Browse files
committed
first-commit
0 parents  commit 67c9f8c

File tree

3 files changed

+896
-0
lines changed

3 files changed

+896
-0
lines changed

one-chapter/one.js

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
var duck = {
2+
duckSinging:()=>{
3+
console.log('嘎嘎嘎')
4+
}
5+
}
6+
7+
var chicken = {
8+
duckSinging:()=>{
9+
console.log('嘎嘎嘎')
10+
}
11+
}
12+
13+
var choir = []
14+
15+
var joinChoir = (animal) => {
16+
if(animal && typeof animal.duckSinging === 'function'){
17+
choir.push(animal)
18+
console.log('恭喜你加入合唱团')
19+
console.log('合唱团已有的成员数量')
20+
}
21+
}
22+
23+
//多态
24+
var makeSound = (animal)=>{
25+
if(animal instanceof Duck){
26+
console.log('嘎嘎嘎')
27+
}else if(animal instanceof Chicken){
28+
console.log('咯咯咯')
29+
}
30+
}
31+
32+
var Duck = function(){}
33+
var Chicken = function(){}
34+
35+
makeSound(new Duck())
36+
makeSound(new Chicken())
37+
38+
//多态实现
39+
var makeSound1 = (animal)=>{
40+
animal.sound()
41+
}
42+
43+
var Duck1 = function(){}
44+
Duck1.prototype.sound = function(){
45+
console.log('嘎嘎嘎')
46+
};
47+
48+
var Chicken1 = function(){
49+
console.log('咯咯咯')
50+
}
51+
52+
var Dog = function(){}
53+
Dog.prototype.sound = function(){
54+
console.log('汪汪汪')
55+
};
56+
57+
makeSound1(new Duck1())
58+
makeSound1(new Chicken1())
59+
makeSound1(new Dog())
60+
61+
//类型检查和多态
62+
63+
//多态在面向对象程序设计中的作用
64+
var googleMap = {
65+
show:()=>{
66+
console.log('开始渲染谷歌地图')
67+
}
68+
};
69+
70+
var baidduMap = {
71+
show:() => {
72+
console.log('开始渲染百度地图')
73+
}
74+
}
75+
76+
var renderMap = (type) => {
77+
if(type === 'goole'){
78+
googleMap.show();
79+
}else if(type === 'baidu'){
80+
baidduMap.show();
81+
}
82+
}
83+
84+
renderMap('google')
85+
renderMap('baidu')
86+
87+
var renderMap = (map) =>{
88+
if(map.show instanceof Function){
89+
map.show();
90+
}
91+
}
92+
renderMap(googleMap)
93+
renderMap(baidduMap)
94+
renderMap(); //开始渲染地图
95+
96+
97+
//封装
98+
99+
//使用克隆的原型模式
100+
var Plane = function(){
101+
this.blood = 100;
102+
this.attackLevel = 1;
103+
this.defenseLevel = 1;
104+
}
105+
106+
var plane = new Plane()
107+
plane.blood = 500
108+
plane.attackLevel = 10
109+
plane.defenseLevel = 7
110+
111+
var clonePlane = Object.create(plane)
112+
console.log(plane)
113+
114+
//对象的继承 object.create 继承写法
115+
Object.create = new Object.create || function(obj){
116+
var F = function(){};
117+
F.prototype = obj;
118+
return new F();
119+
}
120+
121+
//原型编程范型基本规则
122+
//1. 所有数据都是对象
123+
//2. 要得到一个对象,不能通过实例化类,而是找到一个对象作为原型并克隆它
124+
//3. 对象会记住它的原型
125+
//4. 如果对象无法响应某个请求,他会把这个请求委托给自己的原型
126+
127+
//new 的实现方式 难点
128+
function Person(name){
129+
this.name = name
130+
}
131+
Person.prototype.getName = function(){
132+
return this.name;
133+
}
134+
135+
var objectFactory = function(){
136+
var obj = new Object()
137+
var Constructor = [].shift.call(arguments);
138+
obj.__proto__ = Constructor.prototype ;//指向正确的原型
139+
140+
//借助外部传人的构造器给 obj设置属性
141+
var ret = Constructor.apply(obj,arguments);
142+
143+
return typeof ret === 'object' ? ret : obj;
144+
}
145+
146+
var a = objectFactory( Person ,'smith');
147+
148+
console.log(a.name)
149+
console.log(a.getName())
150+
console.log(Object.getPrototypeOf(a) === Person.prototype)
151+
152+
// 等同于 var a = new A('sten')
153+
154+
//es6 class
155+
class Animal {
156+
constructor(name){
157+
this.name = name;
158+
}
159+
160+
getName(){
161+
return this.name
162+
}
163+
}
164+
165+
class Dog extends Animal {
166+
constructor(name){
167+
super(name);
168+
}
169+
speak(){
170+
return 'woof'
171+
}
172+
}
173+
174+
var dog = new Dog('scamp')
175+
console.log(dog.getName() + ' says ' + dog.speak());
176+
177+
178+
179+
180+
181+
182+
183+

0 commit comments

Comments
 (0)