免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2885 | 回复: 3
打印 上一主题 下一主题

ruby与javascript面向对象编程的比较 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-06 17:39 |只看该作者 |倒序浏览

ruby与javascript面向对象编程的比












原文:http://howtonode.org/object-graphs-3

作者分析了ruby与javascript两者在面向对象模式的区别,这里只摘取重点部分,有兴趣的读者可看原文。

Ruby
先来看一个简单的字符串:

Ruby代码
  1. 1.animal = "cat"  
  2. animal = "cat"
复制代码

引用
Notice that every object has a class. Our string is of class String which inherits from the class Object. It's class String is of class Class which inherits Module and then Object.


再来看JavaScript

Javascript代码
  1. 1.var animal = "cat";  
  2. var animal = "cat";
复制代码



引用
Remember that you can simulate classes in JavaScript using constructor + prototype pairs. That's just what the built-in object types do. The prototypes are objects and thus inherit directly from Object.prototype and the constructors are functions and inherit from Function.prototype.


对象私有方法,注意这里说的是对象,而不是类。类的私有方法在类的声明中用private修饰。而对象私有方法是指一个对象的方法相对同类的其它对象是私有的。如下代码:

Ruby代码
  1. 1.animal = "cat" #animal对象已被创建   
  2. 2.def animal.speak #在animal对象已被创建后,可赋于其新的方法,该方法不能被同类的其它对象共享,所以是私有的   
  3. 3.  puts "The #{self} says miaow"  
  4. 4.end  
  5. 5.animal.speak   
  6. 6.puts animal.upcase  
  7. animal = "cat" #animal对象已被创建
  8. def animal.speak #在animal对象已被创建后,可赋于其新的方法,该方法不能被同类的其它对象共享,所以是私有的
  9.   puts "The #{self} says miaow"
  10. end
  11. animal.speak
  12. puts animal.upcase
复制代码



引用
Notice that it injected a new anonymous class directly in front of the object and put the new method there for you. This class is hidden though. If you call the animal.class() then you'll get a reference to String directly.


来看javascript

Javascript代码
  1. 1.var animal = /cat/;   
  2. 2.animal.speak = function speak() {   
  3. 3.  console.log("The " + this + " says miaow");   
  4. 4.};   
  5. 5.animal.speak();   
  6. 6.animal.test('caterpiller');  
  7. var animal = /cat/;
  8. animal.speak = function speak() {
  9.   console.log("The " + this + " says miaow");
  10. };
  11. animal.speak();
  12. animal.test('caterpiller');
复制代码
可以看到实现方式与 ruby相类似。


定义类
  1. Ruby

  2. Ruby代码  
  3. 1.class Dave   
  4. 2.end  
  5. class Dave
  6. end
复制代码

Javascript

Javascript代码
  1. 1.function Dave() {}  
  2. function Dave() {}
复制代码


类方法
ruby

Ruby代码
  1. 1.# Make a parent class   
  2. 2.class Person   
  3. 3.  # with an instance method   
  4. 4.  def greet   
  5. 5.    puts "Hello"  
  6. 6.  end  
  7. 7.  # and a class method.   
  8. 8.  def self.create   
  9. 9.    self.new  
  10. 10.  end  
  11. 11.end  
  12. 12.  
  13. 13.# Create a subclass   
  14. 14.class Dave < Person   
  15. 15.end  
  16. 16.# and test it.   
  17. 17.puts Dave.create   
  18. 18.puts Dave.new  
  19. 19.Dave.create.greet  
  20. # Make a parent class
  21. class Person
  22.   # with an instance method
  23.   def greet
  24.     puts "Hello"
  25.   end
  26.   # and a class method.
  27.   def self.create
  28.     self.new
  29.   end
  30. end

  31. # Create a subclass
  32. class Dave < Person
  33. end
  34. # and test it.
  35. puts Dave.create
  36. puts Dave.new
  37. Dave.create.greet
复制代码

引用
You see that it inserted a new anonymous class in the chain to store the create method


javascript

Javascript代码
  1. 1.// Make a parent class   
  2. 2.function Person() {}   
  3. 3.// with an instance method   
  4. 4.Person.prototype.greet = function greet() {   
  5. 5.  console.log("Hello");   
  6. 6.}   
  7. 7.// and a class method.   
  8. 8.Person.create = function create() {   
  9. 9.  return new this();   
  10. 10.};   
  11. 11.  
  12. 12.// Create a subclass   
  13. 13.function Dave() {}   
  14. 14.Dave.__proto__ = Person;   
  15. 15.Dave.prototype.__proto__ = Person.prototype;   
  16. 16.// and test it.   
  17. 17.console.log(Dave.create());   
  18. 18.console.log(new Dave);   
  19. 19.Dave.create().greet();  
  20. // Make a parent class
  21. function Person() {}
  22. // with an instance method
  23. Person.prototype.greet = function greet() {
  24.   console.log("Hello");
  25. }
  26. // and a class method.
  27. Person.create = function create() {
  28.   return new this();
  29. };

  30. // Create a subclass
  31. function Dave() {}
  32. Dave.__proto__ = Person;
  33. Dave.prototype.__proto__ = Person.prototype;
  34. // and test it.
  35. console.log(Dave.create());
  36. console.log(new Dave);
  37. Dave.create().greet();
复制代码


引用
Here we make the constructor inherit from it's parent constructor (so that "class methods" get inherited) and inherit the prototypes so that "instance methods" get inherited. Again there is no need for hidden classes since javascript allows storing function properties on any object.

论坛徽章:
0
2 [报告]
发表于 2011-12-07 08:39 |只看该作者
两个语言都是我很喜欢的啊。

论坛徽章:
0
3 [报告]
发表于 2011-12-07 13:14 |只看该作者
呃 这个有啥可比较的

论坛徽章:
0
4 [报告]
发表于 2011-12-09 11:23 |只看该作者
8托, js是伪面向对象, 等它把定义之前去掉 var 声明关键字再来比较才有意义。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP