最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is array.push a form of reassignment? - Stack Overflow

programmeradmin1浏览0评论

I'm working in a project with several javascript files and part of what I'm doing right now is both migrating existing code to use newer ES6+ features as well as making sure we're abiding by the AirBnB Eslint Rules.

So, given that context, this is the specific situation:

let meta = [a.platform];

And right below that:

meta.push(a.browserName ? a.browserName : 'any');

So now the linter is giving me a warning: 'meta is never reassigned. Use const instead'.


I understand that meta = somethingNew would be reassigning the variable. But in this case, isn't this variable also something different than what it used to be when it was created?

or, to make it even more clear

Can I use a const to define an array that will receive new items? If not, why?

Also, if not: why is the linter throwing a warning?

I'm working in a project with several javascript files and part of what I'm doing right now is both migrating existing code to use newer ES6+ features as well as making sure we're abiding by the AirBnB Eslint Rules.

So, given that context, this is the specific situation:

let meta = [a.platform];

And right below that:

meta.push(a.browserName ? a.browserName : 'any');

So now the linter is giving me a warning: 'meta is never reassigned. Use const instead'.


I understand that meta = somethingNew would be reassigning the variable. But in this case, isn't this variable also something different than what it used to be when it was created?

or, to make it even more clear

Can I use a const to define an array that will receive new items? If not, why?

Also, if not: why is the linter throwing a warning?

Share Improve this question edited Aug 9, 2017 at 16:26 Jonathan Soifer asked Aug 9, 2017 at 16:23 Jonathan SoiferJonathan Soifer 2,8377 gold badges28 silver badges54 bronze badges 3
  • Reassignment of Object types (like Arrays) just means to rebind a name with another reference (to another Object type). When you mutate an Object type as in your example, you merely change its properties, but not the underlying reference. – user6445533 Commented Aug 9, 2017 at 18:39
  • "But in this case, isn't this variable also something different than what it used to be when it was created?" No. The variable still has the same array as value. The arrays was mutated in the meantime, but that doesn't make it a different value. – Felix Kling Commented Aug 9, 2017 at 18:59
  • If you worked with C language, might help to think of the const object (array, type etc) as an address pointer to a memory block. That const pointer address is fixed/mutated , cant be reassigned another const object. but the data in the memory address that the const object points to - can be amended. – Peter O Brien Commented May 31, 2024 at 22:02
Add a comment  | 

2 Answers 2

Reset to default 11

The only thing you have to know is that const has nothing to do with immutability. const simply allows you to prevent reassignment, which means that you cannot do that:

   
// OK 
const foo = '';
const bar = [];
const baz = {};

// WTF are we doing!?
foo = 'Foo';
bar = ['Bar'];
baz = {baz: 'Baz'};

However, in JavaScript, object values are mutable ; contrary to primitive values that are immutable. So if you put a string in a const, you will not be able to modify it at all, even with String.prototype methods (which return new strings).

const str = 'Lorem ipsum';

console.log(str.slice(6)); // This is actually a new string...
console.log(str); // The original string has not been modified!

It is quite different with arrays or object literals, though:

const arr = [];
const obj = {};

arr.push('Foo');
arr.push('Bar');

obj.foo = 'Foo';
obj.bar = 'Bar';

console.log(arr); // Modified!
console.log(obj); // Modified!

At this point, it should be clear: the linter shows this warning because, indeed, meta is never reassigned... A mutation is not a reassignment.

meta.push() doesn't reassign the variable, it just modifies the array that the variable refers to -- it's still the same array, but with different contents. So you can use const for the variable declaration, since you never make it refer to a different array.

The same is true if you assign to array elements. This is OK:

const meta = [a.platform];
meta[1] = somethingNew;

The second assignment doesn't change meta, it changes the array.

发布评论

评论列表(0)

  1. 暂无评论