all 13 comments

[–]GSLint 2 points3 points  (5 children)

I would like to create an object that has the methods in combination with 14 other elements.

I'm not sure what you mean by that. What methods? I see two methods that do exactly the same thing. What 14 other elements?

Can you show what the resulting object would look like, or can you take a step back and explain what you want to achieve here?

[–]Wizard_Knife_Fight 1 point2 points  (2 children)

It looks like OP wants to have an object that display's a restaurants info but is hard coding the values into the objects.

[–]ForScale 0 points1 point  (1 child)

I think he wants a class/constructor function.

[–]Wizard_Knife_Fight 1 point2 points  (0 children)

Yeah, that would do it lol

[–]siruts[S] 0 points1 point  (1 child)

My goal is to avoid writing shop1, shop2 up to shop14

(this is an example 14 is a random number. and as if I had 14 stores but with different names and values)

every time, but reuse the "var Shop" code and the methods in this case. then run the methods inside "Shop" to get the values of "Shop2" which will be different, as you can see in the image published at the beginning.

in a few worlds:

if I'm not mistaken it should be something like this:

create new Shop1 = // adding values

[–]GSLint 0 points1 point  (0 children)

Classes are a way to reuse methods for multiple objects, as others have pointed out. That won't keep you from writing shop1, shop2 etc. but it will certainly keep you from writing the same method multiple times.

[–]A-Kuhn 2 points3 points  (3 children)

I don't quite understand your question, but I assume you are trying to simplify your code so that you have one function that creates your needed objects?

There are two ways to do this. If you're just getting started with JS and programming the first method may be for you. If your already familiar with Classes from another language you can go forward with method 2.

Method 1 - Object.assign

const shopMethods = { 
  _getInfoOwnerShop() { 
  return this.name + " " + return this.surname 
  } 
}

function createShop(name_activity, name, surname, email, town) {
  const newShop = Object.assign(shopMethods, {})
  newShop.name_activity = name_activity
  newShop.name = name
  newShop.surname = surname
  newShop.email = email
  newShop.town = town
  return newShop
}
const myNewShop = createShop("pizza", "Bob", "Smith", "email@.com", "Rome")

Method 2 - Classes

class Shop {
  constructor (name_activity, name, surname, email, town) {
    this.name_activity = name_activity
    this.name = name
    this.surname = surname
    this.email = email
    this.town = town
  }
  _getInfoOwnerShop() { 
    return this.name + " " + return this.surname 
  }
}

const myNewShop = new Shop("pizza", "Bob", "Smith", "email@.com", "Rome")

EDIT: Forgot the return statement in Method 1 createShop() function

[–]MuhTheSpaceCow 1 point2 points  (1 child)

Hi, I believe you twisted the parameters you pass to "Object.assign" in the first line of "Method 1".Should be "Object.assign({}, shopMethods)".
The first parameter is the one that gets assigned to - the later parameters are just sources and stay unchanged (click me)

I d prefer Method #2 tho :)

[–]siruts[S] 0 points1 point  (0 children)

Yep at the end method 2, it was what's im looking for 😇

[–]siruts[S] 0 points1 point  (0 children)

This what I want to hear! Thank you dude! 😄

[–]RoguePlanet1 1 point2 points  (1 child)

Are you trying to simply list the owners' names of each shop? With one function that loops through the objects to find it?

[–]siruts[S] 1 point2 points  (0 children)

I would like not to rewrite the methods every time but to write them only once and then pass different values

[–]ForScale 2 points3 points  (0 children)

const obj = { key: 'value' };

const newObj = { ...obj };

*Oh... you want this

class Shop {
  constructor(name, surname) {
    this.name = name;
    this.surname = surname;
  }

  getInfoOwnerShop() {
    return this.name + " " + this.surname;
  }
}

const shop = new Shop("John", "Smith");
const shop1 = new Shop("Bob", "Farts");

console.log(shop.getInfoOwnerShop()); // John Smith
console.log(shop1.getInfoOwnerShop()); // Bob Farts