JavaScript core Syntax Summary (recommended) _ Basics

Source: Internet
Author: User
Tags function definition

This article introduces the basic (commonly used) grammar of JavaScript for your reference.

Overview

Let's talk about how JavaScript should be written. Two forms, one is to write JavaScript directly in the HTML page, the other is to exist in the *.js file and then referenced in the page.

Write directly, that is, can be placed in

<body>
<script>
   document.writeln ("haha <br/>");
</script>
</body>

Reference mode

<script type= "Text/javascript" src= "Js_css/menutree.js" ></script>

Execution order and life period

JavaScript is executed/parsed from top to bottom. When invoking a function or, it must be guaranteed that the function has been parsed/declared. This is also the general reason to put the function definition in

All function and variable are valid only on this page, and when you leave the page, you cannot access the variables or functions declared in the previous page. If you must visit, you can save it with a cookie and read it on the next page.

Syntax-variables

The variable definition begins with Var, and you can define a variable. In the body of the function is considered a local variable, the outside is the global variable. It should be noted that JavaScript syntax is very messy, and there are other ways to define variables. But I hope you do not use other messy grammar , use this most common syntax can be enough, and save brains.

About variables we need to understand

(1) The variable is weakly typed, and an integer can even be added directly to the string to get a string

(2) The so-called global variables, but also only in the current page is valid, when the page is unload to another page, all variables are destroyed. These global variables are different from the inline variables of the browser (also known as the user Agent), and each page automatically has window, document these inline objects.

(3) Do not distinguish between single or double quotes

<script>
  //variable definition
  var nnumofbytes = ten;
  var fpercent = 0.32;
  var stryourname = ' Mr.known ';
  var prect = new Object ();

</script>

Syntax-function

<script>
  function Gotourl (strurl)
  {
    location.href = strURL;
  }
</script>

Functions can be invoked in HTML events/Form/javascript, such as:

HTML element Event callback:

<body onload= "Gotourl (' http://www.jb51.net ')" >

Form Element Event Callback

<select name= "somelist" onchange= "onsomethingchanged ()" >

Hyperlink destination

<a href= "Javascript:gotourl (' www.jb51.net ')" > Go </a>

Call directly in JavaScript

<script>
    gotourl ("www.jb51.net");
</script>

Syntax-Class

General JavaScript is not required to make custom classes, that is, generally do not reach such a complex degree. Because it's a scripting language, just write a few sentences. One is difficult to debug, two is inconvenient to read. But if you have to write a complex function, such as a menu tree, then you have to write a little class to complete.

Similarly, class in JavaScript also has 2 kinds of definition format, we will be the following format, the other is bad format, or do not know for good.

Let's start with a constructor without a parameter:

<script>
  function SampleClass ()
  {
    //variable variable definition
    This.nid = ten;
    
    member function definition
    this.plusid = function (nplus)
    {return
      This.nid + nplus;
    }
  }
</script>

Another class that makes arguments

function MenuItem (L, h, T)
  {
    This.label = l;
    This.href = h;
    This.target = t;
    
    this.tohtml = function ()
    {
      var html = ' <a href= ' + this.href + ' ';
        
      if (this.target!= null)
        HTML = = ("target=" + This.target + "");
        
      HTML + + ">" + This.label + "</a>";      
      return html;
    }
  

To create an instance of a class

var item = new MenuItem ("haha", http://www.jb51.net, NULL);

For a class, note the following points:

(1) Do not write a class from scratch, please copy the past from me to change, so that there will be no inexplicable trouble

(2) to refer to member variables in member functions, be sure to add this.

(3) member function if you want to return a value, you use returns, you want to go back to what type do not have to declare in advance, direct return is good

(4) For Programmers Learning C + + and Java, it's important to understand that var p = new SomeClass (); The new in this sentence is actually creating an object and returning its pointer

(5) Consider using the object class

Syntax-object class

JavaScript syntax is very messy, scripting language, do not require too high. Although you can customize a class, but in many cases you do not need to declare a class yourself, you can use the object class directly to define a struct without having to declare the form of the struct in advance.
Such as:

function CreateObject ()
{
  var obj = new Object ();
  obj.x = ten;
  OBJ.Y = one;
  return obj;
}

See, this function randomly creates an object, adds members x, Y, and does not notify anyone (no prototype declaration). And the user is also directly can call its o.x, O.Y, no need to guess.

<script>
  var o = CreateObject ();
  Document.writeln ("x=" + o.x + ", y=" + o.y);  
</script>

But for security reasons, you can write this:

var o = CreateObject ();

  if (o.x!= null && o.y!= null)
  {
    Document.writeln ("x=" + o.x + ", y=" + o.y);  

  }

Syntax-arrays Array

<script>
  //Create
  var a = new Array ();
  Example for
  (var i=0; i<a.length; i++)
  {
    var e =   a[i];  
  }
  Add
  var item = new MenuItem ("V");
  This.itemArray.push (item);
</script>

How do I delete an element? Remember to assign the value directly to null and then add it later.

Array as member variable

function Menu ()
{
  This.itemarray = new Array ();
 
  This.additem = function (L, h, T)
  {
    var item = new MenuItem (l,h,t);
    This.itemArray.push (item);}        

The above JavaScript Core grammar summary (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

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.