Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
Can i create new object by define new keyword from object literal?

I have created an object by defining the 'new' keyword from the object literal. But the problem is I can't access that object without looping. so is there any way to access that object?

const animal = {
  animalDetails: function(a, b) {
    this.a = a
    this.b = b
  },

  animalOutPut: function() {
    console.log(this.a, this.b)
  },
}

// passing output by parameter ................................................................

animal.animalDetails('ape', 'Baboon')

// Call the object animal ................................................................

animal.animalOutPut()

// Create New Object Fruits from animal object literal ................................................................
let fruits = new animal.animalDetails('Apple', 'Blackberries')


for (let v in fruits) {
  console.log(fruits[v])
}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You want a class

class animal {

  constructor(a, b){

    this.a = a;
    this.b = b;
 
  }

  animalOutput(){

    console.log(this.a, this.b);

  }

}

You can easily define the class let tiger = new animal("tiger", "grrr") A class allows you to create multiple of them as well, much more useful then an object.

Hope this helped!

about 4 years ago · Juan Pablo Isaza Denunciar

0

You don't need to loop it. You can access the a and b properties just like you can with animal.

You can't use fruits.animalOutput() because animalOutput is a property only of the animal object. This object is not a prototype of objects created using new animalDetails(), so there's no inheritance.

const animal = {
  animalDetails: function(a, b) {
    this.a = a
    this.b = b
  },

  animalOutPut: function() {
    console.log(this.a, this.b)
  },
}

// passing output by parameter ................................................................

animal.animalDetails('ape', 'Baboon')

// Call the object animal ................................................................

animal.animalOutPut()

// Create New Object Fruits from animal object literal ................................................................
let fruits = new animal.animalDetails('Apple', 'Blackberries')

console.log(fruits.a, fruits.b);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda