I've been working in React
and Redux
for past 2 years but when I was using inheritance
in javascript, I found the difference between these 2 type of function declaration in javascript.
I've the class a
and a class b
which inherits from class a
and whenever I'm running the following snippet, it logs out
bfunc called from class a
afunc called from class b
I assume that the syntax bfunc = function(){
puts the function in this
and the syntax afunc() {
puts the function in the prototype of class but I'm not really sure. Can someone please explain this behaviour?
class a {
afunc() {
console.log('afunc called from class a');
}
bfunc = function() {
console.log('bfunc called from class a');
}
}
class b extends a {
afunc() {
console.log('afunc called from class b');
}
bfunc() {
console.log('bfunc called from class b');
}
}
const bObject = new b();
bObject.bfunc();
bObject.afunc();
bfunc called from class a
afunc called from class b