As I was creating a class constructor to manage my database, I found myself questioning the behavior of JavaScript along the way.
To simplify things and understand the problem better, I stripped it down to its bare minimum. Here is the core of the issue:
name.js
class Name {
constructor(){
this.value = 'mo'
}
setName(){
this.value = 'moom'
}
}
export default new Name()
sayName.js
import name from './name'
const outsideName = name.value
function sayName(){
const insideName = name.value
console.log(insideName) // => moon
console.log(outsideName) // => mo
}
export default sayName
index.js
import name from './name'
import sayName from './sayName'
name.setName()
sayName()
Initially, I expected to see the same result in the console (moon). But why are the console outputs showing different values (mo !=== moon)? I would greatly appreciate any insights on this matter.