心大了,这个世界的烦恼就小了;心小了,这个世界的烦恼就大了;心无所住!

Javascript面向对象编程

17 12月
作者:妙清自牧|分类:未命名|标签:人生 生活

Javascript是一个类C的语言,他的面向对象的东西相对于C++/Java比较奇怪,但是其的确相当的强大。这篇文章主要想从一个整体的角度来说明一下Javascript的面向对象的编程。

初探
我们知道Javascript中的变量定义基本如下:

JavaScript Code复制内容到剪贴板
  1. var name = 'Chen Hao';   
  2. var email = 'haoel(@)hotmail.com';   
  3. var website = 'http://coolshell.cn';  

如果要用对象来写的话,就是下面这个样子:

JavaScript Code复制内容到剪贴板
  1. var chenhao = {   
  2.     name :'Chen Hao',   
  3.     email : 'haoel(@)hotmail.com',   
  4.     website : 'http://coolshell.cn'   
  5. };  

于是,我就可以这样访问:

JavaScript Code复制内容到剪贴板
  1. //以成员的方式   
  2. chenhao.name;   
  3. chenhao.email;   
  4. chenhao.website;  
  5.   
  6. //以hash map的方式   
  7. chenhao["name"];   
  8. chenhao["email"];   
  9. chenhao["website"];  

关于函数,我们知道Javascript的函数是这样的:

JavaScript Code复制内容到剪贴板
  1. var doSomething = function(){   
  2.    alert('Hello World.');   
  3. };  

于是,我们可以这么干:

JavaScript Code复制内容到剪贴板
  1. var sayHello = function(){   
  2.    var hello = "Hello, I'm "this.name   
  3.                 + ", my email is: " + this.email   
  4.                 + ", my website is: " + this.website;   
  5.    alert(hello);   
  6. };  
  7.    
  8. //直接赋值,这里很像C/C++的函数指针   
  9. chenhao.Hello = sayHello;  
  10. chenhao.Hello();  

相信这些东西都比较简单,大家都明白了。 可以看到javascript对象函数是直接声明,直接赋值,直接就用了。runtime的动态语言。

还有一种比较规范的写法是:

JavaScript Code复制内容到剪贴板
  1. //我们可以看到, 其用function来做class。  
  2.    
  3. var Person = function(name, email, website){   
  4.     this.name = name;   
  5.     this.email = email;   
  6.     this.website = website;    
  7.     this.sayHello = function(){   
  8.         var hello = "Hello, I'm "this.name  + ", \n" + "my email is: " + this.email + ", \n" + "my website is: " + this.website;   
  9.         alert(hello);   
  10.     };     
  11. };     
  12. var chenhao = new Person("Chen Hao""haoel@hotmail.com""http://coolshell.cn");     
  13. chenhao.sayHello();  

顺便说一下,要删除对象的属性,很简单:

JavaScript Code复制内容到剪贴板
  1. delete chenhao['email']  

上面的这些例子,我们可以看到这样几点:
1. Javascript的数据和成员封装很简单。没有类完全是对象操作。纯动态!
2. Javascript function中的this指针很关键,如果没有的话,那就是局部变量或局部函数。
3. Javascript对象成员函数可以在使用时临时声明,并把一个全局函数直接赋过去就好了。
4. Javascript的成员函数可以在实例上进行修改,也就是说不同实例相同函数名的行为不一定一样。

属性配置 – Object.defineProperty

先看下面的代码:

JavaScript Code复制内容到剪贴板
  1. //创建对象     
  2. var chenhao = Object.create(null);     
  3.   
  4. //设置一个属性     
  5. Object.defineProperty( chenhao,     
  6.                 'name', { value:  'Chen Hao',     
  7.                           writable:     true,     
  8.                           configurable: true,     
  9.                           enumerable:   true });   
  10.   
  11. //设置多个属性     
  12. Object.defineProperties( chenhao,     
  13.     {     
  14.         'email'  : { value:  'haoel@hotmail.com',   
  15.                      writable:     true,     
  16.                      configurable: true,     
  17.                      enumerable:   true },     
  18.         'website': { value: 'http://coolshell.cn',     
  19.                      writable:     true,     
  20.                      configurable: true,     
  21.                      enumerable:   true }     
  22.     }     
  23. );  

下面就说说这些属性配置是什么意思。
writable:这个属性的值是否可以改。
configurable:这个属性的配置是否可以改。
enumerable:这个属性是否能在for…in循环中遍历出来或在Object.keys中列举出来。
value:属性值。
get()/set(_value):get和set访问器。

Get/Set 访问器

关于get/set访问器,它的意思就是用get/set来取代value(其不能和value一起使用),示例如下:

JavaScript Code复制内容到剪贴板
  1. var  age = 0;     
  2. Object.defineProperty( chenhao,     
  3.             'age', {     
  4.                       get: function() {return age+1;},     
  5.                       set: function(value) {age = value;}     
  6.                       enumerable : true,     
  7.                       configurable : true     
  8.                     }     
  9. );     
  10. chenhao.age = 100; //调用set     
  11. alert(chenhao.age); //调用get 输出101(get中+1了);  

我们再看一个更为实用的例子——利用已有的属性(age)通过get和set构造新的属性(birth_year):

JavaScript Code复制内容到剪贴板
  1. Object.defineProperty( chenhao,     
  2.             'birth_year',     
  3.             {     
  4.                 get: function() {     
  5.                     var d = new Date();     
  6.                     var y = d.getFullYear();     
  7.                     return ( y - this.age );     
  8.                 },     
  9.                 set: function(year) {     
  10.                     var d = new Date();     
  11.                     var y = d.getFullYear();     
  12.                     this.age = y - year;     
  13.                 }     
  14.             }     
  15. );    
  16. alert(chenhao.birth_year);     
  17. chenhao.birth_year = 2000;     
  18. alert(chenhao.age);  

这样做好像有点麻烦,你说,我为什么不写成下面这个样子:

JavaScript Code复制内容到剪贴板
  1. var chenhao = {     
  2.     name: "Chen Hao",     
  3.     email: "haoel@hotmail.com",     
  4.     website: "http://coolshell.cn",     
  5.     age: 100,     
  6.     get birth_year() {     
  7.         var d = new Date();     
  8.         var y = d.getFullYear();     
  9.         return ( y - this.age );     
  10.     },     
  11.     set birth_year(year) {     
  12.         var d = new Date();     
  13.         var y = d.getFullYear();     
  14.         this.age = y - year;     
  15.     }     
  16. };     
  17. alert(chenhao.birth_year);  
  18. chenhao.birth_year = 2000;     
  19. alert(chenhao.age);  

是的,你的确可以这样的,不过通过defineProperty()你可以干这些事:
1)设置如 writable,configurable,enumerable 等这类的属性配置。
2)动态地为一个对象加属性。比如:一些HTML的DOM对像。
查看对象属性配置

如果查看并管理对象的这些配置,下面有个程序可以输出对象的属性和配置等东西:

JavaScript Code复制内容到剪贴板
  1. //列出对象的属性.     
  2. function listProperties(obj)     
  3. {     
  4.     var newLine = "<br />";     
  5.     var names = Object.getOwnPropertyNames(obj);     
  6.     for (var i = 0; i < names.length; i++) {     
  7.         var prop = names[i];     
  8.         document.write(prop + newLine);  
  9.   
  10.         // 列出对象的属性配置(descriptor)动用getOwnPropertyDescriptor函数。     
  11.         var descriptor = Object.getOwnPropertyDescriptor(obj, prop);     
  12.         for (var attr in descriptor) {     
  13.             document.write("..." + attr + ': ' + descriptor[attr]);     
  14.             document.write(newLine);     
  15.         }     
  16.         document.write(newLine);     
  17.     }     
  18. }     
  19. listProperties(chenhao);     
  20. call,apply, bind 和 this  

关于Javascript的this指针,和C++/Java很类似。 我们来看个示例:(这个示例很简单了,我就不多说了)

JavaScript Code复制内容到剪贴板
  1. function print(text){     
  2.     document.write(this.value + ' - ' + text+ '<br>');     
  3. }    
  4. var a = {value: 10, print : print};     
  5. var b = {value: 20, print : print};    
  6. print('hello');// this => global, output "undefined - hello"    
  7. a.print('a');// this => a, output "10 - a"     
  8. b.print('b'); // this => b, output "20 - b"       
  9. a['print']('a'); // this => a, output "10 - a"  

