Is there any syntax to do something like the following
var obj = {a: this, b: this}
and get
obj = {a: 'a', b: 'b'}
If you want to create an object that has a bunch of keys and values equaling the same string, it's as simple as:
var obj = {};
['a','b'].forEach(function(item) {
obj[item] = item;
});
['a','b','c'].forEach(function(c) { Console.log(c); }); into node.js. It worked.Yes! I'm not sure why you would use but here it is:
var obj = { a: 'you', b: 'they'};
var newObj = {};
for (var key in obj) {
newObj[key] = key
};
//newObj = {a: "a", b: "b"}
You can do this:
var obj = {a: "this is A", b: "this is B"}
obj.a equals to "this is A"
obj.b equals to "this is B"
A quick snippet using this object example to add the pointer locations stored in an object:
function getMousePosition(event){
var mouse = {x: 0, y: 0};
mouse.x = event.clientX;
mouse.y = event.clientY;
Elem = document.getElementById("Elem");
Elem.innerHTML = "Simple Mouse Position by Khriztian Azuaje.<br><br>" + "X pos: " + mouse.x + ", Y pos: " + mouse.y;
}
#Elem {
background:black;
text-align: center;
color:white;
padding:2px;
display: flex;
align-items: center;
justify-content: center;
height:160px;
}
<body onmousemove="getMousePosition(event)">
<div id="Elem"></div>
</body>
obj to {a: this, b: this}? Writing var obj = {} would give you identical results in the end