This question already has answers here:
How does variable assignment work in JavaScript?
(7 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
Consider the Two Snippets
const sayMyName = wizard => {
wizard.name = 'Shazam';
}
let boy = { name: 'Billy' };
sayMyName(boy);
console.log(boy.name);
Since We know that objects in js are passed by reference hence a reference to boy object is assigned a property with value 'Shazam'. Since Object at the stored Reference is changed hence boy's name is changed to 'Shazam'.
const sayMyName = wizard => {
wizard = { name: 'Shazam' };
}
let boy = { name: 'Billy' };
sayMyName(boy);
console.log(boy.name);
Considering above case Here When Boy is passed in sayMyName function why it is behaving as pass by value and boy.name still return 'billy' ?
Related
This question already has answers here:
Modifying a copy of a JavaScript object is causing the original object to change
(13 answers)
Why can I change a constant object in javascript
(12 answers)
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 12 months ago.
I created an object const with a data and I made other const from the first. But my problem is when I change the second object, my first const also is changed.
Have a method to resolve this?
const user1 = {
name: "samuel"
}
const user2 = user1;
user2.name = "guedes";
console.log(user1); //output: "guedes"
console.log(user2); //output: "guedes"
Yes, you can use es6
const user1 = {
name: "samuel"
}
const user2 = {...user1};
user2.name = "guedes";
console.log(user1); //output: "guedes"
console.log(user2); //output: "guedes"
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 4 years ago.
I would like to know why this example shows the following behavior. If we write:
let person = {
name: 'ENoy',
age: 25,
weekendAlarm: 'No alarms needed',
weekAlarm: 'Alarm set to 7AM',
sayHello: () => {
return `Hello, my name is ${this.name}`;
},
sayGoodbye(){
return 'Goodbye!';
}
};
console.log(person.sayHello());
let friend = {
name: 'Bruno'
};
friend.sayHello = person.sayHello;
console.log(friend.sayHello());
We get as output:
Hello, my name is undefined
Hello, my name is undefined
And if we try to use:
let person = {
name: 'ENoy',
age: 25,
weekendAlarm: 'No alarms needed',
weekAlarm: 'Alarm set to 7AM',
sayHello: () => {
return `Hello, my name is ${person.name}`;
},
sayGoodbye(){
return 'Goodbye!';
}
};
console.log(person.sayHello());
let friend = {
name: 'Tori'
};
friend.sayHello = person.sayHello;
console.log(friend.sayHello());
We see:
Hello, my name is ENoy
Hello, my name is ENoy
In Java I have seen various ways to get it done:
Java - get the current class name?
In JavaScript I have seen the constructor:
Get name of object or class
But I think in the previous link we get the method which instantiated the object we would like in this topic get the variable from the object where the function is declared.
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I need an object in Typescript declared like this:
queryParameters = { flagged: true };
Now I would like to have the flagged to be retrieved from a variable name. Something like:
var param = 'flagged';
queryParameters = { ValueOf(param): true };
Any idea ?
Thanks.
Why not use computed property names:
queryParameters = { [param]: true };
This question already has answers here:
beginner's: const definition in Redux confusing
(1 answer)
What does curly brackets in the `var { ... } = ...` statements do?
(4 answers)
Closed 5 years ago.
What is this code convention in javascript?
const { navigate } = //whatever
As in, what sense does it make. I saw it in RNs React navigation
https://reactnavigation.org/docs/intro/
It's named destructuring. When you have an object and you want to take only a property of that object, you can get only it by using that convention.
let fullName = {
first: 'John',
last: 'Smith'
}
const { first } = fullName;
You can take a look here for more info's
http://wesbos.com/destructuring-renaming/
It's called destructuring
Example:
var myObject = {a: "what", b: "ever"};
const {a} = myObject;
console.log(a); // will give "what"
This question already has answers here:
JavaScript - Why can't I call a variable "name"?
(2 answers)
Closed 5 years ago.
My question is pretty simple. I'm creating two objects. The second object is referencing an object inside the first object.
var me = {
name: {
first: "justin"
}
};
var name = me.name;
console.log(me.name.first); // "justin"
console.log(name.first); // undefined
Why am I getting undefined in my second console log? Shouldn't I get "justin" instead?
You need to use another name. There is a name variable which is global.
var me = {
name: {
first: "justin"
}
};
var anotherName = me.name;
console.log(me.name.first);
console.log(anotherName.first);