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 ( )
0 commit comments