javascript typeof的用法
经常会在js里用到数组,比如 多个名字相同的input, 若是动态生成的, 提交时就需要判断其是否是数组.
if(document.mylist.length != "undefined" ) {} 这个用法有误.
正确的是 if( typeof(document.mylist.length) != "undefined" ) {}
或 if( !isNaN(document.mylist.length) ) {}
typeof的运算数未定义,返回的就是 "undefined".
运算数为数字 typeof(x) = "number"
字符串 typeof(x) = "string"
布尔值 typeof(x) = "boolean"
对象,数组和null typeof(x) = "object"
函数 typeof(x) = "function"
typeof 运算符返回一个用来表示表达式的数据类型的字符串。
可能的字符串有:"number"、"string"、"boolean"、"object"、"function" 和 "undefined"。
如:
alert(typeof (123));//typeof(123)返回"number"
alert(typeof ("123"));//typeof("123")返回"string"
typeof 运算符
返回一个用来表示表达式的数据类型的字符串。
typeof
[(
)expression
[]
] ;
expression
参数是需要查找类型信息的任意表达式
。
说明
typeof
运算符把类型信息当作字符串返回。typeof
返回值有六种可能: "number," "string," "boolean," "object," "function," 和 "undefined."
typeof
语法中的圆括号是可选项。
来自:http://hi.baidu.com/fyears/blog/item/7494734eaff7720bb2de0516.html/cmtid/da4d8c77101ea810b051b9bc
相关文档:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head ......
1.对于数组,我们可以自己写一个indexOf()方法:
[].indexOf || (Array.prototype.indexOf = function(v){
for(var i = this.length;i-- && this[i] !== v;);
return i; });
使用: var array = new Array();
array = ...;
if(array.ind ......
<html>
<head>
<mce:script language="javascript"><!--
function old_page()
{
window.location = "http://www.jb51.net"
}
function replace()
{
window.location.replace("http://www.jb51.net")
}
function new_page()
{
window.op ......
日前,Mozilla 的 Arun Ranganathan 向 W3C 提交了一个草案,旨在推出一个 JavaScript API,让
JavaScript 脚本直接操作本地文件,Firefox 的最新浏览器版本中已经对该 API 提供了大量的支持,不过出于安全考虑,该API
将只对本地文件提供有限的访问。
该 API 引入了一个 FileReader 对象,可以异步加载本地文件系统中的 ......
在页面上回车后,不做任何动作。
<script type="text/javascript">
function document.onkeydown()
{
if(event.keyCode ==   ......