变量仅在本地工作

时间:2017-03-07 02:37:14

标签: javascript global-variables local-variables

我写了一些涉及素数因子分解的函数,我注意到当我将我的测试段落(用于测试函数的结果等)确定为document.getElementById("text")时,它运行正常。但是,当我将全局变量text声明为var text = document.getElementById("text"),然后在text中替换为较长版本时,它就不再起作用了。但是,我确实注意到当我在本地声明text时它起了作用。为什么这样,我该如何解决?我的JSFiddle在这里:https://jsfiddle.net/MCBlastoise/3ehcz214/

这是我的代码:



var text = document.getElementById("text");

function isPrime(num) {
  var lastDigit = parseInt((num + "").split("").reverse()[0]);
  if (typeof num !== "number" || num <= 1 || num % 1 !== 0) {
  	return undefined;
  }
	else if (num === 2) {
  	return true;
  }
  else if (lastDigit === 0 || lastDigit === 2 || lastDigit === 4 || lastDigit === 5 || lastDigit === 6 || lastDigit === 8) {
  	return false;
  }
  else {
    for (var i = 2; i < num; i++) {
    	if (num % i === 0) {
        return false;
      }
    }
    return true;
  }
}

function factorSplit(dig) {
  if (typeof dig !== "number" || dig <= 1 || dig % 1 !== 0) {
    return undefined;
  }
  else if (dig === 2) {
  	return undefined;
  }
  else {
  	var factor;
  	for (var i = 2; i < dig; i++) {
   		if (dig % i === 0) {
   			factor = i;
				break;
  		}
  	}
  	if (factor === undefined) {
  	  return undefined;
  	}
		else {
  		return [factor, (dig / factor)];
		}
  }
}

function allPrimes(arr) {
	if (Array.isArray(arr) === false || arr.length < 1) {
		return undefined;
	}
	else {
    for (var i = 0; i < arr.length; i++) {
  		if (isPrime(arr[i]) !== true) {
   	 		return false;
  	  }
  	}
		return true;
	}
}

function primeFactors(int) {
	if (typeof int !== "number" || int <= 1) {
  	return undefined;
  }
  else if (isPrime(int) === true) {
  	return false;
  }
  else {
  	var initFactors = factorSplit(int);
  	while (allPrimes(initFactors) !== true) {
  		initFactors = initFactors.concat(factorSplit(initFactors[initFactors.length - 1]));
    	initFactors.splice((initFactors.length - 3), 1);
  	}
  	return initFactors;
  }
}

function listPrimes() {
	repeat = setInterval(findPrime, 1);
}

var primeInts = [2];
var check;
function findPrime() {

  var i = primeInts[primeInts.length - 1] + 1;
	if (check === undefined) {
		check = true;
		text.innerHTML = primeInts[0];
	}
	else {
		while (isPrime(i) !== true) {
			i++;
		}
		primeInts.push(i);
		text.innerHTML += ", " + primeInts[primeInts.length - 1];
	}
}

//text.innerHTML = isPrime(6);
&#13;
<div onclick="listPrimes()" style="cursor:pointer; background-color:black; width:30px; height:30px"></div>
<p id="text"></p>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

文本是全局的,您只需要确保整个脚本文件包含在html中。这是我的意思的一个例子

这里的代码片段stackoverflow已经为我们做了这个。

&#13;
&#13;
var text = document.getElementById("text");

function isPrime(num) {

  var lastDigit = parseInt((num + "").split("").reverse()[0]);
  if (typeof num !== "number" || num <= 1 || num % 1 !== 0) {
    return undefined;
  } else if (num === 2) {
    return true;
  } else if (lastDigit === 0 || lastDigit === 2 || lastDigit === 4 || lastDigit === 5 || lastDigit === 6 || lastDigit === 8) {
    return false;
  } else {
    for (var i = 2; i < num; i++) {
      if (num % i === 0) {
        return false;
      }
    }
    return true;
  }
}

function factorSplit(dig) {

  if (typeof dig !== "number" || dig <= 1 || dig % 1 !== 0) {
    return undefined;
  } else if (dig === 2) {
    return undefined;
  } else {
    var factor;
    for (var i = 2; i < dig; i++) {
      if (dig % i === 0) {
        factor = i;
        break;
      }
    }
    if (factor === undefined) {
      return undefined;
    } else {
      return [factor, (dig / factor)];
    }
  }
}

function allPrimes(arr) {
  if (Array.isArray(arr) === false || arr.length < 1) {
    return undefined;
  } else {
    for (var i = 0; i < arr.length; i++) {
      if (isPrime(arr[i]) !== true) {
        return false;
      }
    }
    return true;
  }
}

function primeFactors(int) {
  if (typeof int !== "number" || int <= 1) {
    return undefined;
  } else if (isPrime(int) === true) {
    return false;
  } else {
    var initFactors = factorSplit(int);
    while (allPrimes(initFactors) !== true) {
      initFactors = initFactors.concat(factorSplit(initFactors[initFactors.length - 1]));
      initFactors.splice((initFactors.length - 3), 1);
    }
    return initFactors;
  }
}

function listPrimes() {
  repeat = setInterval(findPrime, 1);
}

var primeInts = [2];
var check;

function findPrime() {

  var i = primeInts[primeInts.length - 1] + 1;
  if (check === undefined) {
    check = true;
    text.innerHTML = primeInts[0];
  } else {
    while (isPrime(i) !== true) {
      i++;
    }
    primeInts.push(i);
    text.innerHTML += ", " + primeInts[primeInts.length - 1];
  }
}

function test() {
  console.log("inside test1")
  console.log(text);
  text.innerHTML = "testtt"
}

function test2() {
  console.log("inside test2")
  console.log(text);
  text.innerHTML = "testtt2"
}

text.innerHTML = isPrime(6);
&#13;
<div onclick="test()" style="cursor:pointer; background-color:black; width:30px; height:30px"></div>


<p id="text"></p>

<div onclick="test2()" style="cursor:pointer; background-color:black; width:30px; height:30px"></div>
&#13;
&#13;
&#13;

在头部中,脚本首先运行/加载,因为你在函数中没有var,它们永远不会被重复使用,所以它们保留原始值,因为文档没有&# 39; t当时存在,然后当页面加载所有它具有对函数的访问权限时,对全局var的调用为null。这就是为什么代码以前仅在text = document.getElementById('text')处于函数中时才有效。