What does window['someKeyword'] mean in JavaScript? [duplicate] - javascript

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
What does window['someKeyword'] mean in JavaScript?
I guess the window is the DOM window object, but the syntax to retrieve a property is window.propertyName in the reference I found, not window['propertyName']. Are these two ways to retrieve the property?

To access a field of an object in Javascript there are two ways.
let's this is an object
obj = {name:'Object1'};
You can access name field by two ways
obj.name // returns 'Object1'
obj['name'] //returns 'Object1'
besically [] this method is useful when you have to access a field using a variable like bellow
var obj = {name:'Object1'}
var field = 'name'
obj[field] //returns 'Object1' because it will put value of field in `[]`

Related

Creaating a javascript object with computed name [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 6 years ago.
how would I make an object in javascript with a computed name.
Context:
I am making an add-on that will sit in the browser, log each hostname visited, create an object named after each one. I'm replacing "." with "_"
So for example on this site it would create a stackoverflow_com object.
is this possible?
another example would be
var 1+1 (with the variable actually being 2)
I know brackets make this possible with properies but I don't know how to do it with the name itself.
You need to store them either in a global object (like window) or preferably an object of your choosing
var mySites = {};
mySites["stackoverflow_com"] = "foo"; // access as mySites.stackoverflow_com or mySites["stackoverflow_com"]
mySites[1+1] = "bar"; // access as mySites[2];
More info: JavaScript property access: dot notation vs. brackets?

Use string as object name in Javascript [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
Is it possible to access the property of a javascript object using a string?
example: I have a javascript object "obj" that contains a property 'index'.
obj.index = 4;
Now let's say I have a string whose value is the same as a property name of my object:
var str = "index";
Can I use the value of the str variable to access the 'index' property of 'obj'?
Just use the following code:
obj[str]
You can use array Notation (squire bracket notation )
obj[str]

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".

How to access a dynamic property: objectName.{variable} [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
jquery dynamic id
(4 answers)
Closed 8 years ago.
i need to access
objectName.myID
but the "myID" part is dynamically generated..
how do i do this?
i tried
this['objectName.'+ variable]
i'd hate to use eval...
ps
this happens in a function (local scope) by the way..
You can access Object properties in two ways:
o.propertyname
//or
o.["propertyname"]
When using the bracket notation you have to put the propertyname in quotes or else it will be interpreted as a variable name (which in your case is exactly what you want). So in your case where you have stored the name of the property as a string, the way to go would be:
var variable = "propertyname";
o[variable];
/* /\ variable is replace with it's string representation "propertyname" */
You can even call methods this way:
var o = {};
var functionname = 'toString';
o[functionname]();
You can mix both notations, your example would look like:
var obj = 'objectName';
var prop = 'myID';
this[obj][prop]
// or this is possible too:
this.objectName[prop]
Assuming propertyName is the name of a variable holding the name of the property, for example 'myId', then you can use.
objectName[propertyName]
More details in the MDN : Working with objects

How to refer to object fields with a variable? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
Let's asume that I have an object:
var obj = {"A":"a", "B":"b", "x":"y", "a":"b"}
When I want to refer to "A" I just write obj.A
How to do it when I have key in a variable, i.e.:
var key = "A";
Is there any function that returns a value or null (if key isn't in the object)?
Use bracket notation, like this:
var key = "A";
var value = json[key];
In JavaScript these two are equivalent:
object.Property
object["Property"];
And just to be clear, this isn't JSON specific, JSON is just a specific subset of object notation...this works on any JavaScript object. The result will be undefined if it's not in the object, you can try all of this here.
How about:
json[key]
Try:
json.hasOwnProperty(key)
for the second part of your question (see Checking if a key exists in a JavaScript object?)

Categories