0

what is the point of assign classes and functions to variables? I mean in other languages like Java, functions are functions and classes are classes ,to get what I mean fully check out the code below

const creature=class Animal {
  constructor(name) {
    this.name = name;
  }
}
const cat = new creature('Lilly');
console.log(cat.name);  // Lilly

const dog = new Animal('Boby');
console.log(dog.name);  //Error

const sum=(x,y)=>x+y;
console.log(sum(1,2));//3

function sum2(x,y) {
  return x+y;
}
console.log(sum2(2,1));//3

I feel it’s confusing to demonstrate between a function and a variable with more complex and sophisticated code

Bhmmam
  • 11
  • 3
  • Possible duplicate of [var functionName = function() {} vs function functionName() {}](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – Sjeiti Apr 10 '19 at 15:40
  • [`functions` are 1st class in JS](https://hackernoon.com/javascript-and-functional-programming-pt-2-first-class-functions-4437a1aec217?gi=81a2612a3a0e) – zer00ne Apr 10 '19 at 16:28
  • Possible duplicate of [What is meant by 'first class object'?](https://stackoverflow.com/questions/705173/what-is-meant-by-first-class-object) – zer00ne Apr 10 '19 at 16:29
  • A variable is just a container for a value. In JavaScript, functions and classes are values (more specifically: they are objects). Hence you can assign them to variables. Even just doing `class Foo {}` creates a variable `Foo` whose value is a class (really a constructor function). – Felix Kling Apr 11 '19 at 06:44

0 Answers0