Javascript one variable is not accessible - javascript

I have variable "a" and variable "cm", both declared in the same scope.
But inside each loop, variable a is accessible, while cm is not accessible:
var a = [];
var sm = grid.getSelectionModel();
var cm = grid.getColumnModel();
Ext.each(sm.getSelections(), function (item, index) {
var s = 'test';
a.push(s); //it works, a is accessible
var colHeader = cm.getColumnHeader(index); //Uncaught ReferenceError: cm is not defined
});
why? How can I access cm inside each loop?
This is documentation of grid.getColumnModel:
ExtJsGrid

cm should be accessible in the code that you gave. You can verify that by doing console.log(cm); right before you get the error.
The issue is most likely that the getColumnHeader function doesn't exist on cm.

Related

how to calculate sum, multiply, modulus of two global variables and two local variables?

I am quite confused to understand the logic in my question as i"m new to javascript but i tried to write some logic mentioned below but unable to understand what logic to write exactly to get the result. If someone can please help me in rectifying my code and help me understand to get the result.
JS
// Global Variable
var a = 40;
var b = 70;
function var_ops_5() {
// Local Variable
var a = 4;
var b = 7;
var c = a + b;
var d = a * b;
var e = a % c;
};
Using var is the problem. You can't have var = 40 and then have var a = 4 inside a block because with var, variables declared inside a block can be accessed from outside the block. The var a = 4 inside your function overwrites var = 40. If you change them to let keyword it should work as intended.

Assigning objects to an array, then call it from the array based on position

I've been working on this code but am puzzled as to why it doesn't work. I've created three "Geocache" objects and assigned them to an array called 'caches'. To test, I've assigned the variable "value" to whatever is in index 1 of caches. I then defined the variable "tease" as the 'lony' property of value.
function Geocache (lat, lon, disc){
this.laty=lat;
this.lony=lon;
this.disc=disc;
}
var loc1=new Geocache(43.77416104496804, -79.50804830784301, "lifesci building");
var loc2=new Geocache(43.77381242916627, -79.50533927673797, "lassonde building");
var loc3=new Geocache(43.77305321438563, -79.50353146786193, "vari hall");
var caches=[loc1,loc2,loc3];
var value = caches[1];
var tease=value.lony;
This should spit back the value of "-79.505..." but instead I get an "undefined" in the preview.
Can someone please help me figure out what is wrong with this code because I can't seem to figure it out.
Here is you code, with the javascript in the javascript part of the snippet.
And a fix to use a self invoking function to stop global leak.
function Geocache (lat, lon, disc){
this.laty=lat;
this.lony=lon;
this.disc=disc;
}
(function () {
var loc1=new Geocache(43.77416104496804, -79.50804830784301, "lifesci building");
var loc2=new Geocache(43.77381242916627, -79.50533927673797, "lassonde building");
var loc3=new Geocache(43.77305321438563, -79.50353146786193, "vari hall");
var caches=[loc1,loc2,loc3];
var value = caches[1];
var tease=value.lony;
console.log(tease);
})();

querySelector returns String instead of DOM-Element

I tried to parse the color-names on this page: http://www.google.com/design/spec/style/color.html#
Using this code:
var all = document.querySelectorAll(".color-group");
for(var i=0; i<all.length; i++){
var e = all[i];
var name = e.querySelector('span.name');
console.debug(name.innerHTML);
}
However the printed result is always undefined.
This slightly changed code however works:
var all = document.querySelectorAll(".color-group");
for(var i=0; i<all.length; i++){
var e = all[i];
var name = e.querySelector('span.name').innerHTML;
console.debug(name);
}
The only difference is that I access the result of querySelector directly and not via the name-variable.
I tried it with Chrome, Safari and Firefox which all did not return the color-names. IE however manged to get it right this time.
Is this a general bug or feature or is it a problem with the website?
If you're running that code in the global scope, the variable name conflicts with that of window.name (which is a string); consider creating a scope:
(function() {
var all = document.querySelectorAll(".color-group");
for(var i=0; i<all.length; i++){
var e = all[i];
var name = e.querySelector('span.name');
console.debug(name.innerHTML);
}
}());
Or, just run that code inside a regular named function and call that from the global scope.
It seems there is something special about variable name.
var name = 3; name
// => "3"
var dame = 3; dame
// => 3
This behaviour is exhibited even by a blank tab (in Chrome at least). If you name your variable something else, it will go away. I believe (!) the reason is that you're executing in console, and name always refers to window.name; it goes away if you run it in a script; but I am not 100% sure my explanation is the correct one.

