2018.12.24学习JavaScript

JavaScript:js是属于网络的脚本语言

三大引入方式

行内引入:

<a href="javascript:confirm('你确定要学习吗');">报名</a>
<div onclick="javascript:alert('请确定');">哈哈哈</div>

内部引入:

<script>
var test = null;
var test1 = undefined;
alert(null == undefined);
alert(test);
</script>

也可以写在<body></body>里

外部引入:

创建一个.js文件

下方引入

<script src="js/index.js" herf="text/javascript" charset="UTF-8"></script>

var 是用来声明变量的

var text(变量)=“aaa” 定义变量     变量的名称以字母或下划线开始,后面可跟数字

confirm 确认框

onclick 点击

alert 弹出

console.log(向控制台中书写内容)

通过isNaN(Not a Number)检测是否是NaN值  不是一个数值结果为true  是数值结果为false

如果浏览器出现:

Undefined  没有设值    Not defined  没定义变量

驼峰标记法:

firstName="aaa"  //小驼峰

FirstName="aaa"  //大驼峰

first_name     //下划线(可设多)

=  赋值  ==  判断(判断值)  ===  判断(判断值和数据类型)

var 变量=" 傻子\n别点了\r出去吧\t点确定"haha"\\n;"  \n  \r换行  \t 小表格(小空格)  \ 转义 后面不读

六大数据类型:字符串(String)、数字(Number)、布尔(Boolean)、Null、Undefined、对象(Object)。

if(exp)

    {exp为true的代码段;

}else{

    exp为false的代码段;

}判断是否存在变量

typeof得到变量的类型

强制转换:

alert(Number(undefined));转换成数字  得到NaN

alert(Number(null));转换成数字  得到0

a=0/0;  (Number(a));转换成数字  NaN

字符串  先转换布尔  转换数字 

+  不止有相加的意思,还有拼接的意思

隐式转换的特殊规则:

    1、null 和 undefined 是相等的。
    2、要比较相等性之前,不能将 null 和 undefined 转换成其他任何值。
    3、如果有一个操作数是 NaN,则相等操作符返回 false,而不相等操作符返回 true

     null == undefined  // true
        NaN == NaN         // false
        NaN != NaN         // true
        false == 0         // true
        undefined == 0     // true
        null == 0          // true
通过parseInt()进行转换成整形

注释:只有字符串中的第一个数字会被返回。

注释:开头和结尾的空格是允许的。

提示:如果字符串的第一个字符不能被转换为数字,那么 parseFloat() 会返回 NaN。

parseInt(string字符串,radix可选)后边一般两个值

test = parseInt(“324”);  //console.log(typeof test);  //324

test = parseInt(“324”,0);  //console.log(typeof test);  //324

test = parseInt(“012344”);  //console.log(typeof test);  //12344

test = parseInt(“365k8i23”);  //console.log(typeof test);  //365

test = parseInt(“true”);  //console.log(typeof test);  //NaN

test = parseInt(true);  //console.log(typeof test);  //NaN

test = parseInt("35 6 a");  //console.log(typeof test);  //35

test = parseInt("44.666");  //console.log(typeof test);  //44

通过parseFloat()转换成浮点型

注释:开头和结尾的空格是允许的。

提示:如果字符串的第一个字符不能被转换为数字,那么 parseFloat() 会返回 NaN。

提示:如果只想解析数字的整数部分,请使用 parseInt() 方法。

test = parseFloat("123.34.9abc");    //123.34

test = parseFloat("123");    //123

test = parseFloat("sdf");    //NaN

test = parseFloat("2e8a");    //200000000

alert(test);

猜你喜欢

转载自www.cnblogs.com/liuwei1/p/10170728.html