js basics (1): ECMAScript, DOM, BOM, variable type, type conversion, variable nomenclature

ECMQAScript: This is the core of js, equivalent to the virtual machine of java, responsible for interpreting the code

DOM: DocumentObject Model, which represents the ability of js to manipulate HTML.

BOM: Broswer Object Model, which represents the ability of js to operate the browser

variable:

1. Judgment of variable type in js, typeof(a);

2. Before a variable is assigned a value, its type is undefined.

Conversion of strings to numbers:

1. String to integer conversion

The main method:

parseInt() converts a string to an integer

isNAN() determines whether a string is a number

For example, to implement the sum of two strings:

<!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. String and decimal conversion

<!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>

The difference between "==" and "===":

"==" is first converted to the same type before comparison

"===" does not convert the type and compares directly

Note: The problem of subtracting two strings

The subtraction of two strings will first convert the type before subtracting

Hungarian notation:

Type prefix:

capitalized 

Guess you like

Origin blog.csdn.net/psjasf1314/article/details/124165675