У нас вы можете посмотреть бесплатно Understanding Reassigning Non-Primitives in JavaScript Functions: A Deep Dive или скачать в максимальном доступном качестве, видео которое было загружено на ютуб. Для загрузки выберите вариант из формы ниже:
Если кнопки скачивания не
загрузились
НАЖМИТЕ ЗДЕСЬ или обновите страницу
Если возникают проблемы со скачиванием видео, пожалуйста напишите в поддержку по адресу внизу
страницы.
Спасибо за использование сервиса ClipSaver.ru
Explore how reassigning non-primitive values inside functions works in JavaScript. We break down the behavior of arrays and objects, clarifying the concept of passing by reference and why certain reassignments do not reflect outside function scope. --- This video is based on the question https://stackoverflow.com/q/74819733/ asked by the user 'Hasaan Khan' ( https://stackoverflow.com/u/20615928/ ) and on the answer https://stackoverflow.com/a/74819750/ provided by the user 'Spectric' ( https://stackoverflow.com/u/14251221/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How does reassigning non-primitives in functions work (Javascript)? Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com. --- Understanding Reassigning Non-Primitives in JavaScript Functions: A Deep Dive JavaScript can often be puzzling, particularly when dealing with function parameters and non-primitive values like arrays and objects. One such conundrum that many developers encounter is how reassigning non-primitives within functions behaves. In this post, we will explore this concept, breaking down how passing by reference works, and ultimately clarifying why output may not always reflect changes as one might initially expect. The Problem: Confusion Around Non-Primitives Consider the following example: [[See Video to Reveal this Text or Code Snippet]] At first glance, one might assume that since we're passing an array (a non-primitive) to the changeto5 function, the value at the memory address would change from [9] to [5]. However, this isn't what happens. After executing the function, when we log the original array, it still outputs [9]. So, why does this happen? The Explanation: What Actually Happens Passing by Reference vs. Passing by Value The root of the confusion lies in understanding how JavaScript handles function arguments: Non-Primitives and References: When we pass a non-primitive (like an array or object) into a function, we don't pass the original object itself; instead, we pass a reference (or address) to it. However, this reference is copied, meaning that within the function, the variable holding the reference points to the same object in memory. Scope of the Reference: When you perform reassignment inside the function, like array = [5];, you’re not changing the original array but creating a new array and changing array to point to that new location. Therefore, the original variable (a) outside the function still points to the original array at memory location 0x01. Modifying Contents: Not the Same as Reassignment If instead of reassigning, you modify the contents of the array directly, as shown below, the original array will change: [[See Video to Reveal this Text or Code Snippet]] In this case, since array[0] modifies the content at the memory location 0x01, the change reflects outside of the function. The key takeaway is that while you can modify the elements of the array through its reference, reassigning the reference itself does not affect the original reference. Summary: Clarifications and Key Takeaways In conclusion, understanding how non-primitives behave in functions requires a clear grasp of how JavaScript manages references: Reassignment Creates a New Reference: When you reassign a non-primitive inside a function, you create a new reference and do not change the original object. Direct Modifications Persist: As long as you modify the contents of the passed object, those changes will be seen outside the function. By keeping these principles in mind, developers can avoid common pitfalls related to function arguments in JavaScript, fostering a clearer understanding of how to effectively manage and manipulate data. With this knowledge, we hope you feel more confident navigating the complexities of JavaScript functions and their treatment of non-primitive values!