Skip to content

Commit 14825b5

Browse files
committed
数据类型
1 parent 9662fae commit 14825b5

19 files changed

Lines changed: 540 additions & 642 deletions
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
console.log(typeof "hello");//console.log()控制台输出内容
2+
console.log(typeof 222);//"number"
3+
console.log(typeof '222');//"string"
4+
console.log(typeof false);
5+
console.log(typeof "false");//这个上面的都是基本数据类型/
6+
console.log(typeof null);//object//object function 是引用数据类型;
7+
console.log(typeof {});
8+
console.log(typeof function () {});//function
9+
10+
//变量是数据的代言人;
11+
var demoStr="hello";
12+
console.log("-----------------------");
13+
console.log(typeof demoStr);//string
14+
demoStr=22;//不推荐这么做 ;
15+
console.log(typeof demoStr);//number
16+
17+
//typeof 总是返回string类型的; typeof()
18+
console.time("123");
19+
console.log("+++++++++++++++++++++");
20+
console.log(typeof (typeof "hello"));
21+
console.log(typeof (typeof 222));
22+
console.log(typeof typeof '222');
23+
console.log(typeof typeof false);
24+
console.log(typeof typeof "false");
25+
console.log(typeof typeof null);
26+
console.log(typeof typeof {});
27+
console.log(typeof typeof function () {});
28+
console.timeEnd("123");
29+
30+
console.assert(1==1,"打脸了");
31+
32+
var aloneVal=Symbol();
33+
console.log(typeof aloneVal);//symbol 存在的意义,避免多人开发时,变量覆盖;

数据类型初识/1.String.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//string 单引号'' / 双引号""包裹的内容
2+
/*alert(typeof "22");
3+
alert(typeof '22');
4+
alert(typeof "");
5+
alert(typeof '');*/
6+
console.log(typeof "22");
7+
console.log(typeof '2');
8+
console.log(typeof '');
9+
console.log(typeof "");
10+
console.log(typeof "false");
11+
12+
console.log(typeof "null");
13+
14+
//length,一般作为用户输入店名/用户名的长度限制;需要注意双字符精准度的问题;
15+
var str="123123123123123123123123123123";
16+
var chinaStr="哈喽";//双字节,不精确 4字节;
17+
console.log(str.length);
18+
console.log(chinaStr.length);//2
19+
20+
//字符串创建的原理;
21+
var str1="12345";//把"12345"这个字符串赋值给str1这个变量;
22+
str1="1234";//把"1234"这个字符串赋值给str1这个变量;
23+
/*
24+
* 并不是把"12345"修改为"1234"然后赋值给str1
25+
*
26+
* */
27+
console.log(str1);//"1234"
28+
console.log('1"23\'4\n56\'78');
29+
// alert("1213123\naaaaaa")
30+
31+
//toString方法;
32+
//22 - > "22"
33+
console.log(typeof 22);
34+
console.log(typeof (22).toString());
35+
var testStr=22+"";//隐式调用toString方法
36+
console.log(typeof 22+"");//+ - * / 2*2+10
37+
console.log(typeof testStr);//2*(2+10)
38+
39+
//eval()
40+
console.log("00000000000000000");
41+
console.log(eval("22+12"));//"22+12" -> 22+12 ->34
42+
43+
44+
//
45+
console.log("**********************");
46+
console.log(typeof (22+""));//string 22 -> "22"
47+
console.log(typeof (+"22"));//"22" -> 22 单目运算符+
48+
console.log([1,2,3,4]+"");//1,2,3,4
49+
50+
console.log("**********************");
51+
console.log("1.1" + 1.1);
52+
console.log(+"1.1" + 1.1);
53+
console.log((+"1.1") + (+1.1));

