Javascript 中几种数组赋值的区别及=号的作用

Wayne posted @ Fri, 18 Nov 2016 15:17:26 +0000 in Experience with tags Javascript , 2208 readers

Javascript中,目前要把一个数组的内容等于另一个数组,我会的手段大致有三种:

  1. a = b;

  2. a = b.slice(0);

  3. b.forEach(function(element){ a.push(element);});

这三种有什么不同呢?

 

这三种写法的不同究其原因都出在=号的作用上。数组间的等号可以认为是一个别名。也就是 a = b 的效果等同于 b.alias => a, 给 b 取了个叫a的别名。数组还是同一个数组,多了个名字而已。于是不论用名字 a 操作还是用名字 b 操作,改变的都是同一个数组,换个名字都能看到效果。比如:

var b = [1];
var a = b;
a.push(2);
b.push(3);
console.log(a);  //[1,2,3]
console.log(b);  //[1,2,3]

而 b.slice(0) != b 本身, 只是等于 b 的内容。 a = b.slice(0); 相当于先 new 了一个新的匿名 array,内容为 b 的内容,然后取了个名字叫 a。因为是new出来的,所以用 a = b.slice(0) 后,操作 a  和操作 b 是操作两个不同的数组。

 

那么,什么时候用第三种呢?要用第三种,很明显,首先 a 必须是一个已经存在的数组,不然会报 undefined。第三种的使用场景有比如这样的:

var a = [];
var b = [1];
var c;
afunction(a){ c = a;};  //一个函数,使用a作为传入参数,只调用一次

//此时想把 b 的值赋给c。

因为 c 只在函数体内才能被赋值,而这个值只能通过a来进行。 如果用 a = b, 那么效果等同于 (Original Array a) . removeName(a), 然后 b.alias(a);  而 c 依然是那个 Original Array a,只是丢失了原本a这个名字,只剩下c这个名字了。类似的,如果用 a = b.slice(0), 也无法改变c,只是让c这个数组丢失了另一个叫a的名字而已。这时候,用第三种,把 b 的内容逐个加到已有的 a 里面,才能同时操作到 c。

 

incometaxindiaefilin said:
Sun, 31 Jul 2022 06:02:40 +0000

The Income Tax department of India is a centralized form of association where they keep track of tax redemption from people all across India, and we have seen people surrounding us talk about Income tax refund and the status of the application most of the times. incometaxindiaefiling As you already know that the department makes a small deduction from your salaried account and sometime during December you are eligible to apply for tax refund application. Once you have applied for the refund process then the Income Tax department takes around 3-4 months and releases the status of every application basing on their calculation, but in order for all the steps to happen that we've mentioned it is necessary for you to apply for the tax refund in the first place.

AP 6th Class Telugu said:
Wed, 21 Sep 2022 17:20:37 +0000

Telugu is the first or mother language for all Telugu Medium students studying at the state board of Andhra Pradesh. Those Class 6th Standard students studying other medium schools have chosen Telugu as a Second or Third language from their electives. AP 6th Class Telugu Question Paper Every government and private school on the state board is working with SCERT Syllabus & curriculum. Telugu is the first or mother language for all Telugu Medium students studying at the state board of Andhra Pradesh. Those Class 6th Standard students studying other medium schools have chosen Telugu as a Second or Third language from their electives.

Alyssa said:
Sat, 21 Jan 2023 12:22:21 +0000

The significance of the equal sign between arrays in JavaScript lies in its ability to create an alias. When you use the assignment operator (=) between arrays, it is the same as assigning the same array a new name. The <a href="https://homesbypedroreyes.com/home-valuation/">real estate prices Marin County</a> array itself isn't changed, it is just given a new alias. For example, if you set a = b, it is the same as b.alias => a, giving b an alias called a. Even so, any changes made to the array using either the name a or the name b will yield the same result, as it is still the same underlying array. Consequently, the three methods of array assignment in JavaScript differ in how they handle aliases.

Emma said:
Sat, 21 Jan 2023 12:23:05 +0000

The equal sign between arrays in JavaScript lies in its ability to create an alias. When you use the assignment operator (=) between arrays, it is the same as assigning the same array a new name. The array itself isn't changed, it is just real estate prices Marin County given a new alias. For example, if you set a = b, it is the same as b.alias => a, giving b an alias called a. Even so, any changes made to the array using either the name a or the name b will yield the same result, as it is still the same underlying array. Consequently, the three methods of array assignment in JavaScript differ in how they handle aliases.

Free CIBIL score che said:
Sat, 21 Jan 2023 14:52:07 +0000

CIBIL is an organization and an entity gathers all the consumer’s loan, credit, payment and money related transaction, and this gives a general behavior example and statics allowing to generate a score for each customer also calls as CIBIL score. Free CIBIL score check Check CIBIL score is before you try to apply for a home loan, want to buy something new on EMI, or need to request any credit facility for a loan, then the primary thing they would look for is what is your CIBIL score to be precise.

NCERT 3rd Class Boo said:
Fri, 28 Jul 2023 10:28:53 +0000

NCERT 3rd Class New Students get your Class Book in Your School, Students Any Problem NCERT 3rd Books 2019-2021 not Available no Problem Students Visit our Website Download e-Class Book Subject Wise Books, NCERT Books 2024 Download in zip File format. Chapters’ Wise textbook will be in PDF format. NCERT Class 3 Books 2024 Very Important Student Carer, Students Books Regular Redding in Exam Time at Best NCERT 3rd Class Book 2024 Performance.NCERT Class 3 Textbooks 2024 for Providing the best learning Platform with Various Resources to Support and enhance Educational Delivery and Management. Students Download NCERT Books 2024 for Class 3 Subject Wise and Hindi, Urdu, English Medium Wise our Website Provide Pdf Format.


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter