File tree Expand file tree Collapse file tree 2 files changed +150
-0
lines changed Expand file tree Collapse file tree 2 files changed +150
-0
lines changed Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+ < head >
4+ < meta charset ="utf-8 ">
5+ < title > </ title >
6+ </ head >
7+ < style >
8+ # ev {
9+ width : 300px ;
10+ height : 100px ;
11+ background : red;
12+ color : # fff ;
13+ text-align : center;
14+ line-height : 100px ;
15+ }
16+ </ style >
17+ < body >
18+ < div id ="ev ">
19+ 目标元素
20+ </ div >
21+ </ body >
22+ < script >
23+ var ev = document . getElementById ( 'ev' ) ;
24+ window . addEventListener ( 'click' , function ( ) {
25+ console . log ( 'window captrue' ) ;
26+ } , true ) ;
27+
28+ document . addEventListener ( 'click' , function ( ) {
29+ console . log ( 'document captrue' ) ;
30+ } , true ) ;
31+
32+ document . documentElement . addEventListener ( 'click' , function ( ) {
33+ console . log ( 'html captrue' ) ;
34+ } , true ) ;
35+
36+ document . body . addEventListener ( 'click' , function ( ) {
37+ console . log ( 'body captrue' ) ;
38+ } , true )
39+
40+ ev . addEventListener ( 'click' , function ( ) {
41+ console . log ( 'ev captrue' ) ;
42+ } )
43+
44+ var eve = new Event ( 'test' ) ;
45+ ev . addEventListener ( 'test' , function ( ) {
46+ console . log ( 'test dispatch' ) ;
47+ } )
48+ ev . dispatchEvent ( eve ) ;
49+
50+ setTimeout ( function ( ) {
51+ ev . dispatchEvent ( eve ) ;
52+ } , 1000 ) ;
53+ </ script >
54+ </ html >
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+ < head >
4+ < meta charset ="utf-8 ">
5+ < title > 面向对象</ title >
6+ </ head >
7+ < body >
8+ < script type ="text/javascript ">
9+ //类的声明
10+ function Animal ( ) {
11+ this . name = 'name' ;
12+ }
13+ //ES6中class的声明
14+ class Animal2 {
15+ constructor ( ) { //构造函数
16+ this . name = 'name' ;
17+ }
18+ }
19+ //通过类实例化生成对象
20+ console . log ( new Animal ( ) , new Animal2 ( ) ) ;
21+ //借助构造函数实现继承
22+ function Parent1 ( ) {
23+ this . name = 'parent1' ;
24+ }
25+ //Parent1
26+ function Child1 ( ) {
27+ Parent1 . call ( this ) ; //apply
28+ this . type = 'child1' ;
29+ }
30+
31+ console . log ( new Child1 ) ;
32+ //借助原型链继承方式
33+ function Parent2 ( ) {
34+ this . name = 'parent2' ;
35+ this . play = [ 1 , 2 , 3 ] ;
36+ }
37+ function Child2 ( ) {
38+ this . type = 'child2'
39+ }
40+
41+ Child2 . prototype = new Parent2 ( ) ; //理解为prototype属性指向Parent2
42+
43+ console . log ( new Child2 ( ) ) ;
44+
45+ var s1 = new Child2 ( ) ;
46+ var s2 = new Child2 ( ) ;
47+ console . log ( s1 . play , s2 . play ) ;
48+ s1 . play . push ( 4 ) ;
49+ //组合方式
50+ function Parent3 ( ) {
51+ this . name = 'Parent3' ;
52+ this . play = [ 1 , 2 , 3 ] ;
53+ }
54+ function Child3 ( ) {
55+ Parent3 . call ( this ) ;
56+ this . type = 'Child3' ;
57+ }
58+ //子类化原型链 执行了两次实例化
59+ Child3 . prototype = new Parent3 ( ) ;
60+ var s3 = new Child3 ( ) ;
61+ var s4 = new Child3 ( ) ;
62+ s3 . play . push ( 5 ) ;
63+ console . log ( s3 . play , s4 . play ) ;
64+ //面向对象通用继承
65+
66+ //组合继承的优化1
67+ function Parent4 ( ) {
68+ this . name = 'Parent4' ;
69+ this . play = [ 1 , 2 , 3 ] ;
70+ }
71+ function Child4 ( ) {
72+ Parent4 . call ( this ) ;
73+ this . type = 'Child4' ;
74+ }
75+ Child4 . prototype = Parent4 . prototype ;
76+ var s5 = new Child4 ( ) ;
77+ var s6 = new Child4 ( ) ;
78+ console . log ( s5 , s6 ) ;
79+
80+ console . log ( s5 instanceof Child4 , s5 instanceof Parent4 ) ; //true
81+ console . log ( s5 . __proto__ === Child4 . prototype , s5 . __proto__ === Parent4 . prototype ) ; //true
82+ console . log ( s5 . constructor ) ;
83+
84+ //组合继承的优化2
85+ function Parent5 ( ) {
86+ this . name = 'Parent5' ;
87+ this . play = [ 1 , 2 , 3 ] ;
88+ }
89+ function Child5 ( ) {
90+ Parent4 . call ( this ) ;
91+ this . type = 'Child5' ;
92+ }
93+ Child5 . prototype = Object . create ( Parent5 . prototype ) ;
94+ </ script >
95+ </ body >
96+ </ html >
You can’t perform that action at this time.
0 commit comments