Js Object-Oriented Programming: how to detect object types and js Object-Oriented Programming
In js, there are three types of objects to be detected: typeof, instanceof, and constructor. These types can detect objects, but there are some differences.
1. Use typeof to detect object types.
Typeo, as the most common method for detecting types, returns the string type. The specific usage is as follows:
Function testType (value) {var str = typeof (value); // alert (str); switch (str) {case 'undefined': // undefined type case 'object ': // null type, any built-in object, array case 'boolean': // true, false type case 'string': // string type case 'function ': // any function case 'number': // any numeric type, including NaN }}
We can see that the type can be tested for the most basic type, but for other types, including date and array, most of them return the object type, and null also returns the object type, that is, there is no way to know the exact type.
Another improved detection method is to use the default ToString that inherits from the object and can return type information.
Function classof (o) {if (o = null) return "Null"; if (o = undefined) return "Undefined"; return Object. prototype. toString. call (o ). slice (8,-1);} function testType (value) {var str = classof (value); // typeof (value); alert (str); switch (str) {case 'undefined': // undefined type case 'object': // object case 'boolean': // true, false type case 'string ': // string type case 'function': // any Function case 'number': // any numeric type, including NaN case 'date ': // Date case 'array': // Array case 'regexp': // regular }}
We can see some improvements, but there is still a large part of the object type.
2. Use instanceof to detect Object Types
If typeof is detected as an object type, you can use instanceof to further detect the specific type. The prototype of the object actually detected by instanceof.
It can detect whether a variable is an instance of an object and return the bool value.
For example:
var value=new Date();var isdate= value instanceof Date alert(isdate);
3. Use constructor to detect Object TypesDetection object type
Constructor is equivalent to detecting constructors and returns a function.
For example:
Function testconstructor (value) {var str = value. constructor; switch (value. constructor) {case Number: // value type break;} // alert (str );}
You can use these three methods to check the exact type of an object.
For example:
Function type (o) {var t, c, n; // type, class, name // is null type: if (o = null) return "null "; // It is a special type in the value: NaN: if (o! = O) return "nan"; // use typeof to detect other types except "object". if (t = typeof o )! = "Object") return t; // typeof detection is of the "object" type, then we can further detect // we can detect most of the built-in types of if (c = classof (o ))! = "Object") return c; // when classof (o) returns "Object", constructor if (o. constructor & typeof o. constructor = "function" & (n = o. constructor. getName () return n; // other types that cannot be identified. The default value is "Object" return "Object";} function classof (o) {return Object. prototype. toString. call (o ). slice (8,-1) ;}; // return the function name, which may be "" Or nullFunction. prototype. getName = function () {if ("name" in this) return this. name; return this. name = this. toString (). match (/function \ s * ([^ (] *) \ (/) [1] ;};
JS Object-Oriented Programming: How B inherits
B. prototype = newa;
What is the idea of js object-oriented programming? Can I give an example?
The idea of object-oriented programming is to regard all the specific things in the problem you need to solve as an object, and then combine all the objects of the same nature into a class, objects irrelevant to the problem are ignored.