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
Related
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=$_;
everyone:
as we all know, getting a nonexistent value from Object will got a "undefined", is there any way to throw an error instead of return a undefined?
for instance:
var a= {example:"example"};
//do something here
a.b //error throw instead of "undefined"
thx very much for helping.
Object.define property has a way to specify the get and set methods
look at the link below
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
Object class has a method "hasOwnProperty" which could also be used to check if a property exists or not before trying to access it
ES6 comes with Proxy Objects which will help you overload the dot operator. What this means is that you can invoke a function when you retrieve a property. This will allow you to check if the property is defined and if it isn't "throw" an error. Hope this helps. More info here
Looking at the above example, if you are you trying to initialize the property "b" on the object a with a value after "a" has been defined, then you could just as easily do the following:
var a= {example:"example"};
//do something here
a.b = 'some value';
a.b //now an error is not throw
Most Objects can be re-defined even after they have been initialized. Properties and methods can be added and removed once objects have been initialized.
I say most, because it is possible to prevent objects from being extended/altered (using Object.preventExtension method).
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/preventExtensions
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;
}
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.
If a is undefined, this works:
if(window.a) {}
while this throws an error:
if(a)
Can someone explain why?
window.a is a property of window and it's undefined. a is a variable, and it's undeclared.
To use a variable, you should first declare it using the var statement. Since you didn't declare a, the interpreter raises an error. Object properties are not needed to be explicitly declared in order to use them. Crockford writes in The Good Parts:
If you attempt to extract a value from
an object, and if the object does not
have a member with that name, it
returns the undefined value instead.