Is it okay to instantiate from "class" directly?

0

I understand that in javascript everything is an object. In making these examples I realized that the two ways of declaring the object that I need returns the same, is it right to do this?

    
asked by John 09.09.2018 в 20:51
source

1 answer

2
  

I explain it to you under 2 examples, the 2 scenarios that you have; then in one   the class has a name that identifies it and in another it is a class   anonymous that is being assigned as a constant / variable value

anonymous class

  

When you declare an anonymous class, as in your first scenario; by   example you will not be able to carry out the inheritance process, given the   which an anonymous class is limited by that aspect; second point in   an anonymous class as you could already notice you do not need to instantiate   object to access their methods, it is enough that you carry out the syntax of variable.método

const datos = new class{
  hello(){
    return 'Hello Worl';
  }
}

let obj = datos.hello()
console.log(obj)

named class

  

For your second case the class does have a name, so you can   inherit their properties and methods to a daughter class, in addition to that   When you need to access their properties and attributes, you must take   the instantiation process, as in this example

class Mundo 
{
  bye(){
    return 'Adios mundo';
  }
}

let dato = new Mundo
console.log(dato.bye())
    
answered by 09.09.2018 / 21:15
source