0

I have this piece of code:

    sliderData () {
        let sliderData = this.product.slider; 

        sliderData.title = 'test';

        // {{ $t('related_product_slider_header', { category: 'hello', designer: 'hey' }) }}
        return sliderData;
    }

Then es-lint(prefer-const) warns me and tells me that:

'sliderData' is never reassigned. Use 'const' instead.eslint(prefer-const)

But that's a lie, isn't it? I do reassign the object one line down.

I don't really get it.

FooBar
  • 5,752
  • 10
  • 44
  • 93
  • You are not actually reassigning the object. The reference of sliderData is constant in this case, you are just changing a property value. If you switch to const your program will run ok – Cristopher Rosales Feb 11 '20 at 15:48

3 Answers3

4

You don't reassign sliderData. You change a property. As far as JS is concerned, "assigning" an object only ever refers to using it as the left hand term of the assignment operator as in:

sliderData = someObject;

So:

const sliderData = this.product.slider; 

sliderData.title = 'test'; //This is fine

sliderData = new Object(); //error

Note: This is because the value of "sliderData" is a reference containing the address of the object, not the object itself: this is also why equivalency fails between two objects unless one was assigned with the value of the other.

Jawi
  • 360
  • 1
  • 8
2

You are not reassigning the address that sliderData is storing, you are simply mutating that object.

You can and should use const instead of let, because you are not reassigning the sliderData variable. If for exmaple you were to write sliderData = 3, this is reassigning, sliderData.something = 3 is not reassigning.

Daniel B.
  • 2,491
  • 2
  • 12
  • 23
1

As long as you are not reassigning the sliderData its considered as an const. Though you declare an object as const you can add properties and edit values in it. Please see below:

const car = {type:"Fiat", model:"500", color:"white"};

// You can change a property:
car.color = "red";

// You can add a property:
car.owner = "Johnson";
Ganesh chaitanya
  • 638
  • 6
  • 18