0

I am not sure what I am doing wrong here, or if I am misunderstanding let vs var/this as per https://www.w3schools.com/js/js_let.asp Hopefully somebody can explain.

The below sample code demonstrates the problem:

testObjectScope(){
    let localObject=this.testObject;
    localObject["c"]="charlie";
    return localObject;
}

with output in developer console in first line of function:

this.testObject: Object
a: "alpha"
b: "bravo"
__proto__: Object

then at the return line:

this.testObject: Object
a: "alpha"
b: "bravo"
c: "charlie"
__proto__: Object

localObject: Object
a: "alpha"
b: "bravo"
c: "charlie"
__proto__: Object

however I would expect 'let' should not be able to change the object property (ie be a deep copy) and it should be this:

this.testObject: Object
a: "alpha"
b: "bravo"

__proto__: Object
localObject: Object
a: "alpha"
b: "bravo"
c: "charlie"
__proto__: Object

so does 'let' not declare a variable within the function that cannot be accessed anywhere else. Therefore when the function is done and scope is away from function how is it changing the localObject property create a change with testObject

Datadimension
  • 872
  • 1
  • 12
  • 31
  • "*however I would expect 'let' should not be able to change the object property (ie be a deep copy)*" I'm not sure I follow. `let` only declares a variable. It doesn't do anything other than that. – VLAZ Oct 31 '19 at 14:35
  • thanks for asking clarification, appended to question – Datadimension Oct 31 '19 at 14:37
  • Yes, `let` declares a variable that you cannot access from a different scope but you are *not* accessing this variable in order to change `this.testObject`. `let localObject` is merely a variable that points to *the same object*, you don't get a copy of this object. `let a = {}; let b = a; let b.foo = "bar"; console.log(a)` will show you `{foo: "bar"}` because both `a` and `b` are *the same* object. Doesn't matter if it's two different variables. – VLAZ Oct 31 '19 at 14:40
  • Does this answer your question? [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – VLAZ Oct 31 '19 at 14:42

2 Answers2

0

What you have done is assigning the testObject to localObject. Although let is only valid under the nearest function scope, you can still operate the reference of testObject. Try pass by value (which will not affect the original object) instead of pass by reference as below:

testObjectScope(){
    let localObject = {};
    localObject["a"] = this.testObject["a"];
    localObject["b"] = this.testObject["b"];
    localObject["c"] = "charlie";
    return localObject;
}

You will see testObject remains the same.

Orio Ryo
  • 132
  • 1
  • 2
  • 8
  • close, but 'manually' doing it is prone to error, hence my solution which applies to differing objects. Thanks anyway – Datadimension Oct 31 '19 at 15:39
  • Yes you are right, but the main idea is that you need to deep clone the object. Do not try to operate the reference of original object. Otherwise you won't reach the expectation. – Orio Ryo Nov 01 '19 at 01:27
0
testObjectScope(){
    let localObject = $.extend(true, {}, this.testObject);
    localObject["c"]="charlie";
    return localObject;
}

at return values are as required:

this.testObject: Object
a: "alpha"
b: "bravo"
__proto__: Object

localObject: Object
a: "alpha"
b: "bravo"
c: "charlie"
__proto__: Object
Datadimension
  • 872
  • 1
  • 12
  • 31