js基础(一):ECMAScript,DOM,BOM,变量类型,类型转换,变量命名法

ECMQAScript: 这是js的核心,相当于java的虚拟机,负责对代码进行解释

DOM: DocumentObject Model,表示js操作HTML的能力。

BOM: Broswer Object Model,表示js操作浏览器的能力

变量:

1. js中对变量类型的判断,typeof(a);

2. 一个变量在没有赋值之前,类型是不确定的。

字符串与数字的转换:

1.字符串与整数转换

主要的方法:

parsenInt() 字符串转成整数

isNAN()判断一个字符串是不是数字

例如实现两个字符串求和:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>字符串转成整数</title>
    <script>
        window.onload = function () {
            var oInput1 = document.getElementById('input1');
            var oInput2 = document.getElementById('input2');
            var oBuuton1 = document.getElementById('button1');
            oBuuton1.onclick = function () {
                if (isNaN(oInput1.value) == true) {
                    alert('请在第一个框输入数字');
                } else if (isNaN(oInput2.value) == true) {
                    alert('请在第二个框输入数字');
                } else {

                    var iAnswer = parseInt(oInput1.value) + parseInt(oInput2.value);
                    alert(iAnswer);
                }
            }

        }

    </script>
</head>
<body>
<input type="text" id="input1">
<input type="text" id="input2">
<input type="button" id="button1" value="求和">
</body>
</html>

2.字符串与小数转换

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>字符串转成整数</title>
    <script>
        window.onload = function () {
            var oInput1 = document.getElementById('input1');
            var oInput2 = document.getElementById('input2');
            var oBuuton1 = document.getElementById('button1');
            oBuuton1.onclick = function () {
                if (isNaN(oInput1.value) == true) {
                    alert('请在第一个框输入数字');
                } else if (isNaN(oInput2.value) == true) {
                    alert('请在第二个框输入数字');
                } else {

                    // var iAnswer = parseInt(oInput1.value) + parseInt(oInput2.value);
                    var iAnswer = parseFloat(oInput1.value) + parseFloat(oInput2.value);
                    alert(iAnswer);
                }
            }

        }

    </script>
</head>
<body>
<input type="text" id="input1">
<input type="text" id="input2">
<input type="button" id="button1" value="求和">
</body>
</html>

“==”和“===”的区别:

“==”先转换成同类型在比较

“===”不转换类型直接比较

注:两个字符串相减的问题

两个字符串相减会先转换类型在相减

匈牙利命名法:

类型前缀:

首字母大写 

猜你喜欢

转载自blog.csdn.net/psjasf1314/article/details/124165675