Questions regarding Objects in javascript [duplicate] - javascript

This question already has answers here:
Keyword 'const' does not make the value immutable. What does it mean?
(5 answers)
Why can I change a constant object in javascript
(12 answers)
Closed last year.
I'm using Code Academy to learn basic javascript. I had a problem when dealing in assigning objects with const.
The tutorial says:
It’s important to know that although we can’t reassign an object declared with const, we can still mutate it, meaning we can add new properties and change the properties that are there.
const spaceship = {type: 'shuttle'};
spaceship = {type: 'alien'}; // TypeError: Assignment to constant variable.
spaceship.type = 'alien'; // Changes the value of the type property
spaceship.speed = 'Mach 5'; // Creates a new key of 'speed' with a value of 'Mach 5'
Why I can't change the value of the type property with the code in line 2??
My guess on this: Since we couldn't reassign any value declared by const, we still can change the value of type property using bracket notation or dot notation. Is it right??
Thanks

Related

Why javascript indexOf method give me wrong output [duplicate]

This question already has answers here:
Using the variable "name" doesn't work with a JS object
(4 answers)
When using object destructuring assignment, why is a property "name" cast to string? [duplicate]
(1 answer)
Closed 1 year ago.
If i us "var" to declare array given as below. Why give me 6
var name = ['Rohan', 'Google', 'Student'];
console.log(name.indexOf('Google'));
Otput is 6
If i use "let" to declare array give me right answer why
let name = ['Rohan', 'Google', 'Student'];
console.log(name.indexOf('Google'));
Otput is 1
Instead of naming your variable as name change it to some other name and you will get a right result.
var myArray = ['Rohan', 'Google', 'Student'];
console.log(myArray.indexOf('Google'));
The reason is there is some global variable called name whose result is being shown (If you check developer console and search window.name you can know what that variable is).
var has global scope in this case pointing to window object.
And in case of let name, we should know that let has local scope within the context so this time name points to your array not global name. Infact ES6 introduced let, const to avoid global spacing issue that we were facing with var

What does the "var" before a JavaScript array initialization refer to, the elements of the list or the array itself? [duplicate]

This question already has answers here:
What does "var" do in JavaScript? Why is it sometimes part of an assignment? [duplicate]
(3 answers)
Declaring javascript variables as specific types [closed]
(1 answer)
Closed 4 years ago.
I was taught to use "let" to initialize arrays in JavaScript, but I've recently discovered that "var" can be used as well.
var evenNumbers=[2,4,6,8];
I know that it's possible to initialize a function in JavaScript using var, as in
var hello=function(){};
so naturally, I assumed that the "var" being used to initialize the variable refers to the name of the array, in this case evenNumbers.
However I also recently learned that to initialize arrays in C, which I think of as the grandfather of Java-type languages, the type of variable used in the array is used to initialize the array call.
int evenNumbers[]={2,4,6,8};
Obviously in this case, int refers to the elements of the list, since an array is not an int.
I therefore assumed that var before an array call in JavaScript refers to the elements of the list. I tried to test it by applying the wrong strong type to a new JavaScript variable, like
int newYearsResolutions=["Stop procrastinating"];
Which gives me an unexpected identifier, but that's not too helpful since an array is not an int nor is "Stop procrastinating" an int. I then tried
int evenNumbers=[2,4];
and this gives me the same error, leading me back to my original conclusion that the var being named here is "evenNumbers" and not the ints 2 and 4, but I still feel like I might be missing something.
So, var evenNumbers=[2,4] appears to name the evenNumbers variable and not the elements of the array. I just want to double-check that that's the case.
JavaScript is not a typed language. int isn't a reserved word, and thus is not doing anything. var, let, const are all ways to assign variables - regardless of type.

If a string is not an object, how am I able to use built-in methods? [duplicate]

This question already has answers here:
How is a Javascript string not an object?
(2 answers)
Object and String prototypes are not prototypes of defined strings?
(3 answers)
Closed 4 years ago.
When you create an object in JavaScript, you inherit properties from the Object prototype you can utilize.
But a string is a primitive type, thus there is no prototype. So how am I able to use methods such as substr() and repeat() on a string when there is no prototype the string can inherit those methods from?
For example, when I create a new array and assign it to a variable, I type the variable name into the console and the Array prototype is listed where I have access to methods I can use. But if I create a string and assign the string to a variable, then I type the variable into the console, there is no prototype attached to it.
Does that make sense?
When you access a property on a primitive string, number, or other primitive, the JavaScript engine acts as though you've converted it to the equivalent object and then looks for the property. So for instance:
var str = "hi";
var upper = str.toUpperCase();
The JavaScript engine acts as though that code were written like this (for the purposes of accessing the toUpperCase property):
var str = "hi";
var upper = new String(str).toUpperCase();
Before ES5, the spec said that the JavaScript engine actually did create a string object there and then call the property. As of ES5 that changed slightly (because in strict mode it became possible for this to have a non-object value), but in essence the concept is still the same.

Do 'variables' have properties? [duplicate]

This question already has answers here:
Why can't I add properties to a string object in javascript?
(2 answers)
Closed 9 years ago.
Do variables have properties?
The obvious answer should be NO. If I try to assign a property to a variable, it should give an error. Right?
If I do something like:
var someVariable = 'Cat';
someVariable.eyes = 'two'; //Gives no error!
alert(someVariable.eyes); // alerts 'undefined' instead of giving an error!
Variables don't have properties, but their values do. (If the value's an object, anyway.)
In this case, you're trying to set the eyes property of the string currently referenced by someVariable.
It won't work in this case, though. Since primitive values don't have properties, JS will convert the primitive string value into an object and set the property on that object, which pretty much immediately gets silently discarded. The end result: the primitive string remains unmodified.
"Variables" don't actually exist (except strictly within the definition of a scope), only objects. And string objects can't have arbitrary properties assigned by default.

Adding custom variables to an object [duplicate]

This question already has answers here:
How to convert string as object's field name in javascript
(5 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
Suppose I have an object obj which has some variables in it. What I want to do is to add a new variable which has a name i gave as a parameter.
For example:
var obj=
{
number:8
}
function AddField(field_name,obj)
{
//some magical code here
}
If I call
AddField('name',obj);
i want to be able to do
obj.name='Apple'
after calling this function.
Any ideas?
Use square brackets notation:
obj[field_name] = value;
REF: http://www.jibbering.com/faq/faq_notes/square_brackets.html#vId
Use the bracket notation:
obj[field_name] = void 0;
This creates a new property with an undefined value.
If obj is already defined, you can just do obj.field_name = null or obj["field_name"] = null.
I wouldn't set any value to undefined even though the language allows for that, simply for a semantic issue: undefined means "this property/variable has not been defined" and null means "this property/variable has no value".

Categories