数据类型初识/2.Number.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//正数,负数,零,NaN都是属于Number
2+
// ***NaN -> Not a Number不是一个数,但是属于数字类型;**
3+
console.log(typeof 222);
4+
console.log(typeof 222.22);
5+
console.log(typeof -10);
6+
console.log(typeof -10.34);
7+
console.log(typeof 0);
8+
console.log(typeof -0);
9+
10+
//NaN
11+
console.log((22*"aa"));//NaN number
12+
console.log(typeof (22*"aa"));//number
13+
console.log(22==22);//true 单个等于号是赋值的意思; Invalid left-hand side in assignment
14+
//== 两个等于号才是判断;
15+
console.log((22*"aa")==(22*"bb"));//false
16+
console.log((22*"aa")==22);//false
17+
18+
//inNaN
19+
console.log(isNaN(22*"aa"));//true (22*"aa")计算出的值不是一个数,对吗? 对;
20+
console.log(isNaN(22));// 22这个值不是一个数,对吗? 不对; false (NaN -> Not a Number不是一个数,)
21+
22+
//number的转换方法; 别的数据类型转为数字类型;(不一定是字符串)
23+
/* 五种 转为数字的;
24+
* +"22" 字符串转为数字;
25+
* "22"-0
26+
* Number()
27+
* parseInt()
28+
* parseFloat()
29+
* */
30+
console.log("+++++++++++++++++++++++++");//隐式
31+
console.log(+"22");//隐式
32+
console.log(Number("22"));//显式 22
33+
console.log(Number(false));//显式 ->0
34+
console.log(Number(true));//显式 ->1
35+
console.log(Number(null));//显式 -> 0
36+
console.log(Number(undefined));//显式 NaN
37+
console.log(Number({}));//显式
38+
console.log(Number("22.22"));
39+
console.log(Number("22.22a"));//失败了 NaN
40+
41+
42+
console.log(parseInt("22.22a"));//22
43+
console.log(parseFloat("22.22a222"));//22.22
44+
console.log(parseFloat("22.22.22"));//22.22
45+
46+
// + - * /
47+
48+
console.log(("++++++++"));
49+
console.log(13%10); //13/10 3 多余的3 会被输出
50+
console.log(5%3);//2
51+
52+
var time=623648364569346563463;//time单位是秒;转为X分X秒;
53+
var minis=parseInt(time/60);//保存是分钟
54+
var second=time%60;//3
55+
var targetVal=minis+"分"+second+"秒";
56+
57+
console.log(" minis:"+minis+" second:"+second);
58+
console.log(targetVal);
59+
//小练习:
60+
/*
61+
* var time=623648364569346563463;//time是怎么得到的》当前的服务器时间 - 双11 这个阶段多少秒;
62+
* 是多少天,多少小时,多少分钟,多少秒;
63+
* */
64+
65+
66+
console.log("***********************");
67+
console.log("22"-1);//"22" -> 22 -> (22-1) -> 21
68+
console.log("22"-0);

数据类型初识/3.Boolean.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var a=52;
2+
var b=30;
3+
4+
function demoBoolean(a, b) {
5+
if (a>b){//2>30 -> false
6+
console.log(a+" 大于 " +b)
7+
}else if(a==b){//2==30 -> false
8+
console.log(a+" 等于 " +b)
9+
}else{
10+
console.log(a+" 小于 " +b)
11+
}
12+
console.log(a>b);
13+
console.log(a==b);
14+
15+
16+
if(null){
17+
console.warn("条件为真的时候才输出")
18+
}else{
19+
console.warn("if条件为假的时候才输出")
20+
}
21+
}
22+
23+
demoBoolean(a,b);
24+
/*
25+
* 如果a>B; ->"a的值"大于"b的值"
26+
* 如果a==B;->"a的值"等于"b的值"
27+
* 如果a<B;->"a的值"小于"b的值"
28+
*
29+
* */
30+
31+
32+
/* ................................非常重要。。。。。。。。。。
33+
* 哪些值是false(false本身)
34+
* 数字:0、-0 、NaN、
35+
* 字符串:''(空字符串)
36+
* null 、
37+
* undefined、
38+
* */
39+
40+
console.log("---------------------------------");
41+
console.log(Boolean(0)); //false
42+
console.log(Boolean(-0)); //false
43+
console.log(Boolean(1)); //true
44+
console.log(Boolean(-5)); //true
45+
console.log(Boolean("222"));//true
46+
console.log(Boolean('')); //false 空的字符串
47+
console.log(Boolean(" ")); //true 包含空格的字符串
48+
console.log("---------------------------------");
49+
console.log(Boolean("0"));//true
50+
console.log(Boolean(null));//false
51+
console.log(Boolean(undefined));//false
52+
console.log(Boolean({}));//true
53+
console.log(Boolean(function () {}));//true
54+
console.log(Boolean(false));//false
55+
console.log("---------------------------------");
56+
console.log(Boolean(55*"asd"));// NaN false
57+
58+
59+
// ! !!
60+
console.log("************************");
61+
console.log(Boolean(0));//false
62+
console.log(Boolean(!0));//true
63+
console.log(!0);//true 原理: 0 隐式的调用Boolean方法Boolean(0) -> false -> !false ->true
64+
console.log(!!0);// !(!false)->!true ->false
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// var NULL=2;
2+
console.log(typeof null);//object 这是一个空的指针;
3+
console.log(typeof NULL);// NULL- >undefined
4+
5+
//结果是undefined 除了用在typeof外,会报错;
6+
console.log(undefined*15);//NaN
7+
// console.log(undefined*aa);//aa is not defined
8+
// console.log(NULL+15);//NULL is not defined 相当于NULL 这个变量没有找到;
9+
10+
//
11+
var js;
12+
console.log(js);//undefined
13+
console.log(typeof undefined);//undefined
14+
console.log(typeof typeof undefined);//string
15+
16+
17+
//null和undefined比较
18+
/*
19+
* === 相同比较,首先判断是否是同一个类型;不是的话,直接false;
20+
* == 比较是否相等;(如果是不一样的类型,会转为相同的类型,然后再进比较)
21+
* */
22+
console.log(null == null);//true
23+
console.log(null == undefined);//true
24+
console.log(undefined == undefined);//true
25+
console.log(undefined === undefined);//true
26+
console.log(null === undefined);//false null->Null undefined -> Undefined
27+
console.log(false == 0);//true false->0 0==0? true
28+
console.log(Number("22") == 22);//true "22"隐士的调用Number->22 -> 22== 22 -> true
29+
30+
31+
console.log("---------------------------");
32+
console.log(null == 1);//false
33+
console.log(null == "2312312");//false
34+
console.log(null == false);//false
35+
console.log(null == 0);//false
36+
console.log(null == "");//false
37+
console.log(null == NaN);//false
38+
39+
console.log("++++++++++++++++++++++++++");
40+
console.log(undefined == 1);//false
41+
console.log(undefined == "2312312");//false
42+
console.log(undefined == false);//false
43+
console.log(undefined == 0);//false
44+
console.log(undefined == "");//false
45+
console.log(undefined == NaN);//false
46+
47+
48+
//应用场景经常出现在 做定时器的时候;
49+
var timer=null;
50+
51+
52+
53+
// 对象的属性
54+
var oDemo={
55+
// aaa:"这是oDemo的aaa值"
56+
};
57+
oDemo.bbb="Hello";
58+
59+
console.log(oDemo.aaa);//undefined
60+
console.log(oDemo.bbb);//Hello
61+
62+
63+
//ByIn
64+
var oDiv1=document.getElementById("div1"),
65+
oDiv2=document.getElementById("div2");
66+
console.log(oDiv1);
67+
console.log(oDiv2);//null
68+
69+
//function 返回值 默认是undefined;
70+
71+
function demo() {
72+
return;
73+
}
74+
var aDemo=demo();
75+
console.log(aDemo);//undefined

