JavaScript core syntax Summary (recommended), javascript Core

Source: Internet
Author: User

JavaScript core syntax Summary (recommended), javascript Core

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

Overview

Let's talk about how to write Javascript first. One is to directly write JavaScript in the HTML page, and the other is to exist in the *. js file and then reference it in the page.

When writing directly, it can be placed in

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

Reference Method

<script type="text/javascript" src="js_css/menutree.js"></script>

Execution sequence and life cycle

JavaScript is executed/parsed from top to bottom. When calling a function, you must ensure that the function has been parsed/declared. This is also the reason that the function definition is generally placed in

All functions and variables are valid only on this page. After leaving the page, the variables or functions declared on the previous page cannot be accessed on the new page. If you want to access the service, you can use cookies to store them and read them on the next page.

Syntax-Variable

The variable definition starts with var and defines a variable. If it is placed in the function body, local variables are considered as global variables. It should be noted that the JavaScript syntax is messy, and variables also have other definition methods. However, I hope that you can use this most common syntax instead of using other messy syntaxes.

You need to understand the variables.

(1) variables are of a weak type. An integer can even be directly added to a string to obtain a string.

(2) global variables are only valid on the current page. When the page is jumped to another page by unload, all the variables are destroyed. These global variables are different from the embedded variables of the browser (also known as the User Agent). Each page automatically has embedded objects such as window and document.

(3) single or double quotation marks are not distinguished.

<Script> // variable definition var nNumOfBytes = 10; 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 called in HTML event/FORM/JavaScript, such:

HTML element Event Callback:

<Body onload = "goToUrl ('HTTP: // www.bkjia.com ')">

FORM element Event Callback

<Select name = "somelist" onchange = "onSomeThingChanged ()">

Hyperlink target

<A href = "javascript: goToUrl ('www .jb51.net ')"> go </a>

Directly calling in JavaScript

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

Syntax-class

General JavaScript does not need to make custom classes, that is, it generally does not reach such a complex level. Because it is a scripting language, just write a few words. One is difficult to debug, and the other is inconvenient to read. However, if you have to write complex functions, such as the menu tree, you have to write a class to complete it.

Similarly, there are two definition formats for classes in JavaScript. We can just use the following format. The other format is very bad, so do not know.

First, let's start with a non-constructor:

<Script> function SampleClass () {// variable defines this. nId = 10; // The member function defines this. plusId = function (nPlus) {return this. nId + nPlus ;}</script>

Another parameter creation class

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

Create a class instance

Var item = new MenuItem ("haha", http://www.bkjia.com, null );

Pay attention to the following points about the class:

(1) do not write a class from the beginning. copy the class from here and modify it again, so there will be no inexplicable troubles.

(2) When referencing a member variable in a member function, you must add this. Otherwise, it cannot be referenced.

(3) If a member function wants to return a value, use return. If you want to return any type, you do not have to declare it in advance. just return it directly.

(4) programmers studying C ++ and Java should understand var p = new SomeClass (); in this sentence, new is actually creating an object and returning its pointer.

(5) consider using the Object class

Syntax-Object class

The JavaScript syntax is messy. The scripting language should not be too demanding. Although you can customize a class, you do not need to declare a class for many cases. You can use the Object class to define a structure without declaring the structure in advance.
For example:

function createObject(){  var obj = new Object();  obj.x = 10;  obj.y = 11;  return obj;}

No, this function creates an object at will, adds members x and y to it, and does not notify anyone (no prototype Declaration ). The user can also directly call its o. x, o. y without speculation.

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

However, for the sake of security, you can also write it as follows:

var o = createObject();  if(o.x != null && o.y != null)  {    document.writeln("x=" + o.x + ", y=" + o.y);    }

Syntax-Array

<Script> // create var a = new Array (); // times for (var I = 0; I <. length; I ++) {var e = a [I];} // Add var item = new MenuItem ("000"); this. itemArray. push (item); </script>

How to delete an element? Remember to assign a value directly to null, and test 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 summary of the core JavaScript syntax (recommended) is all the content shared by the editor. I hope to give you a reference, and I hope you can provide more support for the help house.

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.