我们再来看看call 和 apply,这两个函数的差别就是参数的样子不一样,另一个就是性能不一样,apply的性能要差很多。(关于性能,可到 JSPerf 上去跑跑看看)

JavaScript Code复制内容到剪贴板
  1. print.call(a, 'a'); // this => a, output "10 - a"     
  2. print.call(b, 'b'); // this => b, output "20 - b"     
  3.   
  4. print.apply(a, ['a']); // this => a, output "10 - a"     
  5. print.apply(b, ['b']); // this => b, output "20 - b"  


但是在bind后,this指针,可能会有不一样,但是因为Javascript是动态的。如下面的示例

JavaScript Code复制内容到剪贴板
  1. var p = print.bind(a);     
  2. p('a');             // this => a, output "10 - a"     
  3. p.call(b, 'b');     // this => a, output "10 - b"     
  4. p.apply(b, ['b']);  // this => a, output "10 - b"  

继承 和 重载

通过上面的那些示例,我们可以通过Object.create()来实际继承,请看下面的代码,Student继承于Object。

JavaScript Code复制内容到剪贴板
  1. var Person = Object.create(null);     
  2.   
  3. Object.defineProperties     
  4. (     
  5.     Person,     
  6.     {     
  7.         'name'  : {  value: 'Chen Hao'},     
  8.         'email'  : { value : 'haoel@hotmail.com'},     
  9.         'website': { value: 'http://coolshell.cn'}     
  10.     }     
  11. );     
  12.   
  13. Person.sayHello = function () {     
  14.     var hello = "<p>Hello, I am "this.name  + ", <br>" +     
  15.                 "my email is: " + this.email + ", <br>" +     
  16.                 "my website is: " + this.website;     
  17.     document.write(hello + "<br>");     
  18. }  
  19.      
  20. var Student = Object.create(Person);     
  21. Student.no = "1234567"//学号     
  22. Student.dept = "Computer Science"//系     
  23.   
  24. //使用Person的属性     
  25. document.write(Student.name + ' ' + Student.email + ' ' + Student.website +'<br>');       
  26. //使用Person的方法     
  27. Student.sayHello();       
  28. //重载SayHello方法     
  29. Student.sayHello = function (person) {     
  30.     var hello = "<p>Hello, I am "this.name  + ", <br>" +     
  31.                 "my email is: " + this.email + ", <br>" +     
  32.                 "my website is: " + this.website + ", <br>" +     
  33.                 "my student no is: " + this. no + ", <br>" +     
  34.                 "my departent is: " + this. dept;     
  35.     document.write(hello + '<br>');     
  36. }     
  37. //再次调用     
  38. Student.sayHello();     
  39.   
  40. //查看Student的属性(只有 no 、 dept 和 重载了的sayHello)     
  41. document.write('<p>' + Object.keys(Student) + '<br>');  

通用上面这个示例,我们可以看到,Person里的属性并没有被真正复制到了Student中来,但是我们可以去存取。这是因为Javascript用委托实现了这一机制。其实,这就是Prototype,Person是Student的Prototype。

当我们的代码需要一个属性的时候,Javascript的引擎会先看当前的这个对象中是否有这个属性,如果没有的话,就会查找他的Prototype对象是否有这个属性,一直继续下去,直到找到或是直到没有Prototype对象。

为了证明这个事,我们可以使用Object.getPrototypeOf()来检验一下:

JavaScript Code复制内容到剪贴板
  1. Student.name = 'aaa';  
  2.   
  3. //输出 aaa     
  4. document.write('<p>' + Student.name + '</p>');   
  5.   
  6. //输出 Chen Hao     
  7. document.write('<p>' +Object.getPrototypeOf(Student).name + '</p>');  


于是,你还可以在子对象的函数里调用父对象的函数,就好像C++里的 Base::func() 一样。于是,我们重载hello的方法就可以使用父类的代码了,如下所示:

JavaScript Code复制内容到剪贴板
  1. //新版的重载SayHello方法     
  2. Student.sayHello = function (person) {     
  3.     Object.getPrototypeOf(this).sayHello.call(this);     
  4.     var hello = "my student no is: " + this. no + ", <br>" +     
  5.                 "my departent is: " + this. dept;     
  6.     document.write(hello + '<br>');     
  7. }  

