This article introduces the basic (common) syntax of JavaScript for your reference.
Overview
Let me first talk about how to write Javascript. There are two forms, one is to write JavaScript directly in the HTML page, and the other is to exist in the *.js file and then reference it in the page.
When writing directly, you can put it in <head> </head> or <body> </body>. Generally, functions are placed in the <head> area, and the specific execution code is in the <body> area. For example, write in the body:
<body><script> document.writeln("haha <br/> ");</script></body>Quote method
<script type="text/javascript" src="js_css/menutree.js"></script>
Execution order and life cycle
JavaScript is executed/parsed in sequence from top to bottom. When calling a function or function, it must be ensured that the function has been parsed/declared. This is also the reason why function definition is generally placed in <head>.
All functions and variables are only valid in this page. After leaving the page, the variables or functions declared in the previous page cannot be accessed in the new page. If you have to visit, you can save it with cookies and read it on the next page.
syntax - variables
The variable definition starts with var, and you can define a variable. The ones placed in the function body are considered local variables, while the ones outside are global variables. It should be pointed out that the syntax of JavaScript is very messy, and there are other ways to define variables. But I hope everyone will not use other messy syntaxes, just use this most common syntax, which is enough and saves brainstorms.
Everyone needs to understand about variables
(1) Variables are of weak type, and an integer can even be added directly to a string to get a string
(2) The so-called global variables are only valid in the current page. When the page is unloaded and jumped to another page, all variables are destroyed. These global variables are different from the embedded variables of the browser (also known as User Agent). Each page automatically has embedded objects such as window and document.
(3) Do not distinguish between single quotes or double quotes
<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 events/FORM/JavaScript, such as:
HTML element event callback:
<body onload="goToUrl('//www.VeVB.COM')" >
FORM element event callback
<select name="somelist" onchange="onSomeThingChanged()">
Hyperlink target
<a href="javascript:goToUrl('www.VeVB.COM')" > go </a>
Call directly in JavaScript
<script> goToUrl("www.VeVB.COM");</script>syntax - class
Generally, JavaScript does not require custom classes, which means that it generally does not reach such a complex level. Because it is a scripting language, just write a few words. First, it is difficult to debug, and second, it is inconvenient to read. But if you have to write some complex functions, such as menu trees, you have to write some categories to complete it.
Similarly, classes also have two defined formats in JavaScript. We just need to know the following format. Another format is bad, so it is better not to know.
Let's first take one without constructor parameters:
<script> function SampleClass() { // Variable variable definition this.nId = 10; // Member function definition this.plusId = function (nPlus) { return this.nId + nPlus; } }</script>Another parameter-making 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 an instance of a class
var item = new MenuItem("haha", //www.VeVB.COM, null);
Regarding the class, please note the following points:
(1) Don't write a class from scratch. Please copy it from me and then modify it, so that there will be no inexplicable troubles
(2) To refer to member variables in member functions, be sure to add this., otherwise the reference will not be possible.
(3) If a member function wants to return the value, use return. If you want to return any type, you don’t have to declare it in advance. Just return it directly.
(4) For programmers who learn C++ and Java, you must understand that var p = new SomeClass(); the new in this sentence is actually to create an object and return its pointer.
(5) Consider using the Object class
syntax - Object class
JavaScript syntax is very chaotic, and the scripting language is not too high. Although you can customize a class, in most cases you don’t need to declare a class yourself. You can directly use the Object class to define a structure without declaring the form of this structure in advance.
like:
function createObject(){ var obj = new Object(); obj.x = 10; obj.y = 11; return obj;}Seeing that, this function randomly created an object, adding members x and y to it, and not notifying anyone (no prototype declaration). Users can also call their ox and oy directly without guessing.
<script> var o = createObject(); document.writeln("x=" + ox + ", y=" + oy); </script>But for safety reasons, it can also be written like this:
var o = createObject(); if(ox != null && oy != null) { document.writeln("x=" + ox + ", y=" + oy); }syntax - 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("000"); this.itemArray.push(item);</script>How to delete an element? Remember to directly assign the value to null, and then test it and then add it.
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 grammar of JavaScript (recommended) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.