JavaScript Basics "One" _php tutorial

Source: Internet
Author: User

JavaScript Basics "One"


2015/11/13 16:10:04

JavaScript has been debated since its inception, but it still doesn't affect the mainstream language of today's web programming. The original JavaScript was designed to provide dynamic features such as data interaction, screen rendering, and session authentication on the browser side, and today the popularity of node. js makes JavaScript extensible to the server side.

JavaScript as a weak type of scripting language, the syntax is not complex, as the present era of the program apes, whether or not web development, familiar with the understanding of JavaScript is not a harm. So today let's have a neat understanding of JavaScript!

First, what is JavaScript?

JavaScript is a dynamic scripting language dedicated to Web application development, and the main function is to add dynamic behavioral effects to the page, specifically:

  • Embed dynamic text into an HTML page;
  • respond to browser events (JavaScript is an event language that responds to the user's mouse clicks, moves, and so on);
  • read and write HTML elements (such as form submission, etc.);
  • Validate data before data is submitted to the server;
  • Detecting browser information for visitors;
  • control cookies, including creation and modification, etc.
  • server-side programming based on node. JS technology;

javascript script is implemented mainly through the analysis engine developed independently by the major browser vendors. The existing mainstream JavaScript parsing engine mainly includes: Chrome's V8 engine, IE9 's JS engine, and Firefox's tracemonkey;

for JavaScript development, we're often used to having an IDE like VS that can be used directly, but since JavaScript itself is a " lightweight "language, so we just need a simple The text editor + JavaScript parsing engine is ready for development debugging.

Of course, you can use Notepad under Windows or the Vim editor of Linux, but I recommend that you use a special code editor, because it has a lot of advantages, such as syntax highlighting, auto-completion, and so on. The text editor I'm using here is nodepad++, and the collocation of the debugger is a simple Firefox, of course you can also use IE, chrome or even Safri, because nodepad++ support a variety of browser debugging.

in the notepad++ " Run the menu to choose which browser to run the debug JavaScript script in, or use the shortcut keys above.

c JavaScript Test program

> Next, we'll provide a very simple JavaScript script that covers the knowledge points.

The first is our JavaScript script:program.js, let's look at a piece of code first:


 
 
  1. document.writeln (");
  2. document.writeln ("Hello, world!");
  3. var a = 100000000000000000000e400;
  4. if (A < Infinity)
  5. {
  6. document.writeln (a);
  7. document.writeln (' A less than Infinity, 3q~~ ');
  8. }
  9. else
  10. {
  11. document.writeln (a);
  12. document.write (' Sorry, a more than infinity!\n ');
  13. }

Line 1th: Document.writeln () and document.write () are used to output information to the terminal (browser), except that the "ln" version comes with line breaks;
Line 3rd: Define a variable, no need to declare its type, just use the keyword "var";
Line 4th: The basic control structure can still be used in JavaScript, such as If-else, while and for, and the number of JavaScript is unified with a 64-bit floating point representation, So the values of 1.0 and 1 are the same, and Nan represents a result of an operation that does not produce normal results, and all values greater than 1.798e308 are uniformly represented by infinity, and e308 represents the 308 power of 10, so when the value entered is greater than the defined value, the uniform display is infinity;


 
     
     
  1. var a = ten, B = 9;

  2. Document.writeln (a);

  3. function add (x, y)
  4. {
  5. return x + y;
  6. }

  7. function subtract (x, y)
  8. {
  9. return x-y;
  10. }


Line 5th begins by defining a simple function, one is addition, the other is subtraction (subject), and the function is called when the terminal outputs;


    
    
  1. Document.writeln ("Global Object ...");
  2. var MyObj = {};

  3. Myobj.member = {' First-name ': "Alice", last_name: "Winston"};

  4. Myobj.record = {
  5. Airline: ' T2B ',
  6. number:777,
  7. Departure: {
  8. Date: "Sunday",
  9. Time: "2015-11-01",
  10. City: "Taiwan"
  11. },
  12. Arrival: {
  13. Date: ' Monday ',
  14. Time: "2015-11-02",
  15. City: "Beijing"
  16. }

  17. };

  18. Document.writeln ("retrive a Non-exit attribute value: Exa. Myobj.people. ");
  19. Document.writeln (myobj.people);

  20. Document.writeln ("typeof Myobj.member is ...");
  21. Document.writeln (typeof Myobj.member);

  22. Document.writeln ("MyObj.record.number is ...");
  23. Document.writeln (typeof MyObj.record.number);

  24. Document.writeln (' MyObj.record.airline is ... ');
  25. Document.writeln (typeof MyObj.record.airline);

Because JavaScript has no local domain in its functions, the global domain is often used. Everyone with programming experience knows that global variables are very confusing, so it is recommended to create a global object at the beginning, as in this article, and then all operations as part of it, meaning that the global object we define is actually a "global container".
The simple data types in JavaScript include numbers, strings, booleans, null values, and undefined five, all of which are objects, such as arrays, functions, and regular expressions. In short, the object in JavaScript is a mutable keyed collection (keyed collections). Objects consist of different properties whose names can be any string including an empty string, and the property value can be any value other than undefined.

The 2nd line Initializes an empty global object myobj;
The 4th Behavior Object MyObj Adds an attribute member, and member is an object that contains two key-value pairs, each key-value pair is delimited with ', ', and the last attribute is not signed;
The 7–8 property name is a string, it is recommended to use JavaScript identifier specification (numbers, letters and underscores, the first character can only be letters), because the write can be omitted "" to represent the string, such as "first-name" Cannot omit "", but can be written first_ In addition, the reference symbol "." Can be conveniently used when retrieving object properties. such as MyObj.member.last_name, rather than canonical identifiers can only be used with Myobj.member. ["First-name"], very troublesome;

 
     
     
  1. Update of object property values
  2. Document.writeln (' Attribute value update ... ');
  3. Document.writeln (' Once Date is ' + typeof MyObj.record.departure.date);

  4. MyObj.record.departure.Date = ' Saturday ';

  5. Document.writeln (MyObj.record.departure.Date);

  6. Object Enumeration
  7. Document.writeln (' Object enume ... ')
  8. var name;
  9. for (name in Myobj.record)
  10. {

Line 3rd uses typeof to get the type of the object, the value of the TypeOf operator is only "string", "Boolen", "undefined", "function" and "object" five;
The 5th line updates the existing property directly, if it already exists, updates it, or if not, creates the property key value pair;
Line 9th shows the enumeration of all the properties of an object, with the for-in structure, we can enumerate all the attributes (including functions and properties in the prototype), and the resulting enumeration is not necessarily sequential, so it is generally recommended to use a for () to specify the traversal mode;

Next up is our program.html.

    
    


  1.  



  • The key here is that a JavaScript script is embedded in line 4th.
    The 3rd line tells the browser to display according to the source code style;
    Finally, let's take a look at the results:
      The shortcomings welcome everybody to criticize correct!  


    http://www.bkjia.com/phpjc/1071441.html www.bkjia.com true http://www.bkjia.com/phpjc/1071441.html techarticle javascript Foundation "one" 2015/11/13 16:10:04 JavaScript has been debated since its inception, but it still doesn't affect the mainstream language of Web programming today. Initial JavaScript set ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.