- Python uses "classical inheritance. JS uses "prototypal" inheritance.
-
In python, you have an init constructor which generally contains some methods. In javascript, any function can be constructor by putting
new
in front of the function call. Make a method available to every instance of this javascript constructor by adding it to theprototype
property of the constructor.
class Example(object):
def __init__(self, args):
self.args = args
def example_function(self):
#something to self.args
f = Example(data)
f.example_function()
//python
function Example(args){
this.args = args;
}
Example.prototype.example_function = function() {
//do something to this.args
}
f = new Example();
f.example_function();
//javascript
- In python, functions and methods are different types.
- How to make an object inherit from another in javascript:
function Base() {
console.log('something');
}
function Derived() {
console.log('something2');
}
Derived.prototype = new Base();
- How to make a class inherit from another in python:
class Base(object):
pass
class Derived(Base):
pass
Source: Matt Chisholm (https://blog.glyphobet.net/essay/2557)
This post is a part of the #100daysofcode challenge.