这个很强大吧。

组合

上面的那个东西还不能满足我们的要求,我们可能希望这些对象能真正的组合起来。为什么要组合?因为我们都知道是这是OO设计的最重要的东西。不过,这对于Javascript来并没有支持得特别好,不好我们依然可以搞定个事。

首先,我们需要定义一个Composition的函数:(target是作用于是对象,source是源对象),下面这个代码还是很简单的,就是把source里的属性一个一个拿出来然后定义到target中。

JavaScript Code复制内容到剪贴板
  1. function Composition(target, source)     
  2. {     
  3.     var desc  = Object.getOwnPropertyDescriptor;     
  4.     var prop  = Object.getOwnPropertyNames;     
  5.     var def_prop = Object.defineProperty;     
  6.   
  7.     prop(source).forEach(     
  8.         function(key) {     
  9.             def_prop(target, key, desc(source, key))     
  10.         }     
  11.     )     
  12.     return target;     
  13. }  


有了这个函数以后,我们就可以这来玩了:

JavaScript Code复制内容到剪贴板
  1. //艺术家     
  2. var Artist = Object.create(null);     
  3. Artist.sing = function() {     
  4.     return this.name + ' starts singing...';     
  5. }     
  6. Artist.paint = function() {     
  7.     return this.name + ' starts painting...';     
  8. }     
  9.   
  10. //运动员     
  11. var Sporter = Object.create(null);     
  12. Sporter.run = function() {     
  13.     return this.name + ' starts running...';     
  14. }     
  15. Sporter.swim = function() {     
  16.     return this.name + ' starts swimming...';     
  17. }     
  18.   
  19. Composition(Person, Artist);     
  20. document.write(Person.sing() + '<br>');     
  21. document.write(Person.paint() + '<br>');     
  22.   Composition(Person, Sporter);     
  23. document.write(Person.run() + '<br>');     
  24. document.write(Person.swim() + '<br>');     
  25.   
  26. //看看 Person中有什么?(输出:sayHello,sing,paint,swim,run)     
  27. document.write('<p>' + Object.keys(Person) + '<br>');  

Prototype 和 继承
我们先来说说Prototype。我们先看下面的例程,这个例程不需要解释吧,很像C语言里的函数指针,在C语言里这样的东西见得多了。

JavaScript Code复制内容到剪贴板
  1. var plus = function(x,y){     
  2.     document.write( x + ' + ' + y + ' = ' + (x+y) + '<br>');     
  3.     return x + y;     
  4. };     
  5.   
  6. var minus = function(x,y){     
  7.     document.write(x + ' - ' + y + ' = ' + (x-y) + '<br>');     
  8.     return x - y;     
  9. };     
  10.   
  11. var operations = {     
  12.     '+': plus,     
  13.     '-': minus     
  14. };     
  15.   
  16. var calculate = function(x, y, operation){     
  17.     return operations[operation](x, y);     
  18. };     
  19.   
  20. calculate(12, 4, '+');     
  21. calculate(24, 3, '-');  


那么,我们能不能把这些东西封装起来呢,我们需要使用prototype。看下面的示例:

JavaScript Code复制内容到剪贴板
  1. var Cal = function(x, y){     
  2.     this.x = x;     
  3.     this.y = y;     
  4. }     
  5.   
  6. Cal.prototype.operations = {     
  7.     '+'function(x, y) { return x+y;},     
  8.     '-'function(x, y) { return x-y;}     
  9. };     
  10.   
  11. Cal.prototype.calculate = function(operation){     
  12.     return this.operations[operation](this.x, this.y);     
  13. };     
  14.   
  15. var c = new Cal(4, 5);     
  16. Cal.calculate('+');     
  17. Cal.calculate('-');  


这就是prototype的用法,prototype 是javascript这个语言中最重要的内容。网上有太多的文章介始这个东西了。说白了,prototype就是对一对象进行扩展,其特点在于通过“复制”一个已经存在的实例来返回新的实例,而不是新建实例。被复制的实例就是我们所称的“原型”,这个原型是可定制的(当然,这里没有真正的复制,实际只是委托)。上面的这个例子中,我们扩展了实例Cal,让其有了一个operations的属性和一个calculate的方法。

