JS Array called "name" behaves strangely [duplicate] - javascript

This question already has answers here:
Is variable called "name" always defined in Javascript?
(2 answers)
Closed 6 years ago.
I played around a bit and created a javascript array containing some strings. When I tried to access the array it behaves quite strangely. The array is created correctly and works as expected if it is called something else.
var name = ["foo", "bar"];
alert(name); // shows "foo,bar"
Why is the array converted into a string, if the variable name is name? According to the standard (which the linked website is based on) it should be a valid variable name: https://mothereff.in/js-variables#name

If you execute javascript in a browser environment, the code is executed in the window context. This context already has some global variables set. One of those is the window.name which is a string. When the variable is set, the browser will automatically cast the new value to string which causes the strange behavior.
So even though name is a valid variable name, it should not be used in the global context if you are executing your javascript in the browser (It should work fine in e.g. node.js).

Related

For data-type "Number" num.[[PrimitiveValue]] is giving error. Why? [duplicate]

This question already has answers here:
How can I directly access [[PrimitiveValue]] value in JavaScript [closed]
(2 answers)
What is an "internal slot" of an object in JavaScript?
(2 answers)
What do double brackets mean in javascript and how to access them
(7 answers)
Closed 2 years ago.
In JavaScript, Object Class is the center of all happenings regarding data-type. One of the children of Object Class is Number Class. On making or creating a variable of type number its __proto__ will point to Number Class which then (via Prototypal Inheritance) makes a set of properties readily available to be used (eg: toExponential, toFixed, toString, etc.)
When I went through the whole list, I found at the end 2 more properties -- __proto__ and [[PrimitiveValue]] Now, just like the other properties, these 2 must also be readily avialable. So, I tried ... Now, to my dissapointment out of all these properties, the last one [[PrimitiveValue]] is giving error on Usage! Now, my surprise is, if via prototypal_Inheritance all the other properties are available and give some result, then what the heck is with this last property? I am simply tring to access it and ideally it should retrun me 0. That's all, so why the error???
Snippet:

Referencing an internal variable in a JS object [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 5 years ago.
In a simple JS object like this:
var LeadserData = {
agent_id: 2,
object_queries: {
emails: {
url: "/manual_emails/",
method: 'GET',
send_data: {
the_id: this.agent_id
}
}
}
}
it is obviously possible to access the agent_id simply like this:
LeadserData.agent_id = 100;
alert(LeadserData.agent_id);
This obviously returns 100. But why does not the internal reference of this.agent_id work?
alert((LeadserData.object_queries.emails.send_data.the_id));
I was expecting this to come out as "100" as well, but instead it is undefined. The whole fiddle is here: https://jsfiddle.net/h88zc5nw/1/
What is the explanation for this behaviour, and how can I tweak this to return the expected 100.
First of all, you are using an object literal. You can't use this inside an object literal, at least it won't be what you think it is. It will be whatever this available while the object literal is constructed.
Second, even if we assume that the above works, numbers are native types, they won't be shared via their references. So changing LeadserData.agent_id won't affect LeadserData.object_queries.emails.send_data.the_id even if you have assigned one to the other. Native types are copied on assignment, not passed around using a reference (like you would do with an object).

JS turn string into variable [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 5 years ago.
I have a json feed that returns an array of google map points. The issue is that it returns everything as a string. I send icon to a google map script. In the example below you will see SQUARE_PIN. This is a variable not a string and it adds the quotes around it preventing it from rendering the variable. Is there a easy way of fixing this.
{
"title":false,
"lat":"44.7930232",
"lng":"-89.7031784",
"icon":{
"path":"SQUARE_PIN",
"fillColor":"#FF0000",
"fillOpacity":1,
"strokeColor":"",
"strokeWeight":0,
"micon":"<\/span>"
}
}
Let's suppose you have your JSON stored in an object called obj. Also, let's suppose that the variable you intend to use is inside another object, called obj2 (could be window if the variable is global). In this case you can do this:
obj.icon.path = obj2[obj.icon.path];
and then use obj.

Why does Javascript cast null to string at variable assignment? [duplicate]

This question already has answers here:
Using the variable "name" doesn't work with a JS object
(4 answers)
Closed 6 years ago.
In Firefox 45 on OSX, when I fetch an item from localStorage from a key that does not exist, the function call returns null. I tested this in the console.
If I instead assign the call result to a variable, and print its value in the console, I get "null", i.e. a string.
Why does a variable assignment of a previously not defined variable cast a call result to a String?
Used code (in the console):
localStorage.getItem("non-existing-key"); // returns null
var x = localStorage.getItem("non-existing-key");
x // returns "null"
Edit: both versions seem to behave correctly on Chrome 50.0.2661.86 on OSX (both return null)
Edit2: my mistake. I used another variable name in my tests (specifically: var name). Now, if I let the console return the value of the variable name, it returns window.name, which is a property of window of the type String, defaulting to "null". So, it's not an assignment that causes a cast, but instead its that I got a String property defined by window.
I made a mistake. The specific code I used was the following:
var name = localStorage.getItem("non-existing-key");
name
Now, getItem does return null and not a String. What then happens is that by letting the console print the value of name it does in fact get window.name (see window.name on MDN), which by default is "null" (a String).

Javascript. Swap two variables. How it works? [duplicate]

This question already has answers here:
javascript variable swapping using arrays
(3 answers)
Closed 8 years ago.
Saw in a single source next:
[param_o,param_got] = [param_got,param_o];
This code swap variables param_o & param_got.But how [param_o,param_got] = [param_got,param_o] works, if [] is new instance of Array in Javascript ?
EDIT
Try checking:
var param_o = 1;
var param_got = 2;
[param_o,param_got] = [param_got,param_o];
console.log(param_o+" "+param_got);
// 2 1
This notation is called destructuring assignment and is part of Javascript 1.7:
Destructuring assignment makes it possible to extract data from arrays
or objects using a syntax that mirrors the construction of array and
object literals.
The object and array literal expressions provide an easy way to create
ad-hoc packages of data. Once you've created these packages of data,
you can use them any way you want to. You can even return them from
functions.
One particularly useful thing you can do with destructuring assignment
is to read an entire structure in a single statement.
The first sample actually demonstrates explicitly that this is useful to avoid temporary variables as in your code sample.
Firefox has supported this feature since Firefox 2 already. For Chrome the bug is still open after 3 years. IE11 doesn't support it either from what I've just tested.

Categories

Resources