Conceptual similarity between Ruby’s and JavaScript’s inheritance models

To clear some potential confusion, let me start with the following statements

  • As far as I understand, inheritance is mostly about dynamic dispatch.
  • I understand how virtual pointer table based inheritance works and I assume that this is what ‘classical’ inheritance refers to.
  • I assume that ‘pure’ OO-style class-based inheritance refers either to either classical inheritance or hash-table based method lookups.
  • I understand how prototypical inheritance works and how it is fundamentally different from class-based inheritance.
  • I know how Ruby has constants, instance variables, class variables, singleton classes, etc. that act differently from methods in terms of inheritance and lookup. This question focuses on the method lookup.
  • I know how methods are not a first-class citizens in Ruby as opposed to JavaScript’s functions.
  • I know that JavaScript has values other than objects and that Ruby doesn’t.

Neither of the mentioned languages use what I refer to as class-based inheritance.

The thing that bothers me is that people seem to refer to similar things using contrasting terms. I know how JavaScript’s property lookup works and I consider it very closely related to how Ruby’s method lookup works. The question is targeted at the difference between what is labeled ‘prototypical inheritance’ with prototype-based lookup and ‘class-based OO inheritance’ with superclass-based lookup.

Basically, a JavaScript prototype seems to act the same as a Ruby class in terms of method lookup (with the exception that Ruby also traverses included modules), yet I often hear that Ruby is purely object-oriented and JavaScript does not have ‘real’ inheritance. The conceptual difference I see is that JavaScript’s first-class function objects use the same lookup as any other value, while Ruby’s method lookup obviously only works on methods.

To paraphrase, when executing the expression obj.method(), JavaScript doesn’t know it’s looking for a function in obj, it simply finds a value (maybe even using a getter) and tries to call it, while Ruby knows it searches for a method and uses the rules for method lookup. The difference is slightly more evident when ‘method()’ is not preceded by a dot, JavaScript then searches for a variable in enclosing lexical scopes, while Ruby searches the current scope for local variables and various superclasses for methods.

The first practical difference that comes to mind is that AFAIK there is no real OO-based analogy to the code below, and that Ruby seems to simulate this lack by using complicated rules by looking up different symbol types differently while still being as dynamic as possible.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class A { // Some base class
print() {
// this.props is used. If this was a function call it could easily be implemented in Ruby.
console.log(this.prop); // the this-binding stays the same
}
}
A.prototype.prop = 'A'; // Define <prop> in the base prototype.
class B extends A {}
instance = new B();
instance.print(); // 'A'
B.prototype.prop = 'B'; // Define <prop> in the inherited prototype
instance.print(); // 'B'
instance.prop = 'instance'; // Define <prop> in the object itself
instance.print(); // 'instance'
</code>
<code>class A { // Some base class print() { // this.props is used. If this was a function call it could easily be implemented in Ruby. console.log(this.prop); // the this-binding stays the same } } A.prototype.prop = 'A'; // Define <prop> in the base prototype. class B extends A {} instance = new B(); instance.print(); // 'A' B.prototype.prop = 'B'; // Define <prop> in the inherited prototype instance.print(); // 'B' instance.prop = 'instance'; // Define <prop> in the object itself instance.print(); // 'instance' </code>
class A { // Some base class
  print() {
    // this.props is used. If this was a function call it could easily be implemented in Ruby.
    console.log(this.prop); // the this-binding stays the same
  }
}

A.prototype.prop = 'A'; // Define <prop> in the base prototype.

class B extends A {}

instance = new B();
instance.print(); // 'A'

B.prototype.prop = 'B'; // Define <prop> in the inherited prototype
instance.print(); // 'B'

instance.prop = 'instance'; // Define <prop> in the object itself
instance.print(); // 'instance'

Nevertheless, the following code samples are very similar in how they work.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class A # Some base class
def print
puts 'A'
end
end
class B < A; end # 'real' inheritance
class C < B; end # ditto
instance = C.new
instance.print # 'A'
class B
def print # Shadowing of <print> in the top class
puts 'B'
end
end
instance.print # 'B'
class C
def print # Shadowing in the middle class
puts 'C'
end
end
instance.print # 'C'
def instance.print # Shadowing using a singleton method
puts 'instance'
end
instance.print # 'instance'
</code>
<code>class A # Some base class def print puts 'A' end end class B < A; end # 'real' inheritance class C < B; end # ditto instance = C.new instance.print # 'A' class B def print # Shadowing of <print> in the top class puts 'B' end end instance.print # 'B' class C def print # Shadowing in the middle class puts 'C' end end instance.print # 'C' def instance.print # Shadowing using a singleton method puts 'instance' end instance.print # 'instance' </code>
class A # Some base class
  def print
    puts 'A'
  end
end

class B < A; end # 'real' inheritance
class C < B; end # ditto

instance = C.new
instance.print # 'A'

class B
  def print # Shadowing of <print> in the top class
    puts 'B'
  end
end

instance.print # 'B'

class C
  def print # Shadowing in the middle class
    puts 'C'
  end
end

instance.print # 'C'

def instance.print # Shadowing using a singleton method
  puts 'instance'
end

instance.print # 'instance'
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class A {
print() {
console.log('A');
}
}
class B extends A {}
class C extends B {}
instance = new C()
instance.print() // 'A'
B.prototype.print = function print() { // Shadowing of <print> in the top prototype
console.log('B');
}
instance.print() // 'B'
C.prototype.print = function print() { // Shadowing in the middle prototype
console.log('C');
}
instance.print(); // 'C'
instance.print = function print() { // Shadowing using the object itself
console.log('instance');
}
instance.print(); // 'instance'
</code>
<code>class A { print() { console.log('A'); } } class B extends A {} class C extends B {} instance = new C() instance.print() // 'A' B.prototype.print = function print() { // Shadowing of <print> in the top prototype console.log('B'); } instance.print() // 'B' C.prototype.print = function print() { // Shadowing in the middle prototype console.log('C'); } instance.print(); // 'C' instance.print = function print() { // Shadowing using the object itself console.log('instance'); } instance.print(); // 'instance' </code>
class A {
  print() {
    console.log('A');
  }
}

class B extends A {}
class C extends B {}

instance = new C()
instance.print() // 'A'

B.prototype.print = function print() { // Shadowing of <print> in the top prototype
  console.log('B');
}

instance.print() // 'B'

C.prototype.print = function print() { // Shadowing in the middle prototype
  console.log('C');
}

instance.print(); // 'C'

instance.print = function print() { // Shadowing using the object itself
  console.log('instance');
}

instance.print(); // 'instance'

Does the code above, i.e. being able to use dynamically-bound methods and to gradually narrow down their lookup path, represent the only similarity between the two inheritance models?

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật