Mat Brown - JavaScript's Purer Object-Orientation [tekst, tłumaczenie i interpretacja piosenki]

Wykonawca: Mat Brown
Gatunek: 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 piosenki

Nikt nie dodał jeszcze tłumaczenia do tej piosenki. Bądź pierwszy!
Jeśli znasz język na tyle, aby móc swobodnie przetłumaczyć ten tekst, zrób to i dołóż swoją cegiełkę do opisu tej piosenki. Po sprawdzeniu tłumaczenia przez naszych redaktorów, dodamy je jako oficjalne tłumaczenie utworu!

+ Dodaj tłumaczenie

Wyślij Niestety coś poszło nie tak, spróbuj później. Treść tłumaczenia musi być wypełniona.
Dziękujemy za wysłanie tłumaczenia.
Nasi najlepsi redaktorzy przejrzą jego treść, gdy tylko będzie to możliwe. Status swojego tłumaczenia możesz obserwować na stronie swojego profilu.

Interpretacja piosenki

Dziękujemy za wysłanie interpretacji
Nasi najlepsi redaktorzy przejrzą jej treść, gdy tylko będzie to możliwe.
Status swojej interpretacji możesz obserwować na stronie swojego profilu.
Dodaj interpretację
Jeśli wiesz o czym śpiewa wykonawca, potrafisz czytać "między wierszami" i znasz historię tego utworu, możesz dodać interpretację tekstu. Po sprawdzeniu przez naszych redaktorów, dodamy ją jako oficjalną interpretację utworu!

Wyślij Niestety coś poszło nie tak, spróbuj później. Treść interpretacji musi być wypełniona.

Lub dodaj całkowicie nową interpretację - dodaj interpretację
Wyślij Niestety coś poszło nie tak, spróbuj później. Treść poprawki musi być wypełniona. Dziękujemy za wysłanie poprawki.
Najpopularniejsze od Mat Brown
XPath is actually pretty useful once it stops being confusing
257
{{ like_int }}
XPath is actually pretty useful once it stops being confusing
Mat Brown
Passenger Standalone with debugger and pry
249
{{ like_int }}
Passenger Standalone with debugger and pry
Mat Brown
JavaScript's Purer Object-Orientation
229
{{ like_int }}
JavaScript's Purer Object-Orientation
Mat Brown
Komentarze
Polecane przez Groove
we can’t be friends (wait for your love)
5,3k
{{ like_int }}
we can’t be friends (wait for your love)
Ariana Grande
HILL BOMB
273
{{ like_int }}
HILL BOMB
Guzior
FRI(END)S
2,1k
{{ like_int }}
FRI(END)S
V (뷔) / BTS
NIE OBIECAM
441
{{ like_int }}
NIE OBIECAM
Małach & Rufuz
Nadziei Słowa
119
{{ like_int }}
Nadziei Słowa
KęKę (PL)
Popularne teksty
Siedem
51,7k
{{ like_int }}
Siedem
Team X
34+35
42,4k
{{ like_int }}
Love Not War (The Tampa Beat)
25,5k
{{ like_int }}
Love Not War (The Tampa Beat)
Jason Derulo
SEKSOHOLIK
161,6k
{{ like_int }}
SEKSOHOLIK
Żabson
Snowman
74,6k
{{ like_int }}
Snowman
Sia