美文网首页
一、 JavaScript 编程语法

一、 JavaScript 编程语法

作者: MC_DEV_JIN | 来源:发表于2016-05-23 09:01 被阅读34次
    JavaWeb

    (1)JavaScript语句组成的:

    Values, Operators, Expressions, Keywords, and Comments.

    值,运算符,表达式,关键字和注释。

    值:

    JavaScript的值>JavaScript的语法定义了两种类型的值:固定值和变量值。

    固定值称为文字。变量值被称为变量。

    备注:编程中,变量命名有三种:Hyphns、underscore、CamelCase

    在编程语言中,特别是在JavaScript中,命名通常始于一个小写字母:

    eg: firstName, lastName, masterCard, interCity.

    变量命名:连字符(Hyphens)是不允许在JavaScript。这是减法。

    (2)JavaScript使用Unicode字符集。

    (3)关键字:

    Keyword Description

    break Terminates a switch or a loop


    continue Jumps out of a loop and starts at the top


    debugger Stops the execution of JavaScript, and calls (if available) the debugging function


    do ... while Executes a block of statements, and repeats the block, while a condition is true


    for Marks a block of statements to be executed, as long as a condition is true


    function Declares a function


    if ... else Marks a block of statements to be executed, depending on a condition


    return Exits a function


    switch Marks a block of statements to be executed, depending on different cases


    try ... catch Implements error handling to a block of statements


    var Declares a variable


    (4)注释:

    单行注释://

    多行注释:/*...*/

    (5) JavaScript的数据类型

    var length = 16;                               // 数字

    var lastName = "Johnson";                      // 字符串

    var cars = ["Saab", "Volvo", "BMW"];           // 数组

    var x = {firstName:"John", lastName:"Doe"};    // 对象

    JavaScript 有动态数据类型

    var X ;

    var X=5 ;

    var X ="Jin";

    一个字符串(或一个文本字符串)是一系列的字符,如“美国能源部”。字符串是用引号写的。可以使用单引号或双引号:

    var carName = "Volvo XC60";   // 双引号

    var carName = 'Volvo XC60';   // 单引号

    在字符串中使用引号,只要不匹配字符串周围的引号:

    var answer = "It's alright";             // 单引号在双引号的里面

    var answer = "He is called 'Johnny'";    // 单引号在双引号的里面

    var answer = 'He is called "Johnny"';    // 双引号在单引号的里面

    JavaScript Booleans

    true false

    JavaScript 对象

    var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

    JavaScript typeof

    typeof "John"                // 返回字符串

    typeof 3.14                  // 返回数字

    typeof false                 // 返回 boolean

    typeof [1,2,3,4]             // 返回 object

    typeof {name:'John', age:34} // 返回 object

    区别 Undefined and Null

    typeof undefined             // undefined

    typeof null                  // object

    null === undefined           // false

    null == undefined            // true

    (6)Function

    允许 a function 无()的访问函数将返回函数定义:

    eg:

    function toCelsius(fahrenheit) {

    return (5/9) * (fahrenheit-32);

    }

    document.getElementById("demo").innerHTML = toCelsius;

    Functions 作为变量使用:

    eg:

    var text = "The temperature is " + toCelsius(77) + " Celsius";

    var x = toCelsius(32);

    var text = "The temperature is " + x + " Celsius";

    (7)JavaScript Objection

    object 创建

    eg:

    var car = "Fiat";

    var car = {type:"Fiat", model:"500", color:"white"};

    object 属性的两种访问方式:

    objectName.propertyName

    objectName[propertyName]

    object 方法的访问:

    objectName.methodName()

    不要申明Strings, Numbers, Boolean为对象!

    var x =newString();// Declares x as a String object

    var y =newNumber();// Declares y as a Number object

    var z =newBoolean();// Declares z as a Boolean object

    避免字符串、数字和布尔对象。它们使你的代码复杂化,并减慢执行速度。

    (8)JavaScript访问字符集

    全局变量:

    var carName =" Volvo";

    // code here can use carName

    functionmyFunction() {

    // code here can usecarName

    }

    自动全局化变量:

    // code here can use carName

    function myFunction() {

    carName ="Volvo";

    // code here can use carName

    }

    一般不要创建全局变量。

    相关文章

      网友评论

          本文标题:一、 JavaScript 编程语法

          本文链接:https://www.haomeiwen.com/subject/nmrarttx.html