This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
I have found some sourcecode on a javascript project but couldn't understand a part of code that looks like this:
keystate= {};
document.addEventListener("keydown", function(event) {
**keystate[event.keyCode] = true;**
});
document.addEventListener("keyup", function(event) {
delete keystate[event.keyCode]
});
The first problem I do not understand is the brackets that come after declaring of the object and then especially what the "= true" means? And a more ambigous question would be: is this a part of OOP (object oriented programming)?
The square brackets just allows you to access a property by having its name in a string (rather than in an identifier as you would use in dot notation).
The = is an assignment operator.
true is a boolean literal.
These are all equivalent.
foo.bar = "something";
foo["bar"] = "something";
var property = "bar"; foo[property] = "something";
Related
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 3 years ago.
I cannot understand the following code, var formData = {}; I guess defines a object "formData", but why to save each element in the formdata["fullName"]? What is this with []? Isn't it used for array? i confused. Could somebody explain this code? Thank you!
function readFormData(){
var formData = {};
formdata["fullName"] = document.getElementById("fullName").value;
formdata["empID"] = document.getElementById("empID").value;
formdata["salary"] = document.getElementById("salary").value;
formdata["city"] = document.getElementById("city").value;
return formData;
}
In javascript, array keys are defined and referenced with square brackets. Object properties can be defined an accessed the same way or with dot notation.
In your case you do have an object, and it's properties can be accessed using bracket notation.
The following two lines are thus equal:
obj["property"] = value;
obj.property = value;
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 6 years ago.
What is the difference between referencing an object with object.key = value and object['key'] = value For example if I had an object call var myObj = {};
I believe the quote notation allows for arbitrary keys, (like console.log({' 1a':3}[' 1a']), whereas the dot syntax can only be used with keys that begin with a letter.
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
var stooge = {
"first-name": "Jerome",
"last-name": "Howard"
};
var properties = [
'first-name',
'middle-name',
'last-name',
'profession'];
for (i = 0; i < properties.length; i++) {
console.log(properties[i] + ': ' + stooge[properties[i]]);
}
I don't understand stooge[properties[i]]. Why are we using bracket before properties?
Can someone explain when to use brackets?
I dont understand stooge[properties[i]].
It means more or less literally what it says.
If i is 0, then properties[i] is properties[0], which is set to 'first-name'.
Therefore, stooge[properties[i]] is stooge[properties[0]] is stooge['first-name'] is "Jerome".
EDIT
As someone pointed out, you cannot use dot-notation here. The name of the property is first-name. If you typed stooge.first-name, the parser would interpret that as stooge.first - name. undefined minus undefined is... NaN!
This is just a way to access properties dynamically from an object. In this cause since there is an array of strings, it lets you get the value from the object. Since you can't do something like obj.'some-string'
This might help JavaScript property access: dot notation vs. brackets?
Object properties can be accessed with a .. or with [].
But dot notation won't work here.
because if you switch stooge[properties[i]] to stooge.properties[i] it would return undefined, because the stooge object doesn't have a member named properties.
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 8 years ago.
I never thought I'd stumble in a problem like that but hey what do you know..
var prop = 'horses'
console.log({
prop: 1
});
How can I have this produce an object with a property horses instead of prop and possibly have it done in a single line? Because I can think of a solution but what I'm really looking for is a one-liner.
You can use bracketed notation:
var obj = {};
obj[prop] = 1;
console.log(obj);
In JavaScript, you can refer to a property either by dot notation and a literal name (foo.bar), or bracketed notation with a string name (foo["bar"]). In the latter case, the string can be the result of any expression, including (in your case) a variable reference.
...but what I'm really looking for is a one-liner.
There's no way to do that as part of an object initializer, though, you have to do it separately. If you want a one-liner, you'll need to do a function call, e.g.:
console.log(makeObj(prop, 1));
where makeObj is
function makeObj(propName, propValue) {
var obj = {};
obj[propName] = propValue;
return obj;
}
Try using JSON to create the object:
console.log(JSON.parse("{\"" + prop + "\":\"1\"}");
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
I was reading here on stack overflow that these are not equal. So what is the difference.
What happened is that in 2nd case value was assigned as the property of myObj. So if b='abc';
then myObj.abc was now available.
I had always thought same thing but [] version was used when name were weird ones.
Dot notation takes an identifier that is the property name. The square bracket notation accepts a string representation of the property name.
Given var a = "a"; then myObj.a = b and myObj[a] = b and myObj["a"] = b are equivalent.
The difference between myObj.a=b and myObj[a]=b is that in the first case you are accessing an attribute called a in the object. In the second you are accessing an attribute whose name is in a variable called a.
On the other hand, myObj.a=b and myObj["a"]=b would be equivalent.
a lot, results would depends on a var value. but ["a"] would be the same as .a