Chrome console object [duplicate] - javascript

This question already has answers here:
Why does JavaScript variable declaration at console results in "undefined" being printed?
(4 answers)
Closed 3 years ago.
When I write this in the console it's log undefined.
var obj = { first: 'lorem' };
But when I write this in the console it's log object.
var obj = { first: 'lorem' };
obj = { second: 'ipsum' };
Why is this happening?

The value you see being printed is the return value of the line of code you executed.
var obj = {first: 'lorem'};
returns undefined, whereas
obj = {second: 'ipsum'};
will return the object assigned to obj, hence {second: 'ipsum'} is printed.
This is why you can do things such as:
var a = b = 2;
Here the assignment of b = 2 will set b equal to 2, whilst also returning 2, thus setting a to 2

Anything you execute in the console, it will display the return value of it. When declaring and assigning variables, it will return undefined. When only assigning values to variables, that value is returned.

Related

Unexpected Javascript array behavior [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
I signed the variable bubbleL1 to 1, but why the first variable in the bubbles list, which is bubbleL1 still shows undefined? I know this maybe a stupid question, but I really don't know why.
let bubbleL1, bubbleL2, bubbleL3, bubbleR1, bubbleR2, bubbleR3;
let bubbles = [bubbleL1, bubbleL2, bubbleL3, bubbleR1, bubbleR2, bubbleR3];
bubbleL1 = 1;
console.log(bubbleL1) // 1
console.log(bubbles) // [undefined, undefined, undefined, undefined, undefined, undefined]
What I want is a list with a specific name for each item in it (for the declared reason, I really don't just use bubbles[0], bubbles[1]...)
Let's say we have a list named bubbles, and we also have six variables called bubbleL1, bubbleL2, bubbleL3, bubbleR1, bubbleR2, bubbleR3. I want to put all these six variables into the bubbles list, so later I can assign values to each variables that in the list, like this:
bubbles.forEach((bubble) => {
bubble = "something";
})
bubbleL1 is a primitive and therefore copied by value.
let x = 3;
let sum = x;
sum = 3 + 5;
console.log(x); // 3
Objects and arrays, on the other hand, will exhibit your expected copy-by-ref behavior:
let x = {a: 3};
let sum = x;
sum.a = 3 + 5;
console.log(x.a); // 8

Use object values as new values inside same object [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 8 years ago.
I am trying to do something like this:
var obj = {
a: 5,
b: this.a + 1
}
(instead of 5 there is a function which I don't want to execute twice that returns a number)
I can rewrite it to assign obj.b later from obj.a, but can I do it right away during declaration?
No. this in JavaScript does not work like you think it does. this in this case refers to the global object.
There are only 3 cases in which the value this gets set:
The Function Case
foo();
Here this will refer to the global object.
The Method Case
test.foo();
In this example this will refer to test.
The Constructor Case
new foo();
A function call that's preceded by the new keyword acts as a constructor. Inside the function this will refer to a newly
created Object.
Everywhere else, this refers to the global object.
There are several ways to accomplish this; this is what I would use:
function Obj() {
this.a = 5;
this.b = this.a + 1;
// return this; // commented out because this happens automatically
}
var o = new Obj();
o.b; // === 6
This should return the correct values:
function () {
var aVar = 5;
var bVar = aVar + 1;
return {
a : aVar,
b : bVar;
}
}();
As it turns out you can't reference an object inside another object unless the first one is a function. But you can do it this way.
var obj = {
a: 5
}
obj.b = obj.a + 1; // create field b in runtime and assign it's value
If you console.log(obj) you will have
obj = {
a: 5,
b: 6
}
This way you keep the object literal structure for the remaining part of the code
No, in your example, the value of this doesn't refer to the object literal.
You'll need to assign a value to b after the object has been created in order to base it on another property in obj.
in chrome debugger
> var o = {a: 5, b: this.a+1}
undefined
> o.b
NaN
> o.a
5
No. this will take the same meaning as it would outside the definition.

Is the below Javascript function being passed arguments by value or by reference? I'm not sure how to distinguish between the two [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 8 years ago.
I can't figure out what passing by reference and passing by value looks like (I know what they mean in theory). Can you tell me whether the below function is passing arguments by value ore reference? BTW this is my first post :)
var number_1 = 32;
var number_2 = 43;
function addition(num1,num2) {
alert (num1*num2) ;
}
addition(number_1,number_2); /*is this passing by reference?*/
Primitive types
In javascript number, strings, boolean fall under the category of primitives.
And whenever such types are passed as arguments to a function, a separate copy is created inside the function scope which has no impact on the outer scope
for e.g
var number_1 = 32;
var number_2 = 43;
addition(32,43);
//number_1 is 32 here
//number_2 is 43 here
function addition(number_1,number_2) {
number_1 += 1; // number_1 is 33
number_2 += 1; // number_2 is 44
}
Reference types
Reference types are slightly different
Take the following example
var obj = new Object();
fn(obj);
function fn(object){
object.property = "test";
//obj.property is "test"
object = new Object();
object.property = "test 2";
//obj.property is still "test"
//obj.property should have changed to "test 2",
//if it had been passed by reference
}
if it had been passed by reference , obj.property should have got changed to "test 2" after the last statement inside fn, but it didnt.
so when passing reference values to functions, a separate copy of the pointer to the object is passed.
In javascript every variable is passed by value not reference but there is an exception for objects. objects are references (like it was answered here).
which means a number, a string, a boolean or a function don't change at all after you put'em as argument in a function. but an the propertues and the elements inside an object or an array change if they are changed inside a function.
Here is an example
Simple variable
function nullify(a)
{
a = null;
}
var myVariable = 123;
nullify(myVariable); // myVariable still equals to 123
Object
function changeProperty (obj)
{
obj.key = null;
}
var myObject = new Object();
myObject.key = "value";
changeProperty(myObject); // the property key is null after calling the function
Array
function changeArray (Arr)
{
Arr[0] = null;
}
var myArray = [1,2,3];
changeArray(myArray); // now the value of myArray is [null,2,3]

Why does browser returns undefined? [duplicate]

This question already has answers here:
assignment in javascript and the var keyword
(4 answers)
Closed 8 years ago.
I have found a phenomenon that confused me when I run simple javascript code from the browser console(Chrome & Firefox).
When I typed say
>var a = "a"
The browser will return a string
>"undefined"
but if I just typed
>a = "a"
The browser will return the string
>"a"
Why is this case?
If you write
alert(var a = 'a')
You get a syntax error, var is part of the javascript syntax, it doesn't return anything.
The a = 'a' portion, however, does return something.
You can do var a = b = c = d = 'e';
And the d = 'e' returns e, which gets fed into the c=d which is really c = 'e', etc. Once you get to the var it stops returning the value.
If you type var a; you get undefined. var a = 'b' is essentially just shorthand for var a; a = b;
The console is showing the result of the evaluated expression. Declaring the variable and assigning it at the same time with
var a = 'a'
does not return anything, so you get undefined. The result of just the assignment part
b = 'b'
returns the value, so you see it in the console.
var a = b = c = d = 'foo';
That returns undefined, although several variables have been set. The real purpose of the expression was to define the scope of the variables, setting the value was just a bit of shorthand.

Can I reference other properties during object declaration in JavaScript? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 8 years ago.
I am trying to do something like this:
var obj = {
a: 5,
b: this.a + 1
}
(instead of 5 there is a function which I don't want to execute twice that returns a number)
I can rewrite it to assign obj.b later from obj.a, but can I do it right away during declaration?
No. this in JavaScript does not work like you think it does. this in this case refers to the global object.
There are only 3 cases in which the value this gets set:
The Function Case
foo();
Here this will refer to the global object.
The Method Case
test.foo();
In this example this will refer to test.
The Constructor Case
new foo();
A function call that's preceded by the new keyword acts as a constructor. Inside the function this will refer to a newly
created Object.
Everywhere else, this refers to the global object.
There are several ways to accomplish this; this is what I would use:
function Obj() {
this.a = 5;
this.b = this.a + 1;
// return this; // commented out because this happens automatically
}
var o = new Obj();
o.b; // === 6
This should return the correct values:
function () {
var aVar = 5;
var bVar = aVar + 1;
return {
a : aVar,
b : bVar;
}
}();
As it turns out you can't reference an object inside another object unless the first one is a function. But you can do it this way.
var obj = {
a: 5
}
obj.b = obj.a + 1; // create field b in runtime and assign it's value
If you console.log(obj) you will have
obj = {
a: 5,
b: 6
}
This way you keep the object literal structure for the remaining part of the code
No, in your example, the value of this doesn't refer to the object literal.
You'll need to assign a value to b after the object has been created in order to base it on another property in obj.
in chrome debugger
> var o = {a: 5, b: this.a+1}
undefined
> o.b
NaN
> o.a
5
No. this will take the same meaning as it would outside the definition.

Categories

Resources