Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

javascript - 如何在jQuery中检查'undefined'值(How to check 'undefined' value in jQuery)

Possible Duplicate:(可能重复:)

How we can add a check for an undefined variable, like:(我们如何为未定义的变量添加检查,例如:)

function A(val) {
  if (val == undefined) 
    // do this
  else
    // do this
}
  ask by Gaurav translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

JQuery library was developed specifically to simplify and to unify certain JavaScript functionality.(JQuery库是专门为简化和统一某些JavaScript功能而开发的。)

However if you need to check a variable against undefined value, there is no need to invent any special method, since JavaScript has a typeof operator, which is simple, fast and cross-platform:(但是,如果需要根据undefined值检查变量,则无需创建任何特殊方法,因为JavaScript具有typeof运算符,该运算符简单,快速且跨平台:)

if (typeof value === "undefined") {
    // ...
}

It returns a string indicating the type of the variable or other unevaluated operand.(它返回一个字符串,指示变量的类型或其他未评估的操作数。)

The main advantage of this method, compared to if (value === undefined) { ... } , is that typeof will never raise an exception in case if variable value does not exist.(与if (value === undefined) { ... }相比,此方法的主要优点是,如果变量value不存在, typeof将永远不会引发异常。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...