Using the Variable "Name" Doesn't Work With a Js Object

Using the variable name doesn't work with a JS object

window.name has a special purpose, and is supposed to be a string. Chrome seems to explicitly cast it to a string, so var name = {}; actually ends up giving the global variable name (i.e. window.name) a value of "[object Object]". Since it's a primitive, properties (name.FirstName) won't "stick."

To get around this issue, don't use name as a global variable.

Why can't I use 'name' as a variable/object name?

This is happening because name is a existing property on window, that behaves a little different from normal variables.

You can't replace this property, you can only assign string values to it. When assigning other types to the name property, it gets cast to string:

name = false;
var name = false;
window.name = false;

These lines will all result in window having a property name that contains "false".

In a similar fashion, objects and functions saved to the name variable will be cast to string:

var name = function(){};  // "function (){}"
var name = { a: [1, 2] }; // "[object Object]"

If you want to use a variable named "name", you'll have to enclose it in a scope:

// In the global namespace, this will alert `"[object Object]"`

var name = { a: 1};

alert('Global `name`: \n' +

JSON.stringify(name));

// In it's own namespace, this will alert `{"a":1}`.

(function(){

var name = { a: 1};

alert('Namespaced `name`: \n' +

JSON.stringify(name));

})()

Javascript use variable as object name

Global:

myObject = { value: 0 };
anObjectName = "myObject";
this[anObjectName].value++;

console.log(this[anObjectName]);

Global: v2

var anObjectName = "myObject";
this[anObjectName] = "myvalue"

console.log(myObject)

Local: v1

(function() {
var scope = this;

if (scope != arguments.callee) {
arguments.callee.call(arguments.callee);
return false;
}

scope.myObject = { value: 0 };
scope.anObjectName = "myObject";
scope[scope.anObjectName].value++;

console.log(scope.myObject.value);
})();

Local: v2

(function() {  
var scope = this;

scope.myObject = { value: 0 };
scope.anObjectName = "myObject";
scope[scope.anObjectName].value++;

console.log(scope.myObject.value);
}).call({});

Why JS code doesn't work when we use variable as an object key, and then use destructuring (without alias) to get value of this object key?

One of the issues with the syntax of:

const { [NAME_FIELD] } = state;

... is that it assumes that the value held in NAME_FIELD is a valid identifier which is not always the case. In the case of NAME_FIELD = "name", name is a valid identifier, so it's easy to assume that NAME_FIELD can be made into a valid variable, however, this is not always the case. Consider if you had:

const NAME_FIELD = "my-key";
const { [NAME_FIELD] } = state; // which would be interpreted as { my-key }

this wouldn't work as it would mean creating a variable with a hyphen -, which isn't valid. Similar issues occur with other valid object keys that aren't considered valid identifiers, such as "123", "my key", and symbols.

As a result, an alias needs to be used to assign the destructured value to a valid identifier.

What's wrong with the variable name in JS?

window.name Gets/sets the name of the window.

string = window.name;
window.name = string;

The name of the window is used primarily for setting targets for hyperlinks and forms. Windows do not need to have names.

It has also been used in some frameworks for providing cross-domain messaging (e.g., SessionVars and Dojo's dojox.io.windowName) as a more secure alternative to JSONP. Modern web applications hosting sensitive data should however not rely on window.name for cross-domain messaging but instead rather utilize the postMessage API.

Don't set the value to something unstring since its get method will call the toString method.

is it possible to get values by using object.variable name? not object.property?

In your case cells is one of 'A1', 'B1', 'C1' etc.

What you can do with JavaScript Objects is

object['property']

so please try

this.style(ws[cells])

Unexpected typeof value for number variable in JavaScript

If you were running this in a browser, then I think it's because the default execution context is the window object. Basically, every global value you declare becomes a property of the window object, and vice versa: every property of the window object is available as a global variable (e.g console). Window objects have a name property by default, and redeclaring it as a variable doesn't affect that. Anyway, that's the closest I can get to an explanation.

Why variable name is typed as string?

name it's a property of global object window and you cant replace it.

But you can delete and then define again

delete window.name;

window.name = ()=>{console.log('ok')}

But i don't recommend change global properties.

In node environment all work good:

Sample Image



Related Topics



Leave a reply



Submit