When creating an object and two class I am getting an error “Cannot access fruit before initialization”
#Code
fruit = {
apple: {
color: ‘red’,
price: 100
},
orange: {
color: ‘orange’,
price: 120
},
banana: {
color: ‘yellow’,
price: 40
}
}
console.log(fruit.apple.price)
console.log(fruit.orange.color)
class FruitClass {
constructor(color, price) {
this.color = color;
this.price = price;
}
}
let apple = new FruitClass(“red”, 100)
console.log(apple)
let fruit = class {
constructor(color, price) {
this.color = color;
this.price = price;
}
}
apple = new fruit(“red”, 100)
console.log(apple);
#Error
fruit = {
^
ReferenceError: Cannot access ‘fruit’ before initialization
at Object. (C:\web-dev\class-and-objects.js:3:7)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47