Home Web Front-end JS Tutorial js类型检查实现代码_javascript技巧

js类型检查实现代码_javascript技巧

May 16, 2016 pm 06:17 PM
type checking

复制代码 代码如下:

//检查我们的数字是否其实是一个字符串
if ( num.constructor == String )
//如果是,则将它解析成数字
num = parseInt( num );
//检查我们的字符串是否其实是一个数组
if ( str.constructor == Array )
//如果是,则用逗号连接该数组,得到一个字符串
str = str.join(',');

表1显示了对不同类型对象分别使用我所介绍的两种方法进行类型检查的结果。表格的第一列显示了我们试图找到其类型的对象。每二列是运行typeof Variable(Variable 为第一列所示的值)。此列中的所有结果都是字符串。最后,第三列显示了对第一列包含的对象运行Variable.constructor 所得的结果。些列中的所有结果都是对象。

表1. 变量类型检查

———————————————————————————————
Variable       typeof Variable       Variable.constructor
———————————————————————————————
{an:"object"}    object            Object
["an","array"]     object            Array
function(){}      function           Function
"a string"       string            String
55           number            Number
true         boolean           Boolean
new User()      object            User
——————————————————————————————————

使用一个变量的constructor 作为对象类型的引用可能是最简单的类型检查方式。当你想要确定精确吻合的参数数目的类型传进了你的函数时,严格的类型检查在这种可能会大有帮助。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C++ function parameter type safety check C++ function parameter type safety check Apr 19, 2024 pm 12:00 PM

C++ parameter type safety checking ensures that functions only accept values ​​of expected types through compile-time checks, run-time checks, and static assertions, preventing unexpected behavior and program crashes: Compile-time type checking: The compiler checks type compatibility. Runtime type checking: Use dynamic_cast to check type compatibility, and throw an exception if there is no match. Static assertion: Assert type conditions at compile time.

Type hints and type checking for PHP functions Type hints and type checking for PHP functions Apr 10, 2024 am 11:51 AM

Type hints and type checking of PHP functions help improve the quality and reliability of the code. It tells the PHP function through comments the expected incoming and outgoing data types, including basic data types (integers, floating point numbers, strings, etc. ) and composite data types (arrays, objects, etc.), and verify whether these types meet expectations at runtime through type checking, reducing errors caused by type mismatches.

How does PHP8 provide stricter type checking via Union Types? How does PHP8 provide stricter type checking via Union Types? Oct 18, 2023 am 11:24 AM

How does PHP8 provide stricter type checking via UnionTypes? Summary: PHP8 introduces a new syntax feature - UnionTypes, which allows developers to more accurately define the parameter and return value types of functions and methods. This article will introduce the definition and use of UnionTypes in detail, and demonstrate its advantages in implementing stricter type checking in PHP8 through code examples. Introduction: Over the past few versions, PHP has gradually enhanced its type system, evolving from weak typing to

How to check PHP function parameter types? How to check PHP function parameter types? Apr 10, 2024 pm 06:33 PM

Methods for checking function parameter types in PHP: use typehints to specify parameter and return value types, and throw a TypeError exception; use getType() to get the actual type of the variable, which is used for conditional statements; use assert() to check the condition, and throw AssertionError when it is false Exceptions and error messages.

How to avoid errors in Java code? How to avoid errors in Java code? Sep 11, 2023 pm 11:57 PM

When a developer breaks the rules of the Java programming language, an error appears. It could result from a programmer's typing errors while developing a program. Then

How to use typescript for type checking in Vue How to use typescript for type checking in Vue Jun 11, 2023 pm 05:16 PM

Vue is a popular front-end framework that uses template syntax to render applications and provides a rich set of components and lifecycle hooks. However, Vue was originally written in JavaScript, and JavaScript is a weakly typed language, which means that when developing large applications, it is easy to make type errors. To solve this problem, Vue can use TypeScript for type checking. TypeScript is a superset of JavaScript that adds

How to implement strict type checking of function parameters in PHP? How to implement strict type checking of function parameters in PHP? Apr 10, 2024 pm 03:54 PM

PHP function parameter type strict checking can ensure that the passed parameters are consistent with the declared type. After enabling via declare(strict_types=1), function parameters are required to match the specified type, otherwise a TypeError exception is thrown. Strict checking supports basic types (int, float), composite types (objects, arrays), union types (int|string) and optional types (?int) to improve code robustness and prevent wrong type parameters from being passed.

How to check the type of function return value in PHP? How to check the type of function return value in PHP? Apr 10, 2024 pm 06:12 PM

PHP provides three methods to check the function return value type: 1. Use the gettype() function; 2. Use the is_*() function; 3. Use the instanceof operator. These methods ensure code robustness and avoid unexpected errors.

See all articles