Sign in
Log inSign up

Day86 of #100Daysof Code :::: JavaScript Class Expressions.

Chidera Paul Ugwuanyi's photo
Chidera Paul Ugwuanyi
·Dec 16, 2018

Classes can be found in a function. A lot can be done with them there. For instance, a class in a function can be passed around, returned and can be found inside another expression.

Lets take a look at a functionn that returns a class

function createClass( word ){

  //return a class after a creating it
  return class{
    greeting(){
      console.log( word )
    };
  };
}

let Developer = createClass("New class");
new Developer().greeting() //"New Class"

The above function created and returned a class. We can create and return multiple functions in a class.

function createClass( word,word1 ){

  //return a class after a creating it
  return class{
    greeting(){
      console.log( word )
    };
    greeting1(){
      console.log( word1 )
    };
  };
}

let Developer = createClass("New class", "second class");
new Developer().greeting() //"New Class"
new Developer().greeting1() //"second Class"

In this one we created and returned two methods in the class created with a function.

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct