Cover image for Spotify
Logo

Spotify

Actieve werkgever

Spotify

Een sollicitatiegesprek toevoegen

Sollicitatievraag

Sollicitatiegesprek voor de functie Javascript Engineer

-

Spotify

In Javascript, how would you make a variable read-only?

Antwoorden op sollicitatievragen

6 antwoorden

5

declaring a variable using const keyword. This declares a read-only named constant

Anoniem op

2

There are few solutions, one is to use revealing modular pattern where you return function that only retrieves value of property, and everything is wrapped in closure. Other solution is to use Object.defineProperty where you cane be specific and set write value of object property.

Marko Kovacevic op

0

There is no "const" keyword back in 2013 when the question was asked.

Anoniem op

1

var obj = {}; Object.freeze(obj); // Or when defining a property Object.defineProperty(obj, 'key', { writable: false, value: 'read-only variable' });

Before the `const` key word, freeze an object or define the object explicitly as read-only op

0

This looks so good: bit.ly/faang100

Anoniem op

2

create the variable using the var keyword, then create a function that returns its value

ilya op

Voeg antwoorden of opmerkingen toe

Meld u aan of registreer u om hier een opmerking over te maken.