Changing the selected object in Javascript

What I am trying to do is rewrite content on the page depending on which object I have selected. I have some objects like so:
function floorPlan(name,rev,sqft,bedrm,bthrm) {
this.name = name;
this.rev = rev;
this.sqft = sqft;
this.bedrm = bedrm;
this.bthrm = bthrm;
}
// 1BR Plans
var a1 = new floorPlan('A1',false,557,1,1);
var a2 = new floorPlan('A2',false,652,1,1);
var a3 = new floorPlan('A3',false,654,1,1);
var a4 = new floorPlan('A4',false,705,1,1);
var a5 = new floorPlan('A5',false,788,1,1);
// The Selected plan
var currentPlan = floorPlan.a1;
I am having the user control this via a .click() function in a menu:
$('.sideNav li').click(function() {
// Define the currentPlan
var current = $(this).attr('id');
var currentPlan = floorPlan.current;
});
The problem is that currentPlan keeps coming back as undefined and I have no idea why. Should I be defining currentPlan differently? I can't seem to find any resources to help me find the answer.
UPDATED:
I switched out a few parts per your suggestions:
// The Selected plan
var currentPlan = a1;
and....
// Define the currentPlan
var current = $(this).attr('id');
currentPlan = current;
However, everything is still returning undefined in the click function (not initially though).
First of all $('this') should be $(this)
Secondly you're trying to use a read ID from your LI as a variable name. That doesn't work. If you store your plans in an array you can use the ID to search in that array:
var plans=Array();
plans["a1"]=new floorPlan('A1',false,557,1,1);
plans["a2"]=new floorPlan('A2',false,652,1,1);
Then your jQuery code should be altered to this:
$('.sideNav li').click(function() {
// Define the currentPlan
var current = $(this).attr('id');
var currentPlan = plans[current];
alert(currentPlan);
});
I created a JSFiddle for this. Is this what you were looking for?
Use as floorPlan.currentPlan = a1;
instead of var currentPlan = floorPlan.a1;
Please create a plunker and will correct if any issue.
I spot two errors.
When you write var inside a function, that variable is only accessible with that function. Right now you are creating a new variable in your anonymous function that is "hiding" the global variable with the same name.
So, first remove the var keyword from the assignment in the anonymous function (the one you call on "click").
Secondly I think you mean to assign floorPlan[current].
The final line should read:
currentPlan = floorPlan[current];

Can't access global array in JavaScript [duplicate]

This question already has an answer here:
variable scope in d3 javascript
(1 answer)
Closed 7 years ago.
I have a global variable say :
var series,hours;
var loadData = function(){
series = [[]];
hours = [];
d3.json("data/radar.json", function(error, data) {
data.QualitySummaryObject.forEach(function(d,i) {
series[i] = d.extractPercentage;
hours[i] = d.extractorName;
});
});
console.log(hours);
};
Now if I am trying to access this console.log its working fine, but.
var print = function(){
console.log(hours); //here its not working no value in hours why ... ?
}
Ensure that you hours is global:
window.hours = [];
Then anywhere you can log it:
console.log(window.hours);
Using directly var without declaration will avoid context problems.
If you are running the above code in a function, the variable hours is not a global variable because you previously declared it with var.
If you want to declare a variable as a global variable, instead of this:
var hours = [];
Do this:
hours = [];
To declare a global variable, all you have to do is give it a name.

Categories