Scope in javascript acting weird

前端 未结 6 1300
悲哀的现实
悲哀的现实 2021-02-18 18:47

Object are passed with their reference in javascript. Meaning change in that object from any where should be reflected. In this case, the expected output was {} for console.log(

6条回答
  •  日久生厌
    2021-02-18 18:59

    Okay, so you've figured out that JavaScript objects have reference semantics, so modifying a referent has an effect on the same object in the original scope.

    What you also need to realise is that = is not part of these rules; not only does it perform assignment, but it will also rebind the reference to a new object.

    Under the hood, so to speak, that's basically how your original references were formed.

提交回复
热议问题