js基础语法一
<html>
<head>
<title>javascript01.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
//js是动态语言,java是静态语言,js是基于原型拷贝的,没有类的概念,只有一个根对象,通过根对象来拷贝不同的副本来产生对象。
console.log(1234);
//对于js而言,都是通过var来完成变量的创建,等号后面是什么就是什么类型
var a = 12; //Number
console.log(a);
console.log(typeof a);
a = 'abc'; //String
console.log(a);
console.log(typeof a);
function fn1(){
var b = 10;
//函数内部没有用var来申明的时候,就会成为全局的变量
c = 11;
console.log(b);
}
function fn2(){
console.log(c);
}
console.log(typeof fn1);
//在方法fn1中有个全局的变量c,可以在fn2中输出,但是前提条件是先执行了fn1,让c变量创建之后再调用fn2.如果直接调用fn2,则会说c没有定义
fn1();
fn2();
//变量的类型,常用的类型有Number,String,Date,Array,Boolean,Math等
var d = 10.2;
console.log(typeof d);
a = '11';
//a为String,d为Number。如果要进行计算则需要进行强制类型转换
console.log(d+a); //输出10.211
console.log(d+Number(a)); //21.2 注意在js中强制转换时Number(a) 而java中是(int)a
//如果强制转换一个非数字的值为Number,会得到一个NaN(not a number)值
a = 'abc';
console.log(Number(a));
//使用parseInt可以将字符串开头的几个数字转换为int,但如果开头不是数字,就会得到NaN
a = '12px';
console.log(parseInt(a));
console.log(parseInt('t123px'));
console.log(parseInt('12.5px'));
//对于数组等对象而言,显示的结果就是object,不会显示Array,如果想测试是否是Array的实例用instanceof
var aa = ['a','bc',1,3,'2'];
console.log(typeof aa);
console.log(aa instanceof Array);
//布尔类型:true和false,在js中非0为true。特别注意:0,NaN,undefined,null也为false
var size = '';
var size1;
var size2 = null;
console.log(!!size); //两个!就是本身的boolean值
console.log(!!0);
console.log(!!'abc');
console.log(!!123);
console.log(!!-1);
console.log(size1);
console.log(!!size1);
console.log(!!size2);
</script>
</head>

被折叠的 条评论
为什么被折叠?



