Search code examples
javascriptpropertiesscopescoping

whats going on in this script


I am sorry i cant be very specific to metion what is going on with the script. I wrote this piece of script to understand property modification in the javascripit. I have a variable ctx and it has two properties ctx.exports and ctx.module.exports. As you can see int eh code below, i have establised the relation ctx.module.exports = ctx.exports so when ctx.exports gets modified ctx.module.exports holds the same value. but the script below suggests otherwise.

var ctx = {};
ctx.exports = {};
ctx.module = {
  exports: ctx.exports
}

ctx.exports = {
  h: "hello"
}


if (ctx.exports == ctx.module.exports) {
  console.log("hi");
} else {
  console.log("hey");
}

however when i store the ctx.exports value to a local variable exports in this case. exports becomes equal with ctx.module.exports on modification of ctx.exports

var ctx = {};
ctx.exports = {};
ctx.module = {
  exports: ctx.exports
}

var exports = ctx.exports;
ctx.exports = {
  h: "hello"
}


if (exports == ctx.module.exports) {
  console.log("hi");
} else {
  console.log("hey");
}

cn anybody explain me what is happening in this case? is this a type of scoping in java script. does this have a proper name?

edit

as expllained by the first answer the ctx.module.exports is checking against {} that is why it is a match. but in that case, why is this failing

var ctx = {};
ctx.exports = {};
ctx.module = {
  exports: ctx.exports
}

var exports = ctx.exports;
ctx.exports = {
  h: "hello"
}

if ({} == ctx.module.exports) {
  console.log("hi");
} else {
  console.log("hey");
}


Solution

  • Have a look at what is being compared. In the first example, you set ctx.module.exports to reference ctx.exports, which was an empty object: {}. Then you overwrote ctx.exports with a new value: { h: "hello" }. ctx.module.exports maintained its reference to the original object, while ctx.exports now referenced a different object: you're comparing {} against { h: "hello" }.

    In the second example, you're again overwriting ctx.exports with a new object, but you are comparing two references to the original object: {} to {}. It's a match.

    EDIT - for a little more clarity When you set the value of a variable, you are linking a name to a reference. Overwriting that variable creates a new reference with the same name, but does not modify the reference, which is why overwriting ctx.exports had no effect upon either other reference.

    EDIT RESPONSE TO QUESTION EDIT You can't compare objects like that. {} !== {} because each object literal declaration creates a new Object instance. The reason your references can be compared is because they are references to the same object, i.e., the one created by ctx.exports = {}.

    MORE EDITS! You can see this in effect by comparing the JSON value of each object: JSON.stringify({}) == JSON.stringify(ctx.module.exports) is true because it's comparing identical strings; {} == ctx.module.exports is false because it is comparing different objects, albeit two objects constructively identical.