Use typeof to determine whether a function exists in the context.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.