`

Chapter1 Introduction to JavaScript

阅读更多
1. JS is a high-level, dynamic, untyped interpreted programming
   language that is well-suited to object-oriented and functional
   programming styles.

2.
    // variable is a symbolic name for a value.
    var x;          // Declar a variable named x.
    x = 0;
    x

    x = 1;
    x = 0.01;
    x = "hello world";
    x = 'JavaScript';
    x = true;   // Boolean values.
    x = false;

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


3. 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 propery "fat" has value true.
    };
    // 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.lenght-1]  // => 7: the lastelement 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.lenght             // => 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.
    };
   


4.
    // Operators act on values (the operands) to produce a new value.
    // Arithmetic operators are the most common:
    3 + 2            // => 5: additon
    3 - 2 
    3 * 2
    3 / 2
    points[1].x - points[0].x      // => 1: more complicated operands work, too
    "3" + "2"                      // => "32": + adds numbers, concatenates strings
    
    // JavaScript defines some shorthand arithmetic operators
    var count = 0;              // Define a variable
    count++;                    // Increment the variable
    count--;                    // Decrement the variable
    count += 2;                 // Add 2: same as count = count + 2;
    count *= 3;                 // Multiply by 3: same as count = count * 3;
    count                       // => 6: variable names are expressions. too

    // Equality and relational operators test whether two values are equal, 
    // unequal, less than, greater than, and so on. They evaluate to true or false.
    var x = 2, y = 3;        // These = signs are assignment, not equality tests
    x == y                   // => false: equality
    x != y                   // => true: inequality
    x < y                    // => true: less-than
    x <= y
    x > y
    "two" == "three"      // => false: the two strings are different
    "two" > "three"       // => true: "tw" is alphabetically greater than "th"
    false == (x > y)      // => true: false is equal to false

    // Logical operators combine or invert boolean values
    (x == 2) && (y == 3)    // => true: both comparisons are true. && is AND
    (x > 3) || (y < 3)      // => false: neither comparison is true. || is or
    !(x == y)               // => true: ! inverts a boolean value
   


5.
    // Functions are parameterized blocks of JavaScript code that we can invoke.
    function plus1(x) { // Define a function named "plus1" with parameter "x"
        return x+1; // Return a value one larger than the value passed in
    } // Functions are enclosed in curly braces

    plus1(y) // => 4: y is 3, so this invocation returns 3+1

    var square = function(x) { // Functions are values and can be assigned to vars
        return x*x; // Compute the function's value
    }; // Semicolon marks the end of the assignment.
    square(plus1(y)) // => 16: invoke two functions in one expression
   




分享到:
评论

相关推荐

    JavaScript权威指南(第6版).JavaScript:The.Definitive.Guide

    Chapter 1 Introduction to JavaScript Core JavaScript Chapter 2 Lexical Structure Chapter 3 Types, Values, and Variables Chapter 4 Expressions and Operators Chapter 5 Statements Chapter 6 Objects ...

    JavaScript权威指南(第6版)

    Chapter 1 Introduction to JavaScript Core JavaScript Chapter 2 Lexical Structure Chapter 3 Types, Values, and Variables Chapter 4 Expressions and Operators Chapter 5 Statements Chapter 6 Objects ...

    JavaScript.Novice.to.Ninja.2nd.Edition

    Comprehensively updated to cover ECMAScript 6 and modern JavaScript development, the second edition of this step-by-step introduction to coding in JavaScript will show you how to to solve real-world ...

    Beginning.JavaScript.5th.Edition

    Chapter 1: Introduction To Javascript And The Web Chapter 2: Data Types And Variables Chapter 3: Decisions And Loops Chapter 4: Functions And Scope Chapter 5: Javascript-An Object-Based Language ...

    Data Visualization with Python and JavaScript.azw3

    Chapter 12 Introduction to NumPy Chapter 13 Introduction to Pandas Chapter 14 Cleaning Data with Pandas Chapter 15 Visualizing Data with Matplotlib Chapter 16 Exploring Data with Pandas Chapter 17 ...

    Eloquent JavaScript(NoStarch,2ed,2014)

    JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible,...

    JavaScript.Application.Design.A.Build.First.Approach

    Chapter 1 Introduction To Build First Chapter 2 Composing Build Tasks And Flows Chapter 3 Mastering Environments And The Development Workflow Chapter 4 Release, Deployment, And Monitoring Part 2 ...

    Learning.Information.HTML5.Mobile.for.Android.and.iOS.epub

    Chapter 3 Introduction to CSS3 Chapter 4 Meta Tags and Media Queries Chapter 5 HTML5 Mobile Apps with PhoneGap Chapter 6 HTML5 Media and Device Access Chapter 7 Design, Debug, and Test Mobile Apps ...

    Learning.React.Native.Building.Native.Mobile.Apps.with.JavaScript.149192

    By bringing the advantages of ReactJS to mobile, React Native transforms every web developer into a...Chapter 1. Introduction Chapter 2. Getting Started Chapter 3. Components for Mobile Chapter 4. Styles

    Learning Web Design: A Beginner’s Guide to HTML... ..., 5th Edition

    Introduction to JavaScript D22. Using JavaScript Part V. Web lmages D23. Web lmage Basics D24. Image Asset Production D25. SVG Part Vl. Appendices A. Answers B. HTML5 Global Attributes C. CSS ...

    ASP.NET.MVC.5.with.Bootstrap.and.Knockout.js.1491914394

    Chapter 1. Introduction to MVC Chapter 2. Introduction to Bootstrap Chapter 3. Introduction to Knockout.js Chapter 4. Working with a Database Part II. Working with Data Chapter 5. Listing, Sorting, ...

    Javascript Manual Of Style

    Chapter 1is a quick introduction to JavaScript that describes what it is and where it came from. Chapter 2explains what JavaScript is good for, outlining what you can do with it. Chapter 3lets me get...

    JavaScript and Open Data 2018 pdf

    Chapter 1. Variables: Declaration, Definition and Type Chapter 2. Controls: Booleans, Branch and Loops Chapter 3. Data: Numbers and Strings Chapter 4. Objects and Prototypes Chapter 5. Arrays Chapter...

    JavaScript for kids

    JavaScript for Kids is a lighthearted introduction that teaches programming essentials through patient, step-by-step examples paired with funny illustrations. You’ll begin with the basics, like ...

Global site tag (gtag.js) - Google Analytics