JavaScript's Purer Object-Orientation
Mat Brown
9
Tech
Tekst piosenki
I'm finishing up the excellent Programming Languages course on Coursera, and just watched a couple of lectures comparing the decomposition styles of functional and object-oriented programming. Without getting into too much depth, the basic (and intuitive) idea is that functional languages group operations tightly together, whereas object-oriented languages group data types tightly. I'll illustrate with a simple example.
Say we have two data types, Dog and Bird. And let's say we have two operations, "talk" and "can_fly". In a functional language like ML, the operations are bound tightly:
fun talk (animal) =
Case animal of
Dog _ => "Woof"
| Bird _ => "Cheep"
Fun can_fly (animal) =
Case animal of
Dog _ => false
| Bird _ => trueOr in Erlang:
fun talk({dog, _}) -> "Woof";
Fun talk({bird, _}) -> "Cheep".
Fun can_fly({dog, _}) -> false;
Fun can_fly({bird, _}) -> true.In Ruby, on the other hand, the types themselves are bound tightly, with each type carrying its variant of the operation:
class Dog
Def talk
"Woof"
End
Def can_fly?
False
End
End
Class Bird
Def talk
"Cheep"
End
Def can_fly?
True
End
EndHowever, Ruby isn't limited to this way of organizing your code. We could achieve the same effect using functional decomposition:
def talk(animal)
Case animal
When Dog then "Woof"
When Bird then "Cheep"
End
End
Def can_fly?(animal)
Case animal
When Dog then false
When Bird then true
End
EndMethod overloading in statically-typed Java lets us do something similar to Erlang's function header pattern matching:
public String talk(Dog dog) {
Return "Woof";
}
Public String talk(Bird bird) {
Return "Cheep";
}
Public boolean canFly(Dog dog) {
Return false;
}
Public boolean canFly(Bird bird) {
Return true;
}So while in most situations the first Ruby example would be considered the best way to organize the code in question, neither Ruby nor Java forecloses on the option of organizing your code the way you would in a functional language.
JavaScript, however, is another story. JavaScript is an object-oriented but not class-based language. That is to say, with the exception of some built-in types (numbers, arrays, functions), all objects in JavaScript have the same type, which is simply the type Object. So, in JavaScript, we could certainly do this:
var Dog = function () {};
Dog.prototype = {
Talk: function () {
Return "Woof";
},
CanFly: function () {
Return false;
}
};
Var Bird = function () {};
Bird.prototype = {
Talk: function () {
Return "Cheep";
},
CanFly: function () {
Return true;
}
};In order to do functional decomposition in JavaScript, we'd like to do something like this:
function talk(animal) {
If (isDog(animal)) {
Return "Woof";
} else if (isBird(animal)) {
Return "Cheep";
}
}
Function canFly(animal) {
If (isDog(animal)) {
Return false;
} else if (isBird(animal)) {
Return true;
}
}But, as far as I know, there is simply no way to implement the isDog and isBird functions in a reliable way. One might look at the "constructor" property of objects, or perhaps define a typeOf() method on each prototype, but both of these approaches are easily fooled at runtime.
From this perspective, then, perhaps JavaScript is more "purely" object-oriented, in the narrow sense that object decomposition really is your only option for code organization.
Tłumaczenie
Brak
Polecani artyści
Najnowsze teksty piosenek
Sprawdź teksty piosenek i albumy dodane w ciągu ostatnich 7 dni