0

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])
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `console.log(fruits.a)` – Barmar Apr 27 '22 at 17:47
  • 1
    You want a class, not an object. – Barmar Apr 27 '22 at 17:47
  • Thanks :). But I want to know that is a lack of this way of writing object constructor instead of using class? I'm just trying to understand for which deficit we are using CLASS syntax. – Mohiuddin Apr 27 '22 at 20:11
  • 1
    `animalOutput` is not a property of the object created using `new animal.animalDetails()`, it's a property of `animal`. You need to learn about prototypes. Classes handle these details automatically. – Barmar Apr 27 '22 at 20:13

2 Answers2

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!

KoderM
  • 382
  • 2
  • 15
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);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Yes animalOutput is not a part of the fruits object absolutely right but I'm thinking how to bring for-in loop to the fruits object value :(. where is it comes from? I know about class but I want to know about this ghosting behavior :) – Mohiuddin Apr 28 '22 at 19:29
  • That's how you loop over any object's properties. There's nothing special about using it here. – Barmar Apr 28 '22 at 19:31
  • But you said in the question that you can't access it without looping. I've shown in my answer that this isn't true. It's just like any other object -- if you know the property name, you just use `.`. – Barmar Apr 28 '22 at 19:32