Cant access object via variable? - javascript

I have a javascript object defined as follows:
var tabs = {
'nav_tweet_search':'tweet-panel',
'nav_shopping_cart':'cart-panel',
'nav_stats':'stats-panel'
};
Im trying to access some values using a variable in another function. However, i am getting undefined. I created some console logs ..
console.log("panel: "+panel);
console.log("tabs: "+tabs);
console.log("tabs.panel: "+tabs.panel);
Which outputs,
panel: nav_tweet_search
tabs: [object Object]
tabs.panel: undefined
Any reason why i wouldnt be able to access the obj using a variable? If i do "tabs.nav_tweet_search" it works

Use this:
console.log("tabs[panel]: " + tabs[panel]);
When you do tabs.panel the browser will look for a property called "panel", but only "nav_tweet_search", "nav_shopping_cart" and "nav_stats" are defined.
When using brackets the value of the variable will be used as the key for fetching the object property. If you use tabs.panel that's equivalent to tabs["panel"].

You seem to want
tabs[panel]
This gives the property with name panel of tabs.

Related

Simple JS AutoMock Possible?

Im not really getting into the details of WHY doing this as its needed.
Supose i have a simple object.
var obj = {};
Lets say i want to access a random property of that object
obj.randomProp
This is getting me undefined.
Now, would it be possible, for that obj.randomProp return me another empty object ( {} ) ?
Of course, with no property declaration as obj.randomProp=123. I was thinking on this to be dynamic, somehow like a default property getter as if everytime you try to get a property of this element, you will get a new empty object for it.
Is this possible in JS ?

How to store HTML Elements as global variable in the console

When I use chrome developper tools, for any variable that I logged to the console using console.log, I can right-click and select Store as global variable, except if I log a HTML Element.
I found a workaround: if using console.log([el]) instead of console.log(el), it is possible to save the array to a global variable and then use temp1[0] to access the element.
This seems somehow cumbersome. Is there another way to store a console.logged element in a global variable in the console ?
You can use $$(selector)
ƒ $$(selector, [startNode]) { [Command Line API] }
then use the procedure described at the Question.
$_ returns the last logged item in the console.
Beware, though, that it might return undefined if you use console.log(), because if you do console.log('hi!');, then it will also return undefined. To workaround:
/*just type value instead of console.log*/
'hello!';
var text=$_;
/*If you cannot do this, just log it in this way:*/
x.valueOf();
var xContent=$_;

javascript variables returns strange outputs

I'm new here and new to Javascript. I got a strange problem here when I output the values to the console. As far as I know In both these cases the name, and the color is the properties of object car to access them we need to use this.propertyName or object.propertyName, but when I output those values to the console without using this or object name, 1st console.log returns an empty string and the other one returns the uncaught reference error. are they pointing to the window object? then in both the cases, it should return a uncaught reference, can somebody here please clarify this.. thanks in advance. :)
var car = {
name : "ford",
color:"red",
log : function(){
console.log(name);
// outputs an empty string
console.log(color);
// Returns error (this.js:8 Uncaught ReferenceError: color is not defined)
}
}
car.log();
Try console.log(this.name) and console.log(this.color).
Additional Information from MDN
When a function is called as a method of an object, its this is set to
the object the method is called on.
In the following example, when o.f() is invoked, inside the function
this is bound to the o object.
Source: MDN
The reason you don't get a ReferenceError when outputting name is that browsers have a built-in global called name: It's the name of the current window. But they don't have a built-in global called color, so you get the error.
To access name and color on your object when you use car.log(), you'd use this.name and this.color.
yes you are correct both should have thrown uncaught reference but wait ....
actually there is a property on window which is .. yeah ... name
so actually you console that property of window.. ie window.name
.. second one is just correct .. uncaught reference

Strange behaviour with angularJS $scope.$parent

I'm have having some trouble accessing a value from the parent scope, and what appears to be some strange behaviour also.
If I log $scope.$parent to the console and inspect the DOM object there is a property topicDiscovery.name. However, when I try to log this ($scope.$parent.topicDiscovery.name) it returns undefined.
Also, when I try to log the topicDiscovery object it returns an empty array, even though when its not empty in the DOM for $scope.$parent.
Why is this?
This means $scope.$parent.topicDiscovery is getting changed after console.log. Google chrome doesnot print all json object when you do console.log. It just refer to current Object. Try using JSON.stringify($scope.$parent), Here you will not get this property as stringify converts that JS object to string and there will not be any memory linking between orignal object and string.
Here is best example.
var d={z:{b:{}}};
console.log(d); //When you check here, d.a_1 is available
console.log("d.a_1",d.a); //Here d.a_1 is not available.
//Here I am adding d.a_1 property
for(var i=0;i<10;i++){
d['a_'+i]=i;
}

Why is the property of JSON object not accessible?

I'm not able to access progress property of data object. I used debugger to stop the application and check if data object is defined. Here's the screenshot of the console and it completely makes no sense.
data is an Array. You want data[0].progress.
Data is an array, as evidenced by the fact that in the console it has brackets.
In order to access the object inside it, you need to use array index notation, like so:
data[0].progress //0

Categories

Resources