If ( !obj || jQuery.type(obj) != "object" || obj.nodeType || jQuery. Exclude non object types, then DOM objects, window objects It is not DOM, and the object directly inherits Object.prototype on the prototype chain is called "plain object" Detect whether ispainobject method is used to detect whether it is a "plain object" Part of the code in jQuery (version 1.11.1): var class2type = JQuery considers all kinds of situations and the compatibility of browser versions. Of course, if you think the above methods are not comprehensive enough, let's see how to judge the object type in jQuery. Var toString = Ĭonsole.log(toString.call(new Date)) // Ĭonsole.log(toString.call(new String)) // Ĭonsole.log(toString.call(Math)) // Ĭonsole.log(toString.call(undefined)) // Ĭonsole.log(toString.call(null)) // Ĭonsole.log(toString.call(carr)) // Ĭonsole.log(toString.call(cobj)) // Ĭonsole.log(toString.call(cfun)) // ģ: A more comprehensive way to detect objects (Methods in jQuery) For example, const a 9 console.log (typeof a) // number console. The typeof operator returns the type of variables and values. In order for each object to be detected by (), it needs to be called in the form of () or (), passing the object to be checked as the first parameter. In this tutorial, you will learn about JavaScript typeof operator with the help of examples. You can get the type of each object through toString(). When you want to exclude null, function and Array, you can write as follows: var carr= When you want to exclude null and function, you can write as follows: var carr= Whether Array and Function want to be judged as object depends on the situation. Title: use typeof bar = "object" to determine whether bar is the potential trap of an object? How to avoid this trap?įirst look at the following code: var carr= Ĭonsole.log(typeof carr='object') //trueĬonsole.log(typeof cobj='object') //trueĬonsole.log(typeof cfun='function') //trueĬonsole.log(typeof null='object') //trueĪs you can see, typeof null is also "object", which is generally not what we want.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |