41. 中级:DOM-节点属性 2011-8-18 <ulid="parentDOM"> <liid="id1">I am No.1</li> <liid="id2">I am No.2</li> </ul> vardom=document.getElementById('parentDOM'); var children =dom.childNodes; for(vari=0,len =children.length;i<len;i++){ var type ="type="+ children[i].nodeType+";"; var name ="name="+ children[i].nodeName+";"; var id ="id="+ children[i].id +";"; vartagName="tagName="+ children[i].tagName+";"; log(type+name+id+tagName); }
48. 中级:Events-事件代理(委托) 2011-8-18 <ulid="parentDOM"> <liid="id1">I am No.1</li> <liid="id2">I am No.2</li> <liid="id3">I am No.3</li> <liid="id4">I am No.4</li> <liid="id5">I am No.5</li> </ul> 不用事件代理 var doc = document; for(vari=1;i<=5;i++){ vardom=doc.getElementById('id'+i); dom.onclick=function(){ alert(this.innerHTML); } } 使用事件代理 vardom=document.getElementById('parentDOM'); dom.onclick=function(){ var e = arguments[0]||window.event; var target =e.target||e.srcElement; alert(target.innerHTML); }
65. 高级:作用域 2011-8-18 vari=10; function fn(){ vari=20; var a =3; alert(i);//20 } fn(); alert(i); alert(a); function fn(){ vari=20; var a =3; alert(i); } //20 //10 //error undefined fn的执行环境是window,fn的定义环境是function中。 根据词法作用域,fn的作用域应该是在定义它的function中,而不是在其执行的环境相同的vari=10;(window)中
66. 高级:作用域 2011-8-18 vara = 1; function fn1(){ var a =9; function fn2(){ alert(a); } } fn1(); 2011-8-18 function fn1(){ var a =9; function fn2(){ alert(a); } } function fn2(){ alert(a); } fn2();
67. 高级:作用域链 vara = 1; function fn1(){ var a =9; function fn2(){ alert(a); } } fn1(); Fn2 Activation Obj Fn1 Activation Obj GlobalObj(window) this window arguments [] fn2 function this window arguments [] fn1 function a 9 this window arguments [] fn1 function a 1 作用域链 0 1 2 2011-8-18 function fn1(){ var a =9; function fn2(){ alert(a); } } function fn2(){ alert(a); } fn2(); fn2 execution context Scope chain
73. 高级:this or that 2011-8-18 varmyObject={}; myObject.method=function(){ this.WhoAmI="I'm A"; function test(){ this.WhoAmI=“I’m B"; } test(); returnthis.WhoAmI; }; myObject.method(); window.WhoAmI; myObject.method();//I'm A window.WhoAmI;//I'm B
74. 高级:this or that 2011-8-18 varmyObject={}; myObject.method=function(){ this.WhoAmI="I'm A"; var that =this; function test(){ that.WhoAmI="I'm B"; } test(); returnthis.WhoAmI; }; myObject.method()// I'm B ←这里赋值给that变量
78. 2011-8-18 高级:闭包 function fn1(){ var a =9; function fn2(){ alert(a);//error function fn1(){ var a =9; } 怎么访问函数内部的变量呢?改成全局变量?no!全局变量是魔鬼! 闭包 function fn1(){ var a =9; function fn2(){ alert(a); } return fn2; } var fn = fn1(); fn();
83. 2011-8-18 高级:OOP javascript中没有真正意义上的类 类Animal: var Animal =function(name,legs){ this.name = name; this.legs= legs; }; var cat =new Animal('hello kitty',4); 运用javascript的new关键字创建一个Animal的实例
84. 高级:OOP prototype关键字 2011-8-18 var Fn =function(){ this.xxx =[]; }; Fn.prototype.yyy=[]; var a =new Fn(); var b =new Fn(); a.xxx.push(1); a.yyy.push(1); alert([a.xxx.length,b.xxx.length]); alert([a.yyy.length,b.yyy.length]); //1,0 //1,1
85. this or prototype 2011-8-18 var Animal =function(name,legs){ this.name = name; this.legs= legs; this.sayName=function(){ alert('my name is '+this.name); }; }; 这段代码你有什么更好的写法?
86. 优化 2011-8-18 var Animal =function(name,legs){ this.name = name; this.legs= legs; }; Animal.prototype={ sayName:function(){ alert('my name is '+this.name); } } OR var Animal =function(name,legs){ this.name = name; this.legs= legs; }; Animal.prototype.sayName=function(){ alert('my name is '+this.name); };
87. 高级:原型链 对象通过一个内部属性绑定到它的原型,在firefox,chrome,safari中,这个属性__proto__对开发中可见,而在其他的浏览器却不允许脚本访问此属性。 var Animal =function(name,legs){ this.name = name; this.legs= legs; }; Animal.prototype.sayName=function(){ alert('my name is '+this.name); }; var cat =new Animal('hello kitty',4); var bird =new Animal('polly',2); cat bird Animal.prototype Animal Object __proto__ __proto__ __proto__ prototype __proto__ null name hello kitty name polly constructor toString (function) legs 4 legs 2 sayName (function) valueOf (function) …… …… 2011-8-18
88. 高级:OOP 2011-8-18 父类Animal var Animal =function(){ this.fly =function(){ if(this.wing){ alert(this.name +' can fly!'); } } }; Cat、Bird子类: var Cat =function(name,legs){ this.name = name; this.legs= legs; this.wing=false; }; var Bird =function(name,legs){ this.name = name; this.legs= legs; this.wing=true; };
89. 高级:OOP 2011-8-18 Cat和Bird都是Animal的子类,即:Cat和Bird要继承自Animal 方法一:apply var Cat =function(name,legs){ Animal.apply(this); this.name = name; this.legs= legs; this.wing=false; }; var Bird =function(name,legs){ Animal.apply(this); this.name = name; this.legs= legs; this.wing=true; }; var cat =new Cat('hello kitty',4); cat.name;//hello kitty var bird =new Bird('polly',2); bird.fly();//polly can fly!
90. 高级:OOP 2011-8-18 方法二:prototype + new Cat.prototype=new Animal(); Cat.prototype.constructor= Cat; Bird.prototype=new Animal(); Bird.prototype.constructor= Bird; var cat =new Cat('hello kitty',4); cat.name;//hello kitty var bird =new Bird('polly',2); bird.fly();//polly can fly!
91. 高级:OOP 2011-8-18 方法三:直接使用prototype //父类使用prototype写法 var Animal =function(){}; Animal.prototype.fly=function(){ if(this.wing){ alert(this.name +' can fly!'); } } //封装的继承函数extend function extend(Child, Parent){ var F =function(){}; F.prototype=Parent.prototype; Child.prototype=new F(); Child.prototype.constructor= Child; } extend(Cat,Animal); extend(Bird,Animal); var cat =new Cat('hello kitty',4); cat.name;//hello kitty var bird =new Bird('polly',2); bird.fly();//polly can fly!
92. 高级:OOP 方法四:object对象拷贝 functionextendCopy(Child, Parent){ var p =Parent.prototype; var c =Child.prototype; for(variin p){ c[i]= p[i]; } } extendCopy(Cat,Animal); extendCopy(Bird,Animal); var cat =new Cat('hello kitty',4); cat.name;//hello kitty var bird =new Bird('polly',2); bird.fly();//polly can fly! 2011-8-18
125. 高级:OOP资料 Douglas Crockford - Prototypal Inheritance in JavaScript Douglas Crockford - Classical Inheritance in JavaScript John Resig - Simple JavaScript Inheritance Dean Edwards - A Base Class for JavaScript Inheritance Javascript继承写法:http://www.js8.in/698.html 2011-8-18