Home >Web Front-end >JS Tutorial >How to use jQuery's isPlainObject() method
This time I will show you how to use jQuery's isPlainObject() method. What are the precautions for using jQuery's isPlainObject() method? The following is a practical case, let's take a look.
Description
The isPlainObject() function in jQuery is used to determine whether the specified parameter is a pure object, and the return value is of Boolean type. "Pure object" is an object created through { }, new Object(), Object.create(null). The purpose of this method is to distinguish it from otherJavaScript objectssuch as null, arrays, host objects (documents), DOM, etc., because these typesof will return object.
Use
Syntax: $.isPlainObject( object )Parameter description: object: Any value of any type that needs to be judged.$.isPlainObject({}); //true $.isPlainObject(new Object); //true $.isPlainObject(Object.create(null)); //true $.isPlainObject([]); //false $.isPlainObject(document); //false
Source code analysis
Let’s take a look at the source code under jQuery 3.3.1 version:https://github.com/jquery/ jquery/blob/ac9e3016645078e1e42120822cfb2076151c8cbe/src/core.js#L236
var class2type = {}; //Object.getPrototypeOf() 方法返回指定对象的原型(内部[[Prototype]]属性的值)。 var getProto = Object.getPrototypeOf; //相当于 Object.prototype.toString var toString = class2type.toString; //hasOwnProperty() 方法会返回一个布尔值,指示对象自身属性中是否具有指定的属性 //相当于 Object.prototype.hasOwnProperty var hasOwn = class2type.hasOwnProperty; //因为 hasOwn 是一个函数,所以这里调用的是内置对象 Function 的toString() 方法 //相当于 Function.prototype.toString var fnToString = hasOwn.toString; //相当于 Function.prototype.toString.call(Object) //就是Object 构造函数 转字符串的结果 // ObjectFunctionString 其实就是 "function Object() { [native code] }" 这样的一个字符串 var ObjectFunctionString = fnToString.call(Object); function isPlainObject (obj) { var proto, Ctor;
Summary
From the source code, the implementation of the isPlainObject() method is mainly divided into three parts1. Remove the type that is not Object. Use the Object.prototype.toString.call() method. This method will get different strings for all types, instead of using typeof, because typeof can only distinguish basic types, such as arrays, typeof still returns "object" stringvar arr = []; var obj = {}; typeof arr; //"object" typeof obj; //"object" typeof document; //"object" Object.prototype.toString.call(arr); //"[object Array]" Object.prototype.toString.call(obj); //"[object Object]" Object.prototype.toString.call(document); //"[object HTMLDocument]"2. Determine whether the object has a prototype. Objects without prototypes are considered pure objects3. Determination Whether the object is created through "{}" or "new Object" methodIt is necessary to determine their constructor, so use the Function.prototype.toString method
Function Object overrides the Object.prototype.toString method inherited from Object.I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:The toString method of the function will return a string representing the function source code. Specifically, it includes the function keyword, formal parameter list, braces, and the content in the function body.
function fn(said){ this.say = said; } Function.prototype.toString.call(fn); //"function fn(said){ // this.say = said; //}" Function.prototype.toString.call(Object); //"function Object() { [native code] }"
How to implement the toggle method in jQuery
How to implement jQuery+JSONP across domains
How to use the select component in jquery
How to achieve the jquery carriage return login effect
The above is the detailed content of How to use jQuery's isPlainObject() method. For more information, please follow other related articles on the PHP Chinese website!