Use typeof to determine whether a function exists in the context.
When loading a window, use typeof to determine whether the function exists in the context.
<Script type = "text/javascript"> window. onload = function () {try {if (test & typeof (test) = "function") {test () ;}} catch (e) {alert ("method does not exist") ;}} function test () {alert ("I'm the test () method") ;}</script>
If (typeof windowonload! = 'Function') What is type?
Typeof Operator
Returns a string that represents the expression's data type.
Typeof [() expression [];
The expression parameter is an arbitrary expression that needs to be searched for type information.
Description
The typeof operator returns the type information as a string. There are six possible types of typeof return values: "number," "string," "boolean," "object," "function," and "undefined ."
Parentheses in the typeof syntax are optional.
Requirements
Version 1
Arrays are often used in js. For example, if multiple inputs with the same name are generated dynamically, You need to determine whether the input is an array during submission.
If (document. mylist. length! = "Undefined") {} This usage is incorrect.
Correct is if (type (document. mylist. length )! = "Undefined") {} Or if (! IsNaN (document. mylist. length )){}
The number of typeof operations is undefined, and "undefined" is returned ".
Number of operations: number typeof (x) = "number"
String typeof (x) = "string"
Boolean value typeof (x) = "boolean"
Object, array, and null typeof (x) = "object"
Function typeof (x) = "function"
Question: What is typeof (callback) = 'function '?
This should be written in JS. Typeof is the type for obtaining the target variable. Typeof (callback) is the variable type for obtaining callback. function is the built-in type in js and represents a function. This statement is used to determine whether callback is a function.