Skip to content

Commit b866ded

Browse files
committed
实现一个类
1 parent 1af26da commit b866ded

File tree

2 files changed

+131
-52
lines changed

2 files changed

+131
-52
lines changed

Class.js

Lines changed: 129 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,135 @@
1-
class LazyManClass{
2-
constructor(name){
3-
this.taskList = []
4-
this.name = name
5-
console.log(`Hi I am ${this.name}`)
6-
setTimeout(() =>{
7-
this.next()
8-
},1000)
9-
}
10-
eat(name){
11-
var that = this;
12-
var fn = (function (n){
13-
return function (){
14-
console.log(`I am eating ${n}`)
15-
that.next()
16-
}
17-
})(name)
18-
this.taskList.push(fn)
19-
return this
20-
}
21-
sleepFirst(time){
22-
var that = this
23-
var fn = (function (t){
24-
return function(){
25-
setTimeout(()=>{
26-
console.log(`等待了${t}秒`)
27-
that.next()
28-
},t * 1000)
29-
}
30-
})(time)
31-
this.taskList.push(fn)
32-
return this
33-
}
34-
sleep(time){
35-
var that = this
36-
var fn = (function (t){
37-
return function(){
38-
setTimeout(()=>{
39-
console.log(`等待了${t}秒`)
40-
that.next()
41-
},t*1000)
42-
}
43-
})(time)
44-
this.taskList.push(fn)
45-
return this
1+
// class LazyManClass{
2+
// constructor(name){
3+
// this.taskList = []
4+
// this.name = name
5+
// console.log(`Hi I am ${this.name}`)
6+
// setTimeout(() =>{
7+
// this.next()
8+
// },1000)
9+
// }
10+
// eat(name){
11+
// var that = this;
12+
// var fn = (function (n){
13+
// return function (){
14+
// console.log(`I am eating ${n}`)
15+
// that.next()
16+
// }
17+
// })(name)
18+
// this.taskList.push(fn)
19+
// return this
20+
// }
21+
// sleepFirst(time){
22+
// var that = this
23+
// var fn = (function (t){
24+
// return function(){
25+
// setTimeout(()=>{
26+
// console.log(`等待了${t}秒`)
27+
// that.next()
28+
// },t * 1000)
29+
// }
30+
// })(time)
31+
// this.taskList.push(fn)
32+
// return this
33+
// }
34+
// sleep(time){
35+
// var that = this
36+
// var fn = (function (t){
37+
// return function(){
38+
// setTimeout(()=>{
39+
// console.log(`等待了${t}秒`)
40+
// that.next()
41+
// },t*1000)
42+
// }
43+
// })(time)
44+
// this.taskList.push(fn)
45+
// return this
46+
// }
47+
// next(){
48+
// var fn = this.taskList.shift()
49+
// fn && fn()
50+
// }
51+
// }
52+
53+
// function LazyMan(name){
54+
// return new LazyManClass(name)
55+
// }
56+
57+
// LazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(5).sleep(4).eat('junk food');
58+
// LazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(5).sleep(10).eat('junk food');
59+
60+
61+
// JavaScript实现一个类,实例化
62+
63+
// 构造函数法 this + prototype
64+
65+
function Mobile(name,price){
66+
this.name = name
67+
this.price = price
68+
}
69+
70+
Mobile.prototype.sell = function(){
71+
console.log(this.name + ',售价¥'+ this.price)
72+
}
73+
74+
var iPhone = new Mobile('iPhone',4000)
75+
iPhone.sell()
76+
77+
// Object.create法
78+
79+
var Person = {
80+
firsrname: "Mark",
81+
lastname: "Yun",
82+
age: 25,
83+
introduce:function(){
84+
console.log('I am ' + Person.firsrname + ' ' + Person.lastname)
4685
}
47-
next(){
48-
var fn = this.taskList.shift()
49-
fn && fn()
86+
}
87+
88+
var person = Object.create(Person)
89+
person.introduce()
90+
91+
if(!Object.create){
92+
Object.create = function(o){
93+
function F(){}
94+
F.prototype = o
95+
return new F()
5096
}
5197
}
5298

53-
function LazyMan(name){
54-
return new LazyManClass(name)
99+
// 极简主义法
100+
101+
var Cat = {
102+
age: 3,
103+
createNew: function(){
104+
var cat = {}
105+
cat.name = "lalala"
106+
var sount = "maomaomaomao"
107+
cat.makeSount = function(){
108+
console.log(sount)
109+
}
110+
cat.changeAge = function(num){
111+
Cat.age = num;
112+
console.log(Cat.age)
113+
}
114+
return cat
115+
}
55116
}
56117

57-
LazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(5).sleep(4).eat('junk food');
58-
LazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(5).sleep(10).eat('junk food');
118+
var cat = Cat.createNew()
119+
console.log(cat.name)
120+
cat.makeSount()
121+
cat.changeAge(7)
122+
123+
// 语法糖
124+
class Point{
125+
constructor(x,y){
126+
this.x = x
127+
this.y = y
128+
}
129+
toString(){
130+
console.log('(' + this.x + ',' + this.y +')')
131+
}
132+
}
133+
134+
var point = new Point(2,3)
135+
point.toString()

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,13 @@ ______
9393
##### parseInt() 函数解析一个字符串参数,并返回一个指定基数的整数 (数学系统的基础)。
9494
###### const intValue = parseInt(string[, radix]);
9595
> string 要被解析的值。如果参数不是一个字符串,则将其转换为字符串(使用 ToString 抽象操作)。字符串开头的空白符将会被忽略。
96+
>
9697
> radix 一个介于2和36之间的整数(数学系统的基础),表示上述字符串的基数。默认为10。 返回值 返回一个整数或NaN
9798
9899

99100
##### sort()方法用于对数组的元素进行排序。默认排序顺序为按字母升序。
100101
> 升序: XXX.sort(function(a,b){return a-b});
102+
>
101103
> 降序: XXX.sort(function(a,b){return b-a});
102104
103105

0 commit comments

Comments
 (0)