This article mainly introducesJavaScriptobject reference and assignment, and analyzes the operation skills and related Notes of JavaScript object reference and assignment in the form of examples. , Friends who need it can refer to
The examples in this article describe Javascript object reference and assignment. Share it with everyone for your reference, the details are as follows:
<script type="text/javascript"> //例子一: 引用 var myArrayRef = new Array(0,1,2); //创建数组对象 var mySeconArrayRef = myArrayRef; // 对象复制. myArrayRef[0] = 100; // 修改元素值 alert(mySeconArrayRef[0]); /** * 输出 100; 学过其它语言的都应该知道这里应该输出的是0 为什么输出的是100呢? * 上面程序通过把myArrayRef对象复制给了mySeconArrayRef这时就存在了2个独立的 但最初值是相同的对象 * 因为是独立的为什么修改myArrayRef会对别一个对象有影响呢?大家都知道只有当他们引用的是同一个对象时这时修改一个才会 * 对别一个产生影响.但是在javascript语言中创建的对象myArrayRef值中其时保存的是对象的引用(也就是一个地址). * 也就是 我用 new Array生成的保存在内存中而new Array把它所在的地方告诉了myArrayRef,myArrayRef又把这地址告诉了mySeconArrayRef * 他们两个都指向的是new Array生成对象的地址而不是把对象保存在myArrayRef中,所以通过其中的一个去修改值时其时是修改他们同指象的那对象. */ alert(mySeconArrayRef[0] ); //例子二: 赋值 var myVa = 'ABC'; //把ABC的值 赋予了myVa var myVb = myVa; // myVa 赋值给 myVb myVa = 'DEF'; //修改myVa /** * 输出的是:ABC. 因为是把值保存在了变量了 而不是保存的是引用地址,所以他们两个是相对独立的整体. */ alert(myVb); </script>
If you really want to copy objects without affecting each other, you must copy the methods and attributes in you by converting assignment or traversing key:value. .
Note: The sub-objects of objects are also references, so when traversing assignments, it is necessary to determine whether the sub-elements are objects. If the sub-elements are objects, continue traversing and assigning values to the sub-elements.
Conversion assignment method:
var data = {a:1,b:2,c:3,d:[0,1,2,3]}; var str = JSON.stringify(data); var data1 = $.parseJSON(str); //$为jQuery对象需要引入jQuery包 data1["e"] = 4; data1["d"][0] = 11; console.log(data); console.log(data1);
Output result:
{a: 1, b: 2, c: 3, d: [0,1,2,3]} {a: 1, b: 2, c: 3, d: [11,1,2,3], e: 4}
No influence on each other
When object references are passed as function parameters , they will still affect each other. Remember , as in the following example:
var data = {a:1,b:2,c:3,d:{q:4,w:5,e:6}}; var data1 = data; function con(data2){ data2["r"] = 5; console.log(JSON.stringify(data2)); } con(data1); console.log(JSON.stringify(data));
Output result:
{"a":1,"b":2,"c":3,"d":{"q":4,"w":5,"e":6},"r":5} {"a":1,"b":2,"c":3,"d":{"q":4,"w":5,"e":6},"r":5}
After the object reference is assigned, if the object is left blank, they will not be affected , as follows:
var arr = {"a":"1","b":"2"}; var arr1 = arr; arr = {}; arr["a"] = 2; console.log(arr1); console.log(arr);
Output result:
{"a":"1","b":"2"},{"a":2}
The above is the detailed content of Sample code sharing for JavaScript object reference and assignment. For more information, please follow other related articles on the PHP Chinese website!

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
