Reputation: 1439
Let's say I've something like this:-
let allDataStuff = () => {
// declare data info
let people = {
name: "",
cars: [],
bikes: []
}
// cars data - function contructor
let Cars = function(id, name, brand) {
this.id = id
this.name = name
this.brand = brand
}
// bikes data - function contructor
let Bikes = function(id, name, brand, noOfWheels) {
this.id = id
this.name = name
this.brand = brand
this.noOfWheels = noOfWheels
}
// return all
return { people, Cars, Bikes }
}
I can access people
data in other function
like normal (as shown below)
let createNewPeopleData = () => { // other function
// get people data info
let peopleData = allDataStuff().people
console.log(peopleData) // this will log out all data currently store in the object
}
But I can't seem to access Cars
and Bikes
function constructor
like the way I did with getting people data as show above. Currently I have something like this:-
let createNewPeopleData = () => { // other function
// let's just say I've values in all these vars define below
// create new car data
let newCar = allDataStuff().Cars(id, carName, carBrand) // this throw error
// push it into array of cars
allDataStuff().people[cars].push(newCar)
// create new bike data
let newBike = allDataStuff().Bikes(id, bikeName, bikeBrand, noOfWjeels) this throw error
// push it into array of cars
allDataStuff().people[bikes].push(newBike)
}
It says allDataStuff is not a function
. What I want is to be able to access the Cars
and Bikes
function constructor from other function (like show above). Is it possible?
Upvotes: 0
Views: 35
Reputation: 85633
You'll need to instantiate with new
keyword:
let createNewPeopleData = () => {
let peopleData = allDataStuff()
let car = new peopleData.Cars(1,'Yamaha XYZ', 'Yamaha')
console.log(car)
}
Upvotes: 0