Basic javascript [1] _ PHP Tutorial

Source: Internet
Author: User
Javascript basics [1 ]. Javascript basics [1] 2015111316: 10: 04javascript was under debate since its birth, but this still does not affect its becoming the mainstream language of WEB programming today. Initial javascript basics [1]

Javascript was under debate since its birth, but it still does not affect it as the mainstream WEB programming language today. The original javascript design was used to provide dynamic functions such as data interaction, screen rendering, and session authentication on the browser side. now, the popularity of node. js has extended javascript to the server side.

Javascript, as a weak scripting language, is not complex in syntax. as programmers of the present age, no matter whether WEB development is in progress or not, it is easy to understand javascript. So today, let's get to know javascript!

1. what is javascript?

Javascript is a dynamic scripting language used for Web application development. The main function is to add dynamic behavior effects to pages, specifically:

  • Embed dynamic text into HTML pages;
  • Respond to browser events (javascript is an event-related language that can respond to user clicks, moves, and other actions );
  • Read and write HTML elements (such as form submission );
  • Verify the data before the data is submitted to the server;
  • Checks visitor's browser information;
  • Control cookies, including creation and modification;
  • Server-side programming based on Node. js technology;

It can be said that javascript is a literal translation scripting language programmed on the client for the dynamic behavior of HTML pages, making Web browsers easier than simply displaying user pages. However, because javascript is deployed on the client, its security has always been the focus of attention.

II. javascript debugging

The execution of javascript scripts is mainly implemented through the parsing engine developed by various browser vendors. Existing mainstream javascript parsing engines include: Chrome V8 engine, IE9 JS engine, and Firefox TraceMonkey;

For javascript development, we often get used to an IDE similar to VS that can be used directly, but javascript itself is aLightweight"Language, so we only need simpleText editor + javascript parsing engineYou can perform development and debugging.

Of course, you can use notepad in Windows or the Vim editor in Linux, but I suggest you use a special code editor, because it has many conveniences such as syntax highlighting and automatic completion. Here, the text editor I use isNodepad ++The combined debugger is Firefox, and you can also use IE, Chrome or even Safri, because nodepad ++ supports debugging in multiple browsers.

In Notepad ++,Run"Under the menu, you can choose which browser to run the debugging javascript script, or you can use the shortcut key above.

III. javascript testing program

Next, we will provide a very simple javascript script, where the knowledge points involved will be explained one by one.

The first is our javascript script:Program. js. let's take a look at the code:

  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. }

Row 1st: both document. writeln () and document. write () are used to output information to the terminal (browser). The difference is that the "ln" version comes with a line break;
Row 3rd: defines a variable without declaring its type. you only need to use the keyword "var;
Row 4th: the basic control structures such as if-else, while, and for can still be used in javascript. the javascript numbers are represented in a 64-bit floating point, therefore, 1.0 and 1 indicate the same value. In addition, NaN indicates an operation result that cannot generate normal results. all values greater than 1.798e308 are expressed in Infinity, e308 indicates the 308 power of 10. Therefore, when the input value is greater than the defined value, it is displayed as Infinity;

  1. var a = 10, 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. }

  11. document.writeln(add(a,b));

Line 3 defines a simple function. one is addition (add), the other is subtraction (subject), and the function is called when the terminal outputs the function;

  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 functions do not have a local region, they are often used. Everyone with programming experience knows that global variables are very confusing. we recommend that you create a global object at the beginning, just as in this article, and then all the operations are part of it, that is to say, the global object we define is actually a "global container ".
In javascript, simple data types include numbers, strings, boolean values, null values, and undefined values. All other values are objects, such as arrays, functions, and regular expressions. In short, objects in javascript are variable keyed collections ). An object consists of different attributes. the attribute name can be any string including an empty string, and the attribute value can be any value except undefined.

Row 3 initializes an empty global object MyObj;
4th add an attribute member to the behavior object MyObj, and member is an object that contains two key-value pairs. each key-value pair is separated by commas (,). the last attribute does not need to be signed;
The attribute name of line 7-8 is a string. we recommend that you use the javascript identifier specification (numbers, letters, and underscores, with the first character only letters ), in this case, "" can be omitted to represent the string. for example, "first-name" cannot be omitted, but can be written as first_name, the reference symbol ". ", such as MyObj. member. last_name, rather than standard identifiers, can only use MyObj. member. ["first-name"], very troublesome;

  1. // Update the object property value
  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. {
  11. Document. writeln (name + ':' + MyObj. record [name]);

Row 3rd uses typeof to obtain the object type. The value of the typeof operator is only "string", "boolen", "undefined", "function", and "object;
Row 3 updates the existing property directly. if the property already exists, it is updated. if the property does not exist, the property key-value pair is created;
Row 3 shows all attributes of an object. with the for-in structure, we can enumerate all attributes (including attributes in functions and prototypes ), the obtained enumeration is not necessarily sequential. Therefore, we recommend that you use the for () format to specify the traversal mode;

Next is ourProgram.html


  • The key here is that a javascript script is embedded in row 3,
    Lines 3 tell the browser to display the source code;
    Finally, let's look at the results:
    Comments and corrections are welcome! Refer: the essence of Javascript language, Douglas Crockford, electronics industry Press

    16:10:04 javascript was under debate since its birth, but it still does not affect its entry into the mainstream WEB programming language today. The initial javascript settings...

    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: 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.