ruby与javascript面向对象编程的比较
ruby与javascript面向对象编程的比较
原文:http://howtonode.org/object-graphs-3
作者分析了ruby与javascript两者在面向对象模式的区别,这里只摘取重点部分,有兴趣的读者可看原文。
Ruby
先来看一个简单的字符串:
Ruby代码1.animal = "cat"
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.var animal = "cat";
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.animal = "cat" #animal对象已被创建
2.def animal.speak #在animal对象已被创建后,可赋于其新的方法,该方法不能被同类的其它对象共享,所以是私有的
3.puts "The #{self} says miaow"
4.end
5.animal.speak
6.puts animal.upcase
animal = "cat" #animal对象已被创建
def animal.speak #在animal对象已被创建后,可赋于其新的方法,该方法不能被同类的其它对象共享,所以是私有的
puts "The #{self} says miaow"
end
animal.speak
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.var animal = /cat/;
2.animal.speak = function speak() {
3.console.log("The " + this + " says miaow");
4.};
5.animal.speak();
6.animal.test('caterpiller');
var animal = /cat/;
animal.speak = function speak() {
console.log("The " + this + " says miaow");
};
animal.speak();
animal.test('caterpiller');可以看到实现方式与 ruby相类似。
定义类Ruby
Ruby代码
1.class Dave
2.end
class Dave
end
Javascript
Javascript代码1.function Dave() {}
function Dave() {}
类方法
ruby
Ruby代码1.# Make a parent class
2.class Person
3.# with an instance method
4.def greet
5. puts "Hello"
6.end
7.# and a class method.
8.def self.create
9. self.new
10.end
11.end
12.
13.# Create a subclass
14.class Dave < Person
15.end
16.# and test it.
17.puts Dave.create
18.puts Dave.new
19.Dave.create.greet
# 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
引用
You see that it inserted a new anonymous class in the chain to store the create method
javascript
Javascript代码1.// Make a parent class
2.function Person() {}
3.// with an instance method
4.Person.prototype.greet = function greet() {
5.console.log("Hello");
6.}
7.// and a class method.
8.Person.create = function create() {
9.return new this();
10.};
11.
12.// Create a subclass
13.function Dave() {}
14.Dave.__proto__ = Person;
15.Dave.prototype.__proto__ = Person.prototype;
16.// and test it.
17.console.log(Dave.create());
18.console.log(new Dave);
19.Dave.create().greet();
// 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();
引用
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. 两个语言都是我很喜欢的啊。 呃 这个有啥可比较的 8托, js是伪面向对象, 等它把定义之前去掉 var 声明关键字再来比较才有意义。
页:
[1]