Day 12 of 21 Days of Javascript
Today I covered prototypes (a quirk of javascript) and the related topic of classes. The protoype topic is an example of an area I can skim over, since the convention today in Javascript is to use the object-oriented approach (more on this here.)
Covering classes was relatively easy since I'm already familiar with them from python. Here's an example of how to create and instantiate a class in both languages.
Javascript:
class BlogPost {
constructor(title, author) {
this.title = title;
this.author = author;
}
summary() {
return `"${this.title}" by ${this.author}`;
}
}
const post = new BlogPost("My First Post", "Neo");
console.log(post.summary());
// → "My First Post" by Neo
Python:
class BlogPost:
def __init__(self, title, author):
self.title = title
self.author = author
def summary(self):
return f'"{self.title}" by {self.author}'
post = BlogPost("My First Post", "Neo")
print(post.summary())
# → "My First Post" by Neo
I'm starting to appreciate why developers say that if you know one language, it's easier to learn another. The broad principles are similar across many programming languages. It's usually just a matter of differences in syntax.
That said, it's worth noting that there are languages that use markedly different paradigms. For example you have,
- functional programming in Lisp, Haskell;
- declarative programming in HTML, SQL;
- and logic programming in Prolog.
Moreover, languages within the same paradigm can also have different philosophies on how to do similar things. Objective oriented languages like C++, Python, and Ruby are an example of this.