ruby与javascript面向对象编程的比较

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

作者分析了ruby与javascript两者在面向对象模式的区别,作者用图形表达这两者在面向对象模式上的区别,值得一看。这里只摘取重点部分,有兴趣的读者可看原文。
Ruby
先来看一个简单的字符串:
animal = "cat"

对象图如下:
[img]http://howtonode.org/object-graphs-3/animal.rb.dot[/img]
[quote]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.[/quote]

再来看JavaScript
var animal = "cat";

[img]http://howtonode.org/object-graphs-3/animal.js.dot[/img]
[quote]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.[/quote]

对象私有方法,注意这里说的是对象,而不是类。类的私有方法在类的声明中用private修饰。而对象私有方法是指一个对象的方法相对同类的其它对象是私有的。如下代码:
animal = "cat" #animal对象已被创建
def animal.speak #在animal对象已被创建后,可赋于其新的方法,该方法不能被同类的其它对象共享,所以是私有的
puts "The #{self} says miaow"
end
animal.speak
puts animal.upcase

[img]http://howtonode.org/object-graphs-3/singleton.rb.dot[/img]
[quote]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.[/quote]

来看javascript
var animal = /cat/;
animal.speak = function speak() {
console.log("The " + this + " says miaow");
};
animal.speak();
animal.test('caterpiller');

可以看到实现方式与 ruby相类似。
[img]http://howtonode.org/object-graphs-3/singleton.js.dot[/img]

定义类
Ruby
class Dave
end

[img]http://howtonode.org/object-graphs-3/dave.rb.dot[/img]

Javascript
function Dave() {}

[img]http://howtonode.org/object-graphs-3/dave.js.dot[/img]

类方法
ruby
# Make a parent class
class Person
# with an instance method
def greet
puts "Hello"
end
# and a class method.
def self.create
self.new
end
end

# Create a subclass
class Dave < Person
end
# and test it.
puts Dave.create
puts Dave.new
Dave.create.greet

[img]http://howtonode.org/object-graphs-3/dave2.rb.dot[/img]
[quote]You see that it inserted a new anonymous class in the chain to store the create method[/quote]

javascript
// Make a parent class
function Person() {}
// with an instance method
Person.prototype.greet = function greet() {
console.log("Hello");
}
// and a class method.
Person.create = function create() {
return new this();
};

// Create a subclass
function Dave() {}
Dave.__proto__ = Person;
Dave.prototype.__proto__ = Person.prototype;
// and test it.
console.log(Dave.create());
console.log(new Dave);
Dave.create().greet();

[img]http://howtonode.org/object-graphs-3/dave2.js.dot[/img]
[quote]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.[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值