Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
Handle function in const class?

I have source code like this

  const item = function(p) {
     let model;
     p.setup = function() {
     }
  }

I am not sure how should I call this class? function component?

What I want to do is add my function to this and call this function from outside.

  const item = function(p) {
     let model;
     p.setup = function() {
     }
     function nextGame(){
         console.log("test");
     }
  }
  item.nextGame(); // error

However it doesn't work.

I am not familiar this style of javascript.

How should I add the function to this(is it class??) and call??

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

For object-oriented programming in javascript there are a few methods, as mentioned in the comments.

1. Factory Function

A factory function is a simple approach, as it just creates and returns an object.

function Item(model) {
  const item = {
    model,
    nextGame: () => { console.log('test') }
  }
  return item
}

const itemInstance = Item('Tesla');

itemInstance.nextGame()
console.log(itemInstance.model)

2. Constructor Function

Uses the new keyword to create an instance of the defined object type. It utilizes the function's prototype, which is accessible on all instances of the type.

function Item(model) {
    this.model = model;
}

Item.prototype.nextGame = () => { console.log('test') }

const itemInstance = new Item('Tesla');

itemInstance.nextGame()
console.log(itemInstance.model)

3. Class

You could also use a Class. Classes in javascript are provided as an abstraction of functions.

class Item {
  constructor(model) {
    this.model = model
  }
  
  nextGame() {
    console.log('test')
  }
}

const itemInstance = new Item('Tesla');

itemInstance.nextGame()
console.log(itemInstance.model)

about 4 years ago · Juan Pablo Isaza Relatório

0

What you describe and demonstrate in your snippet javascript is an anonymous function, you could even say that it is a functional variable.

To access methods using dot notation, you need to create a class (or javascript object).

example using the keyword 'class':

class MyClass 
{
   var name;
   var lastName;
   
   constructor(_name, _lastname)
   {
      this.name = _name;
      this.lastName = _lastname;
   }
   
   sayMyName()
   {
     window.alert(`${this.name} ${this.lastname}`);
   }
}

Now create an instance of this class. Like this:

const myName = new MyClass("John", "Doe");

and now call with the method of your class using notation. Like this:

myName.sayMyName();
about 4 years ago · Juan Pablo Isaza Relatório

0

class Item {

  // private field for whatever
  // got passed to the constructor.
  #value;

  constructor(value) {
    this.#value = value;
  }
  
  // prototypal methods with access to
  // private fields and private methods.

  setup() {
    // do something with `this.#value`
    console.log(this.#value);
  }
  nextGame() {
    console.log("next game");
  }  
}

const item = new Item("OP's mysterious `p` value");

console.log({ item });

console.log('item.setup() ...');
item.setup();

console.log('item.nextGame() ...');
item.nextGame();
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda