一、是否存在指定函數(shù) 
function isExitsFunction(funcName) {  try {    if (typeof(eval(funcName)) == "function") {      return true;    }  } catch(e) {}  return false;}二、類似PHP常用的判斷函數(shù)是否存在,不存在則創(chuàng)建
if (typeof String.prototype.endsWith != 'function') { String.prototype.endsWith = function(suffix) {  return this.indexOf(suffix, this.length - suffix.length) !== -1; };}三、判斷js函數(shù)是否存在,如果存在則執(zhí)行
假設(shè)funcName為函數(shù)名字,用如下方法就可以達(dá)到目標(biāo)
一定要添加try catch塊,否則不起作用。
try {  if(typeof(eval(funcName))=="function")  {   funcName(); }}catch(e){//alert("not function"); } 四、是否存在指定變量 
function isExitsVariable(variableName) {  try {    if (typeof(variableName) == "undefined") {      //alert("value is undefined");       return false;    } else {      //alert("value is true");       return true;    }  } catch(e) {}  return false;}一般情況下,我們單獨(dú)判斷變量是否存在都是用
if("undefined" != typeof downlm){ if(downlm=="soft"){ document.write('成功'); } }這樣就不會(huì)因?yàn)橹苯邮褂米兞繉?dǎo)致出錯(cuò)了,適用于頁(yè)面改版,舊頁(yè)面沒有變量賦值的情況。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注