Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在函数调用之间保留变量值_Javascript_Ios_Uiwebview - Fatal编程技术网

Javascript 在函数调用之间保留变量值

Javascript 在函数调用之间保留变量值,javascript,ios,uiwebview,Javascript,Ios,Uiwebview,我有一些变量,我想在函数调用之间保留它们的值,请大家分享一下如何在javascript中做到这一点。我尝试过使用全局变量,但那没有帮助。非常感谢您的帮助,例如,在下面的代码中,每当调用函数跳转时,警报值总是相同的,不会对每个函数调用递增。警报(this.prevVal);和警报(this.currentVal) //我们使用一个全局变量来存储出现的次数 var MyApp_SearchResultCount=0; var=0; var countStr=0; //var-prevEl,el; /

我有一些变量,我想在函数调用之间保留它们的值,请大家分享一下如何在javascript中做到这一点。我尝试过使用全局变量,但那没有帮助。非常感谢您的帮助,例如,在下面的代码中,每当调用函数跳转时,警报值总是相同的,不会对每个函数调用递增。警报(this.prevVal);和警报(this.currentVal)

//我们使用一个全局变量来存储出现的次数
var MyApp_SearchResultCount=0;
var=0;
var countStr=0;
//var-prevEl,el;
//helper函数,递归搜索元素及其子节点
函数MyApp\u highlightAlloccurrencesofstringforelement(元素,关键字){
if(元素){
如果(element.nodeType==3){//Text节点
while(true){
var value=element.nodeValue;//在文本节点中搜索关键字
var idx=value.toLowerCase().indexOf(关键字);
如果(idx<0)中断;//未找到,则中止
var span=document.createElement(“span”);
var text=document.createTextNode(value.substr(idx,keyword.length));
span.appendChild(文本);
setAttribute(“类”、“MyAppHighlight”);
span.style.backgroundColor=“黄色”;
span.style.color=“黑色”;
text=document.createTextNode(value.substr(idx+关键字.length));
element.deleteData(idx,value.length-idx);
var next=element.nextSibling;
element.parentNode.insertBefore(span,next);
element.parentNode.insertBefore(文本,下一步);
元素=文本;
window.MyApp_SearchResultCount++;//更新计数器
//countStr=MyApp\u SearchResultCount;
}
}如果(element.nodeType==1){//element节点
if(element.style.display!=“无”&&element.nodeName.toLowerCase()!=“选择”){
对于(var i=element.childNodes.length-1;i>=0;i--){
MyApp_HighlightAlloccurrenceSofsTringForElement(element.childNodes[i],关键字);
}
}
}
}
}
//开始搜索的主要入口点
函数MyApp\u HighlightAlloccurrencesofString(关键字){
警报(“测试”);
//MyApp_RemoveAllHighlights();
MyApp_HighlightAlloccurrenceSofsTringForElement(document.body,keyword.toLowerCase());
警报(window.MyApp\u SearchResultCount);
}
//helper函数,递归删除元素及其子元素中的高光
函数MyApp_RemoveAllHighlightsRelation(元素){
if(元素){
if(element.nodeType==1){
if(element.getAttribute(“类”)=“MyAppHighlight”){
var text=element.removeChild(element.firstChild);
element.parentNode.insertBefore(文本,元素);
element.parentNode.removeChild(元素);
返回true;
}否则{
var正常化=错误;
对于(var i=element.childNodes.length-1;i>=0;i--){
if(MyApp_removeallhighlightsfrelation(element.childNodes[i])){
正常化=正确;
}
}
如果(正常化){
元素。normalize();
}
}
}
}
返回false;
}
//删除高光的主要入口点
函数MyApp_RemoveAllHighlights(){
window.MyApp\u SearchResultCount=0;
MyApp_移除所有高亮显示相关内容(document.body);
}
函数goNext(){
跳跃(1);
}
函数goPrev(){
跳跃(-1);
}
所选变量=0;
var currSelectedGlo=0;
this.prevVal=0;
此.currentVal=0;
功能跳跃(多高){
this.prevVal=this.currentVal;
this.currentVal=this.currentVal+1;
警报(this.prevVal);
警报(this.currentVal);
prevSelected=currSelected;
currSelected=currSelected+多高;
//window.currSelectedGlo=currSelected+howHigh;
//currSelected=window.currSelectedGlo;
//警报(“prevSelected”+prevSelected);
//警报(“window.currSelected”+currSelected);
//警报(window.MyApp\u SearchResultCount);
//警报(已选择);
如果(当前选定值<0){
currSelected=window.MyApp\u SearchResultCount+currSelected;
}
if(currSelected>=window.MyApp\u SearchResultCount){
currSelected=currSelected-window.MyApp\u SearchResultCount;
}
prevEl=document.getElementsByClassName(“MyAppHighlight”)[prevSelected];
//警报(window.prevEl);
if(prevEl){
prevEl.style.backgroundColor=“黄色”;
}
el=document.getElementsByClassName(“MyAppHighlight”)[currSelected];
el.style.backgroundColor=“绿色”;
el.scrollIntoView(true);//谢谢techfoobar
}
谢谢
djrecker

您可以使用全局变量:

var value = 0;

function next() {
    return value++;
}

console.log(next());
console.log(next());
 someVar = 0;

 function increaseSomeVar(){

      someVar++;
 }
 var someVar = 0;

 function increaseSomeVar(somelocalVar){
    somelocalVar++;
    return(somelocalVar);   
 }

 someVar = increaseSomeVar(someVar);
或者更好,一个具有属性和方法的对象:

function Counter() {
    this.value = 0;
}

Counter.prototype.next = function() {
    return this.value++;
};

var counter = new Counter();
console.log(counter.next());
console.log(counter.next());

有两种方法可以做到这一点:

1) 全局变量:

var value = 0;

function next() {
    return value++;
}

console.log(next());
console.log(next());
 someVar = 0;

 function increaseSomeVar(){

      someVar++;
 }
 var someVar = 0;

 function increaseSomeVar(somelocalVar){
    somelocalVar++;
    return(somelocalVar);   
 }

 someVar = increaseSomeVar(someVar);
2) 返回一个变量:

var value = 0;

function next() {
    return value++;
}

console.log(next());
console.log(next());
 someVar = 0;

 function increaseSomeVar(){

      someVar++;
 }
 var someVar = 0;

 function increaseSomeVar(somelocalVar){
    somelocalVar++;
    return(somelocalVar);   
 }

 someVar = increaseSomeVar(someVar);

首先,使用全局变量通常是个坏主意

由于javascript通过引用传递对象,所以可以使用具有数字的对象作为更新的属性。通常,您可以随意传递此对象,除非您调用的函数会丢失当前的作用域,如
setTimeout
。在这种情况下,您可以使用jquery的bind函数来跟踪变量和范围,例如

 MyApp_HighlightAllOccurencesOfStringForElement.bind(this, element, keyword);
这不是创建全局变量的常用方法,如果用于此目的,则很容易出错。此外,在使用严格模式时,您可能会遇到额外的障碍

要使变量可靠地全局化,请执行以下操作

var prevVal = 0; 
var currentVal = 0;

function jump(howHigh){ 
    prevVal = currentVal; 
    currentVal = currentVal + 1; 
小提琴(我也将
+1
改为
+howHigh
):

你不能得到比这更全局的结果,但是如果你想让你的变量在页面导航、重新加载等过程中幸存下来,你必须使用
LocalStorage
(在IE7中不起作用)或cookies:

function jump(howHigh){ 
    var currentVal = +localStorage.getItem("currentVal"); // + to cast to number
    prevVal = currentVal; 
    currentVal = currentVal + 1; 
    localStorage.setItem("currentVal", currentVal); // store back
再次,小提琴:


这是
This
的常用用法模式:

function X(){
    this.prevVal=0;
    this.currenVal=0;
}
X.prototype.jump = function(){
    this.prevVal = this.currentVal; 
    this.currentVal = this.currentVal + 1; 
...

//test:
var x1 = new X();
var x2 = new X();

x1.jump(1); // 0=>1
x2.jump(2); // 0=>2
x1.jump(3); // 1=>4

...

你能展示一下你的代码吗?请以你的一些代码为例。全局变量总是保留它们的值,除了跨