Read foreach variable value outside the function - javascript

i have 2 functions where i have many variables stored . i want to read those variables outside the function as well. so i can read that value and apply if condition on that. please help.
And here are the functions,
var dynamicFormObject = new dynamicObject(jsonDataModel);
function dynamicObject(jsonData) {
this.formId = jsonData[0].formName;
console.log(this.formId);
this.formType = jsonData[0].formType;
this.formLayout = jsonData[0].formLayout;
this.sectionCount = jsonData[0].sections.length;
this.getSectionDetails = sectionDetails(jsonData[0].sections);
this.formRows = getSetionFieldDetails(jsonData[0].sections);
this.formFields = getContainedFields(jsonData[0].sections);
this.getNoOfSections = function(jsonData) {
return jsonData[0].sections.length;
};
}
function sectionDetails(sectionData) {
var sectionDetails = [];
if (sectionData.length > 0) {
sectionData.forEach(function(item) {
sectionDetails.push({
sectionId: item.sectionId,
sectionName: item.sectionName,
sectionLayout: item.sectionLayout
});
});
return sectionDetails;
} else {
return "Error";
}
}
sectionDetails(jsonDataModel);
console.log(sectionDetails[0].sectionLayout);
if($scope.entity[0].sections[0].sectionId === 1 && $scope.entity[0].sections[0].sectionLayout == "linear_layout_vertical"){
if(dynamicFormObject.formLayout == "linear_layout_horizontal"){
console.log("sadasd");
var newdiv = document.createElement("div");
console.log(newdiv);
newdiv.setAttribute('horizontal', '');
newdiv.setAttribute('layout', '');
newdiv.className('col-md-12');
document.body.appendChild(newdiv);
}

It's not possible to access a variable declared from within a function closure from outside it's function closure, however, you can declare a variable outside the function and only use/update it within the function, and the results of that use/update will be available outside the function.
var x;
function setX(){
x = 6;
}
setX();
console.log(x) // 6

Related

Get a reference to a local scope from a function

Let's say I have this code:
// somewhere else
var outFn = (() => {
var local = 5;
var fn = () => { console.log(local); };
return fn;
})();
// I have a reference to outFn
outFn(); // prints 5
How to get a reference to outFn's local scope, that is, how to get a reference to local variable using my reference to outFn?
Don't write the last parentheses :
var outFn = (() => {
var local = 5;
var fn = () => { console.log(local); };
return fn;
}) //(); <-- No need those parentheses
Then :
var localFn= outFn();
localFn() ; // prints 5
If you want to access also to local variable also , attach it to fn like following :
var outFn = (() => {
var fn = () => { console.log(fn.local); };
fn.local = 5;
return fn;
});
Now you can read local :
var localFn= outFn();
localFn.local ; // ===5

How to compare two variables and set a value of a third variable

I have this function I can't seem to get to work. I want it to compare two variables and set the value of a third variable.
var win_lose;
var this_roll = $('#past')[0].childNodes[9].textContent;
var last_roll = $('#past')[0].childNodes[9].textContent;
function compare() {
if (!(this_roll == last_roll)) {
win_lose = 'lose';
} else {
win_lose = 'win';
}
console.log(win_lose);
}
Did you actually call the function?
var this_roll = $('#past')[0].childNodes[9].textContent;
var last_roll = $('#past')[0].childNodes[9].textContent;
function compare(this_roll, last_roll) {
var win_lose; //added new variable here
if (this_roll != last_roll) {
win_lose = 'lose';
} else {
win_lose = 'win';
}
return win_lose;
}
var res = compare(this_roll, last_roll);
console.log(res);
I also rewrote your if statement, no need to check for equality then invert.
I would also pass parameters in to a function as I have.

JS how to use 'this' to point to its parent

var data = {
data2 : {
createNew : function() {
data.data2 = 10;
// smth like `this = 10`
}
}
}
How can I use this to point to the data.data2, as I don't want to repeat data.data2?
Understand through the comments your question is actually, to access the parent from within data2 .
You would need a constructor function (root is available in data2 due to closures in javascript)
function Data(){
var root = this;
this.data2 = {
createNew : function() {
data.data2 = 10;
root.x = 10;
}
}
}
var data = new Data();
data.data2.createNew();
console.log(data.x);

how can I remember my variable out if

How can I remember my variable tempValue outside if to give it as a parameter to other function? Because however it returns zero...
function rememberFirstOperand()
{
var rep1 = /[/|*|+|-]/;
var rep2 = /[0-9](?=[*|/|+|-])/;
var tempValue=0;
if (rep1.test(calculator.answer.value))
{
tempValue=calculator.answer.value;
calculator.answer.value='';
}
return tempValue;
}

Javascript function doesn't receive an argument correctly [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript closures and variable scope
Assign click handlers in for loop
I have this script:
var MyClass = {
MyArray: new Array(0, 1, 2, 3, 4),
MyFunc1: function() {
var i = 0;
for (i = MyClass.MyArray.length - 1; i>=0; i--) {
var cen = document.getElementById("cen_" + i); // It is an img element
cen.src = "col.png";
cen.className = "cen_act";
cen.onclick = function() { MyClass.MyFunc1(i); };
} else {
cen.src = "no.png";
cen.className = "cen";
cen.onclick = null;
}
}
},
MyFunc2: function(id) {
alert(id);
}
}
My problem is that, at this line :cen.onclick = function() { MyClass.MyFunc1(i); }; the argument sent to MyFunc2 is always -1. The MyFunc1 function should create four images, each one with an onclick event. When you click on each image, the MyFunc2 function should show the corresponding i value. It looks like the i value is not "saved" for each event and image element created, but only its "pointer".
Thanks!
You should be familiar with the concept of JavaScript closures to understand why this happens. If you are, then you should remember that every instance of the
function() { MyClass.MyFunc1(i); };
function closure contains i's value of -1 (since it is the final value of this variable after the entire loop finishes executing.) To avoid this, you might either use bind:
cen.onclick = (function(i) { MyClass.MyFunc1(i); }).bind(null, i);
or use an explicitly created closure with the proper i value.
It's a normal case and misunderstand of closures, see this thread and you may get some clue, the simply way to fix this problem is to wrap your for loop body with an Immediate Invoked Function Expression
MyFunc1: function() {
var i = 0;
for (i = MyClass.MyArray.length - 1; i>=0; i--) {
(function(i) {
var cen = document.getElementById("cen_" + i); // An img element
cen.src = "col.png";
cen.className = "cen_act";
cen.onclick = function() { MyClass.MyFunc2(i); };
} else {
cen.src = "no.png";
cen.className = "cen";
cen.onclick = null;
}
}(i));
}
}
You are capturing a variable that changes inside the loop, so you always get the last value of i.
You can easily fix that by creating a closure:
MyFunc1: function() {
var i = 0;
for (i = MyClass.MyArray.length - 1; i>=0; i--) {
(function(i) {
var cen = document.getElementById("cen_" + i); // An img element
cen.src = "col.png";
cen.className = "cen_act";
cen.onclick = function() { MyClass.MyFunc2(i); };
} else {
cen.src = "no.png";
cen.className = "cen";
cen.onclick = null;
}
})(i);
}
},

Categories

Resources