如果你想让你的javascript代码变得更加优美,性能更加卓越。或者,你想像jQuery的作者一样,写出属于自己优秀的类库(哪怕是基于 jquery的插件)。那么,你请务必要学习javascript面向对象,否则你无法更灵活的使用javascript这门语言。
什么事闭包?到底什么是原型?(知道闭包和原型的,就算得上是javascript的高手了。但真正能够理解,并且灵活运用的人并不多)到底该如何学习javascript中的面向对象呢?在javascript这么语言正如日中天,相信不少人正在为此而困惑。
本文中,我讲用代码+详细注释的方式,一行行一步步讲述javascript中的面向对象编程。当然有些只是我个人的理解,不足之处,敬请谅解!
1.下面部分的代码,将是从目前十分流行的JSON数据格式以及javascript数组,来一步步像大家阐述javascript中的面向对象思想。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 2 3 4JSON数据格式 5 6 149 150 151 152 153
2.下面部分代码,是从另外一个角度讲解javascript中的面向对象编程。是借鉴EasyJF开源团队的讲解,我个人做了一些补充和说明。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 2 3 4javascript面向对象编程 5 6 192 193 194195 196
3. function,object
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 function aa() { 2 3 } 4 var bb = function () { 5 6 }; 7 var cc = new Function("a", "return a"); 8 var obj1 = new Object(); 9 var obj2 = new Array();10 var obj3 = {};11 var a = new aa();
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 function Person(name , age) { 2 this.name = name; 3 this.age = age; 4 this.show = function () { 5 window.alert(this.name); 6 } 7 this.say = say; 8 } 9 function say() {10 window.alert(this.name);11 }12 Person.prototype.output = function () {13 window.alert(this.name);14 };15 var person1 = new Person("wyp",33);16 var person2 = new Person("wyp", 33);17 person1.say();18 person1.show();19 person1.output();20 window.alert(person1 instanceof Person);21 window.alert(person1.show == person2.show);22 window.alert(person1.say == person2.say);23 window.alert(person1.output == person2.output);
http://blog.csdn.net/dinglang_2009/article/details/6911622