JavaScript深入浅出(一)数据类型
来源:互联网 发布:手机故障检测软件 编辑:程序博客网 时间:2024/05/24 01:24
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
<script>
//object 对象=function,array,date //对象
//number //数字
//string //字符串
//boolean //布尔
//null //无效
//undefined //未定义
var op='p'+1; //理解为字符串拼接
var po='3'-1; //理解为减法运算
//巧用-号将字符串转化为数字
var none='3';
var none1=none-0;
//巧用+号将数字转化为字符串
var nonec=3;
var nonec1=nonec+'';
//等于,当等于一边是字符串一边是数字时会尝试把字符串转换为数字再进行比较
/*var nloo=1.23;
var nlooc='1.23';
if(nloo==nlooc){
console.log(1);
}else{
console.log(2);
}*/
//等于,false==0
/*var nlooa=0;
var nlooca=false;
if(nlooa==nlooca){
console.log(1);
}else{
console.log(2);
}*/
//等于,true==1
/*var nloob=1;
var nloocb=true;
if(nloob==nloocb){
console.log(1);
}else{
console.log(2);
}*/
//等于,null==undefined
/*var nlooc=null;
var nloocc=undefined;
if(nlooc==nloocc){
console.log(1);
}else{
console.log(2);
}*/
//等于,对象==对象字符串
/*var nlooc='ss';
var nloocc=['ss'];
if(nlooc==nloocc){
console.log(1);
}else{
console.log(2);
}*/
//等于,对象==对象数字
/*var nlooc=1;
var nloocc=[1];
if(nlooc==nloocc){
console.log(1);
}else{
console.log(2);
}*/
//严格等于
/*var xl=1;
var xll=1;
if(xl===xll){
console.log(2);
}*/
//严格等于 对象不是用值比较,用引用比较
/*var xl=[1,2];
var xll=[1,2];
if(xl===xll){
console.log(2);
}
*/
//严格等于 对象不是用值比较,用引用比较
/*var xl={};
var xll={};
if(xl===xll){
console.log(2);
}*/
//严格等于 nan不严格等于
/*var Month=30;
var Month1=20;
if (Month < 1 || Month > 12)
{
Month = Number.NaN;
Month1 = Number.NaN;
}
console.log(Month);
console.log(Month1);
if(Month===Month1){
console.log(2);
}*/
//包装对象
//基本字符串类型
/*var str='string';
console.log(str);*/
//基本字符串类型
//var str='string';
//str.t=9;
//console.log(str.length);
//console.log(str.l);
//对象类型 string类型包装类
/*var strobj=new String('string')
console.log(strobj);*/
//类型检测
//判断基本字符串string
/*var hh='jj';
console.log(typeof(hh));*/
//判断基本数字number
/*var hh=0;
console.log(typeof(hh));*/
//判断函数对象function
/*var hh=function(){
alert(1)
};
hh()
console.log(typeof(hh));*/
//判断未定义 undefined
/*var hh;
console.log(typeof(hh));*/
//判断对象 object
/*var hh={a:[1,2],b:2};
var hhc=new Object();
hhc.aa=new Array();hhc.aa[0]='1f';hhc.aa[1]='ddf';
hhc.bb=2;
console.log(hh.a);
console.log(hhc.aa[0]);
console.log(typeof(hh));*/
//判断数组 object array
/*var hh=['d','f','g'];
console.log(hh);
console.log(typeof(hh));*/
//判断NAN number
/*var hh=NaN;
console.log(hh);
console.log(typeof(hh));*/
//判断NALL null object
/*var hh=null;
console.log(hh);
console.log(typeof(hh));*/
//判断对象是否为 string类型包装类
/*var ju=new String();
console.log(ju instanceof String);*/
//判断对象是否为 function 函数对象function
/*function Person(){}
var ju=new Person();
console.log(ju instanceof Person);*/
//判断是否是数组
/*var hhc=new Array();hhc[0]='1f';hhc[1]='ddf';
console.log(hhc);
console.log(Object.prototype.toString.apply(hhc));*/
//判断是否是函数
/*var hhc=function(){}
console.log(hhc);
console.log(Object.prototype.toString.apply(hhc));*/
//判断是否是空
/*var hhc=null;
console.log(hhc);
console.log(Object.prototype.toString.apply(hhc));*/
//判断是否是未定义
/*var hhc;
console.log(hhc);
console.log(Object.prototype.toString.apply(hhc));*/
/*var cases=[{
arr1:[1,true,null],
arr2:[null,false,100],
expect:true
},{
arr1:[function(){},100],
arr2:[100,{}],
expect:false
}]
console.log(cases);*/
/*var cases=[{
arr1:[1,true,null],
arr2:[null,false,100],
expect:true
},{
arr1:[function(){},100],
arr2:[100,{}],
expect:false
},{
arr1:[null,999],
arr2:[{},444],
expect:false
},{
arr1:[window,1,true,new Date(),"hahaha",(function(){}),undefined],
arr2:[undefined,(function(){}),"okokok",new Date(),false,2,window],
expect:true
},{
arr1:[new Date()],
arr2:[{}],
expect:false
},{
arr1:[window],
arr2:[{}],
expect:false
},{
arr1:[undefined,1],
arr2:[null,2],
expect:false
},{
arr1:[new Object,new Object,new Object],
arr2:[{},{},null],
expect:false
},{
arr1:null,
arr2:null,
expect:false
},{
arr1:[],
arr2:undefined,
expect:false
},{
arr1:"abc",
arr2:"cba",
expect:false
}];
for(var i=0;i<cases.length;i++){
console.log(cases[i]);
}*/
//Object.prototype.toString.apply([])
//console.log(nloo);
</script>
<body>
</body>
</html>
案例
html
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb18030">
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
/*
* param1 Array
* param2 Array
* return true or false
*/
function arraysSimilar(arr1, arr2){
if(arr1 instanceof Array && arr2 instanceof Array){
var key1 = [],key2 = [],len = arr1.length,len2=arr2.length;
// 数组的长度相等判断
if(len!=len2){return false;}
// 类型相同判断
if(len){
// 获取类型列表
for(var i= 0;i<len;i++){
// 数组1的类型列表字串
var item1 = arr1[i], typeFirst = typeOf(item1);
if(key1.join().indexOf(typeFirst)<0){
key1.push(typeFirst);
}
// 数组2的类型列表字串
var item2 = arr2[i],typeSecond = typeOf(item2);
if(key2.join().indexOf(typeSecond)<0){
key2.push(typeSecond);
}
}
key1 = key1.sort();
key2 = key2.sort();
// 类型字串比较
if(key1.join() == key2.join()){
return true;
}else{
return false;
}
}else{
// 空数组相等
return true;
}
}else{
// 非数组
return false;
}
}
/**
* 类型判断方法
* param item
* return type(string,function,boolean,number,undefined,null,window,Date,Array,object)
*/
function typeOf(item){
var type = typeof item;
if(type != "object"){
// 判断基本类型string,function,boolean,number,undefine
}else if(item === null){
// check null
type = "null";
}else if(item === window){
// check window
type ="window";
}else{
// 判断object类型object,date,array
if(item instanceof Date){
type = "date";
}else if(item instanceof Array){
type = 'array';
}else{
type = 'object';
}
}
return type;
}
</script>
<script src="testData.js"></script>
</body>
</html>
<!--js-->
var result=function(){
//以下为多组测试数据
var cases=[{
arr1:[1,true,null],
arr2:[null,false,100],
expect:true
},{
arr1:[function(){},100],
arr2:[100,{}],
expect:false
},{
arr1:[null,999],
arr2:[{},444],
expect:false
},{
arr1:[window,1,true,new Date(),"hahaha",(function(){}),undefined],
arr2:[undefined,(function(){}),"okokok",new Date(),false,2,window],
expect:true
},{
arr1:[new Date()],
arr2:[{}],
expect:false
},{
arr1:[window],
arr2:[{}],
expect:false
},{
arr1:[undefined,1],
arr2:[null,2],
expect:false
},{
arr1:[new Object,new Object,new Object],
arr2:[{},{},null],
expect:false
},{
arr1:null,
arr2:null,
expect:false
},{
arr1:[],
arr2:undefined,
expect:false
},{
arr1:"abc",
arr2:"cba",
expect:false
}];
//使用for循环, 通过arraysSimilar函数验证以上数据是否相似,如相似显示“通过”,否则"不通过",所以大家要完成arraysSimilar函数,具体要求,详见任务要求。
for(var i=0;i<cases.length;i++){
if(arraysSimilar(cases[i].arr1,cases[i].arr2)!==cases[i].expect) {
document.write("不通过!case"+(i+1)+"不正确!arr1="+JSON.stringify(cases[i].arr1)+", arr2="+JSON.stringify(cases[i].arr2)+" 的判断结果不是"+cases[i].expect);
return false;
}
}
return true;
}();
document.write("判定结果:"+(result?"通过":"不通过"));
- JavaScript深入浅出(一)数据类型
- JavaScript深入浅出——数据类型(一)
- JavaScript 深入浅出 1 数据类型
- JavaScript深入浅出学习笔记(一)—数据类型
- 深入浅出JavaScript (一)初识
- 深入浅出JavaScript (一)初识
- 【深入浅出】JavaScript (一)初识
- 深入浅出JavaScript (一)初识
- 一 JavaScript之数据类型
- javaScript (一)数据类型
- JavaScript数据类型(一)
- JavaScript学习(一) 数据类型
- 【慕课学习】《JavaScript深入浅出》——javascript数据类型
- javascript基础笔记一数据类型
- JavaScript基础(一)--数据类型
- 深入浅出ArcGIS 9.3 Javascript API(一)
- 深入浅出 Javascript API(一)--基本框架
- 深入浅出 Javascript API(一)--基本框架
- 范德萨
- ANSYS使用笔记(一):开始使用
- Leetcode 196. Delete Duplicate Emails
- js 声明数组和向数组中添加对象变量
- Leetcode 197. Rising Temperature
- JavaScript深入浅出(一)数据类型
- 51Nod-1282-时钟
- JavaScript数组和循环
- TortoiseGit状态图标不正常或不显示的问题修复
- 大数据成果(发展和未来)
- 树形dp(Centroids,cf 708C)
- Python常见数据结构整理
- 关系数据库的第一第二第三范式
- 机器学习部分国内牛人