zoukankan      html  css  js  c++  java
  • jsdefinitionguide0221

    1.1 Core JavaScript

    This section is a tour of the JavaScript language.

    Types, Values, and Variables

    // variable is a symbolic name for a value.
    // Variables are declared with the var keyword:
    var x; // Declare a variable named x.
    // Values can be assigned to variables with an = sign
    x = 0; // Now the variable x has the value 0
    x // => 0: A variable evaluates to its value.
    // JavaScript supports several types of values
    x = 1; // Numbers.
    x = 0.01; // Just one Number type for integers and reals.
    x = "hello world"; // Strings of text in quotation marks.
    x = 'JavaScript'; // Single quote marks also delimit strings.
    x = true; // Boolean values.
    x = false; // The other Boolean value.

    x = null; // Null is a special value that means "no value".
    x = undefined; // Undefined is like null.

     
     
    Two other very important types that JavaScript programs can manipulate are

    objects and arrays

    // JavaScript's most important data type is the object.
    // An object is a collection of name/value pairs, or a string to value map.
    var book = { // Objects are enclosed in curly braces.
    topic: "JavaScript", // The property "topic" has value "JavaScript".
    fat: true // The property "fat" has value true.
    }; // The curly brace marks the end of the object.
    // Access the properties of an object with . or []:
    book.topic // => "JavaScript"
    book["fat"] // => true: another way to access property values.
    book.author = "Flanagan"; // Create new properties by assignment.
    book.contents = {}; // {} is an empty object with no properties.
    // JavaScript also supports arrays (numerically indexed lists) of values:
    var primes = [2, 3, 5, 7]; // An array of 4 values, delimited with [ and ].
    primes[0] // => 2: the first element (index 0) of the array.
    primes.length // => 4: how many elements in the array.
    primes[primes.length-1] // => 7: the last element of the array.
    primes[4] = 9; // Add a new element by assignment.
    primes[4] = 11; // Or alter an existing element by assignment.
    var empty = []; // [] is an empty array with no elements.
    empty.length // => 0
    // Arrays and objects can hold other arrays and objects:
    var points = [ // An array with 2 elements.
    {x:0, y:0}, // Each element is an object.
    {x:1, y:1}
    ];
    var data = { // An object with 2 properties
    trial1: [[1,2], [3,4]], // The value of each property is an array.
    trial2: [[2,3], [4,5]] // The elements of the arrays are arrays.
    };

     

    2.1 Character Set

    2.1.1 Case Sensitivity

    E.x. online, Online, OnLine, and ONLINE are four distinct variable names.

     

    2.3 Literals

    { x:1, y:2 } // An object initializer
    [1,2,3,4,5] // An array initializer

     

    2.4 Identifiers and Reserved Words

    In JavaScript, identifiers are used to name variables andfunctions and to provide labels for certain loops in JavaScript code. A JavaScript identifier must begin with a letter, an underscore (_), or a dollar sign ($).

    These are all legal identifiers:

    i
    my_variable_name
    v13
     
    _dummy
    $str

    3.1 Numbers

    Unlike many languages, JavaScript does not make a distinction between integer values and floating-point values. All numbers in JavaScript are represented as floating-point values.

    3.1.5 Dates and Times

    var then = new Date(2010, 0, 1); // The 1st day of the 1st month of 2010
    var later = new Date(2010, 0, 1, 17, 10, 30);
    // Same day, at 5:10:30pm, local time
    var now = new Date(); // The current date and time

    3.10.2 Variables As Properties

    If you use var to declare the variable, the property that is created is nonconfigurable (see §6.7), which means that it cannot be deleted with the delete operator.

    var truevar = 1; // A properly declared global variable, nondeletable.
    fakevar = 2; // Creates a deletable property of the global object.
    this.fakevar2 = 3; // This does the same thing.
    delete truevar // => false: variable not deleted
    delete fakevar // => true: variable deleted
    delete this.fakevar2 // => true: variable deleted

    4.2 Object and Array Initializers

    [] // An empty array: no expressions inside brackets means no elements
    [1+2,3+4] // A 2-element array. First element is 3, second is 7

    4.3 Function Definition Expressions

    // This function returns the square of the value passed to it.
    var square = function(x) { return x * x; }

    6.4 Testing Properties

    var o = { x: 1 }
    "x" in o; // true: o has an own property "x"
    "y" in o; // false: o doesn't have a property "y"
    "toString" in o; // true: o inherits a toString property
  • 相关阅读:
    windows 下安装securecrt 绿色版
    对Linux命令进一步学习vim(二)
    提高php编程效率的小结
    javaScript 的小技巧
    常用 Git 命令文档和命令
    你 get 了无数技能,为什么一事无成
    Ubuntu14.4下安装FTP
    对Linux命令进一步学习
    可以输入也可以下拉选择的select
    APP接口基础学习一
  • 原文地址:https://www.cnblogs.com/TivonStone/p/2361924.html
Copyright © 2011-2022 走看看