数据类型初识/5.Object.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
var num=2;
2+
console.dir(num);//number 实例
3+
console.dir(num.__proto__);//Number 类
4+
/*
5+
*
6+
* constructor :Object
7+
*
8+
*
9+
* */
10+
11+
var str="haha";
12+
console.dir(str);//string 实例
13+
console.dir(str.__proto__);//String 类
14+
15+
16+
/*判断是否一致*/
17+
console.log(num.__proto__.__proto__=== str.__proto__.__proto__);//true
18+
19+
20+
//万物皆对象,并不等于只有一个对象类型的;
21+
// Number / String / Object / Boolean
22+
23+
24+
//key:value
25+
//1、字面量的创建方式;推荐的方式
26+
var o={
27+
name:"zhu",
28+
say:"hello"
29+
};
30+
console.log(o);//{name: "zhu", say: "hello"}
31+
32+
var oDemo={};
33+
// oDemo.name1=
34+
if(!oDemo.flagVal){
35+
oDemo.flagVal="Test"
36+
}
37+
oDemo.name="bang";
38+
oDemo.say="hahahahahahahha";
39+
console.log(oDemo.flagVal);//Test
40+
console.log(oDemo);//{flagVal: "Test", name: "bang", say: "hahahahahahahha"}
41+
42+
//new
43+
var demo1=new Object();//不推荐的使用方法;
44+
demo1.haha="hahahahahahah";
45+
console.log(demo1);//{haha: "hahahahahahah"}
46+
47+
//jQuery的AJAX;
48+
/*var options={
49+
url:"babababab",
50+
type:"",
51+
date:{}
52+
};
53+
$.ajax(options);*/
54+
55+
56+
function Test(opt) {
57+
//bala bala
58+
}
59+
var testVal=Test({a:"",b:""});
60+
61+
//Array;
62+
var a=[1,2,3,4,5,6,7];//var a=[1,2,3,4,5,6,7,] 低版本浏览器,length的;
63+
console.log(a);//编程语言里,index是以0为开始的;
64+
console.log("第一位,a[0]",a[0]);
65+
console.log(a[1]);
66+
console.log(a.length);//7
67+
68+
// -> //

0 commit comments

Comments
 (0)