这样,我们可以通过这一特性来实现继承。还记得我们最最前面的那个Person吧, 下面的示例是创建一个Student来继承Person。

JavaScript Code复制内容到剪贴板
  1. function Person(name, email, website){     
  2.     this.name = name;     
  3.     this.email = email;     
  4.     this.website = website;     
  5. };     
  6.   
  7. Person.prototype.sayHello = function(){     
  8.     var hello = "Hello, I am "this.name  + ", <br>" +     
  9.                 "my email is: " + this.email + ", <br>" +     
  10.                 "my website is: " + this.website;     
  11.     return hello;     
  12. };     
  13.   
  14. function Student(name, email, website, no, dept){     
  15.     var proto = Object.getPrototypeOf;     
  16.     proto(Student.prototype).constructor.call(this, name, email, website);     
  17.     this.no = no;     
  18.     this.dept = dept;     
  19. }     
  20.   
  21. // 继承prototype     
  22. Student.prototype = Object.create(Person.prototype);     
  23.   
  24. //重置构造函数     
  25. Student.prototype.constructor = Student;     
  26.   
  27. //重载sayHello()     
  28. Student.prototype.sayHello = function(){     
  29.     var proto = Object.getPrototypeOf;     
  30.     var hello = proto(Student.prototype).sayHello.call(this) + '<br>';     
  31.     hello += "my student no is: " + this. no + ", <br>" +     
  32.              "my departent is: " + this. dept;     
  33.     return hello;     
  34. };  
  35.      
  36. var me = new Student(     
  37.     "Chen Hao",     
  38.     "haoel@hotmail.com",     
  39.     "http://coolshell.cn",     
  40.     "12345678",     
  41.     "Computer Science"     
  42. );     
  43. document.write(me.sayHello());  

兼容性
上面的这些代码并不一定能在所有的浏览器下都能运行,因为上面这些代码遵循 ECMAScript 5 的规范,关于ECMAScript 5 的浏览器兼容列表,你可以看这里“ES5浏览器兼容表”。

本文中的所有代码都在Chrome最新版中测试过了。

下面是一些函数,可以用在不兼容ES5的浏览器中:

Object.create()函数

JavaScript Code复制内容到剪贴板
  1. function clone(proto) {     
  2.     function Dummy() { }     
  3.   
  4.     Dummy.prototype             = proto;     
  5.     Dummy.prototype.constructor = Dummy;     
  6.   
  7.     return new Dummy(); //等价于Object.create(Person);     
  8. }     
  9.   
  10. var me = clone(Person);  


defineProperty()函数

JavaScript Code复制内容到剪贴板
  1. function defineProperty(target, key, descriptor) {     
  2.     if (descriptor.value){     
  3.         target[key] = descriptor.value;     
  4.     }else {     
  5.         descriptor.get && target.__defineGetter__(key, descriptor.get);     
  6.         descriptor.set && target.__defineSetter__(key, descriptor.set);     
  7.     }       
  8.     return target     
  9. }  


keys()函数

JavaScript Code复制内容到剪贴板
  1. function keys(object) { var result, key     
  2.     result = [];     
  3.     for (key in object){     
  4.         if (object.hasOwnProperty(key))  result.push(key)     
  5.     }   
  6.     return result;     
  7. }  


Object.getPrototypeOf() 函数

JavaScript Code复制内容到剪贴板
  1. function proto(object) {     
  2.     return !object?                null     
  3.          : '__proto__' in object?  object.__proto__     
  4.          : /* not exposed? */      object.constructor.prototype     
  5. }  


bind 函数

JavaScript Code复制内容到剪贴板
  1. var slice = [].slice     
  2.   
  3. function bind(fn, bound_this) { var bound_args     
  4.     bound_args = slice.call(arguments, 2)     
  5.     return function() { var args     
  6.         args = bound_args.concat(slice.call(arguments))     
  7.         return fn.apply(bound_this, args) }     
  8. }  
浏览316 评论0
返回
目录
返回
首页
新华社区卫生服务中心信息网络管理规定 利用Jquery动态加载ZBLOG栏目背景音乐

发表评论

欢迎你第一次访问网站! 您是本站第6476名访客 今日有0篇新文章
龙年
大吉

分享:

支付宝

微信