This time I will show you how to detect reference values in web development, and what are the precautions for detecting reference values in web development. The following is a practical case, let's take a look.
Reference values are also called objects. In JS, all values other than primitive values are references. There are several built-in reference types: Object, Array, Date and Error, not many in number. The typeof operator is unable to determine these reference types, because all objects will return "object".
Another not recommended use of typeof is when detecting the type of null. The typeof operator will return "object" when used for null. This looks weird and is considered a serious bug in the standard specification. Therefore, you should avoid using typeof to detect the type of null when programming.
The best way to detect the type of a reference value is to use the instanceof operator. The basic syntax of instanceof is: value instanceof constructor.
An interesting feature of instanceof is that it not only detects the constructor that constructs the object, but also detects the prototype chain. The prototype chain contains a lot of information, including the inheritance pattern used to define the object. For example, by default, every object inherits from Object, so value instanceof Object for every object returns true. For this reason, it is not optimal to use value instanceof Object to determine whether an object is of a specific type.
The instanceof operator can also detect custom types, such as:
function Person (name) { this.name = name; }var me = new Person('Nicholas');console.log(me instanceof Object); // trueconsole.log(me instanceof Person); // true
When detecting custom types in JS, the best way is to use the instanceof operator, which is also the only way . Same goes for built-in JS types (using instanceof operator). However, there is a serious limitation.
Suppose an object in a browser frame (frameA) is passed to another frame (frameB). The constructor Person is defined in both frames. If the object from frame A is an instance of Person of frame A, then the following rules hold.
frameAPersonInstance instanceof frameAPerson; // trueframeAPersonInstance instanceof frameBPerson; // false
Because each frame has a copy of Person, it is considered to be a copy instance of Person in that frame, even though the two definitions may be exactly the same. This problem not only occurs with custom types, but also with two other very important built-in types: functions and arrays. For these two types, there is generally no need to use instanceof.
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:
How to detect original values in web development
How to use JS to pass by reference and value
The above is the detailed content of How to detect reference values in web development. For more information, please follow other related articles on the PHP Chinese website!