1. var msg;//Declare a variable. Before assigning a value to this variable, the variable is named: undefined
2. msg = "hello";//If var is not applicable, you can declare a global variable, but because this variable is global, it can easily cause some problems with other calls, so it is not recommended.
3. JavaScript has 5 basic data types, Undefined, Null, Boolean, Number and String; and a complex data type: Object
4. var num=076;//The octal system represented by it starts with 0. If the following value exceeds the range of 8, such as: 08, 079, etc., then analyze it in decimal system.
5. var num=0x22;//represents hexadecimal
6. NaN->Not a Number;//Non-number, NaN is not equal to any value, so it is self-contained; the way to determine whether a value can be converted into a numeric value: isNaN(*), * is the data to be converted
7. When converting to Number, false->0;true->1;""->0;"00022"->22;"helloworld"->NaN; Note: The Number() method is used
8. If the conversion is performed using parseInt() method, it is different from 7 (for strings). The basic principle is as follows:
1) Parses the first non-space character of String. If it is a space, skip it. If it is a non-number or a negative sign, it will directly return to NaN;
2) Continue to parse the contiguous characters until they encounter non-numbers or parsing is completed, and return the parsed content;
3) If the non-space characters parsed to the string start with 0x and add hexadecimal characters afterwards, it will be converted to hexadecimal. If it starts with 0 and adds english characters afterwards, it will be converted to english;
Example: "1234blue"->1234;""->NaN;"0xA"->10;"070"->56;" 22.5"->22;" 12 457 blue"->12; Only the spaces that appear in 1) are skipped during parsing
9. You can also use parseInt(*,*) method. The latter variable represents the division to be converted; you can choose 16, 10, 8, 2, etc., such as parseInt("10", 16)->16, and you do not need to add 0x or 0 at this time.
10. The toString() method generally does not need to use parameters. true->"true" is converted according to strings, but when converted from numbers to String, parameters can be added to set the partition.
Example: var num = 10->String--------num.toString()->"10";num.toString(2)->"1010";num.toString(8)->"12";
11. In JavaScript, a number is represented by 32-bit data. The left shift << will not change the sign bit. Example: 2<<5;-->64 is: 10->1000000
12. Right shift is divided into two types: signed right shift and unsigned right shift:
1) Signed right shift: >> Only move data bits, not sign bits: -64>>5 --->-2
2) Unsigned right shift: >>> All must be moved, so after the negative number moves, it will become a positive number, and this positive number is generally very large
12. The case in the switch statement is very powerful, it can be a numeric value, a string or even an expression
13. There is no parameter added in function() in javascript, for it, it is actually received by arguments[]
14. JavaScript has no block-level scope, as shown in the following example:
Copy the code as follows: if(true)
{
var color = "blue";
}
alert(color);
Blue will be printed out. This is limited to the alert statement that is located in the global environment, but this will not happen in C language. Generally, loop functions such as for functions will be destroyed, and they will also be destroyed in JavaScript.
15. The instance of statement is used to determine whether it belongs to a certain data type or an object: person instanceof Object //The variable person is Object? If so, it returns true
16. Setting the variable value to null can dereference this variable. JavaScript's garbage collector will recycle it when it is run next time.
17. Reference types are similar to classes, but not the same thing! There are two ways to create a reference type:
1)
The code copy is as follows: var person = new Object();//Object is the most commonly used reference type in javascript
person.name = "zhangsan";
person.age = 30;
2)
Copy the code as follows: var person = {
name : "zhangsan",
age : 30 //Note that there is no "," here!
};
3) is a combination of 2) and 1)
The code copy is as follows: var person = {};
person.name = "zhangsan";
person.age = 30;
The second of the above three methods is the most commonly used
18. There are three types of objects: user-defined-object (user-defined object), native-object (built-in object) and host-object (host object)
where native-object is an object built into javascript, such as Array, Math, Date, etc., while host-object is an object provided by the browser.
19. Open a new window in JavaScript with window.open(url, name, features). These three parameters are all optional, as follows:
1) url is the address of the window to be opened
2) Name is the name of a new window, and you can communicate with the new window through name
3) features is a string divided by "," such as "height:300, width:200", whose content is various properties of a new window
20. You can directly call javascript function in html document. This requires the method of javascript:xxxxxx, where javascript is called javascript pseudo-protocol
For example, there is a method: function test(){}
<a href="javascript:test()">test</a>//This method is to use the javascript pseudo-protocol to call the javascript method. This method is not recommended because different browsers support this pseudo-protocol differently.
21. The content of many nodes is not in their value, such as: <p id="desc">Hello world</p>. You can use var text = document.getElementById("desc").firstChild.nodeValue;
The value obtained by text here is: Hello World
22. Method to insert a new element into the html document:
1) Create a new element (including its content, etc.)
2) Insert this new element into the number of nodes
Description: 1) Method to create a new element: var para = document.createElement("p");//Create a <p></p>
At this time, this element already has all the properties of <p>, but there is no content in it and it has not been inserted into the document
Then use the var txt = document.createTextNode(text) method to create a text node content and insert the text node content into <p>
2) parent.appendChild(child)// method can insert a child element into the parent element
Including two parts of work: 1-Insert the text node under the <p> node, para.appendChild(txt); 2-Insert the <p> node under other nodes
23. The set and get methods can also set properties for an object, called memory properties, which can implement relatively complex operations, such as:
Copy the code as follows: var m = {
x : 1,//Data attributes
y : 1,
get r() {return Math.sqrt(this.x*this.x + this.y*this.y);},//Memory attribute, this property is written: set (or get) Property name (param(set method has)) {function body}
set r(newvalue) {
var oldvalue = Math.sqrt(this.x*this.x + this.y*this.y);
var radio = newvalue/oldvalue;
this.x *= ratio;
this.y *= ratio;
}
}
24. Array operation:
1) The length attribute represents length
2) Join() method:
The code copy is as follows: var a = [1,2,3];
a.join()----->"1,2,3"
a.join(" ");----->"1 2 3"
a.join("");----->"123"
3) reverse()//Reverse the elements in the array in reverse order
Copy the code as follows: var a = [1,2,3]; a.reverse().join()--->"3,2,1"
4)sort()//Sort the elements in the array and return the sorted array
sort()//Sort in alphabetical order
The code copy is as follows: sort(function(a,b){//a, b refers to the elements in the array
if(a>b)
{
return 1;
}
else if()
{
return -1
}
else
{
return 0;
}
}
)//This method can freely define the sorting method you need
5)concat()//Create and return a new array, which is used to concatenate arrays, which are connected to elements in the array rather than the array itself
Copy the code as follows: var a = [1,2,3]
a.concat(4,5)----->[1,2,3,4,5]
a.concat([4,5])----->[1,2,3,4,5]
a.concat([4,5],[6,7])----->[1,2,3,4,5,6,7]
a.concat(4,[5,[6,7]])----->[1,2,3,4,5,[6,7]]
6) slice() returns a fragment of the specified array. You can write two parameters or one parameter. One parameter represents starting from the current position of the parameter and ending. The two parameters refer to the first parameter to the second parameter.
Parameter -1 specifies the last element, that is, the penultimate element, while -3 specifies the penultimate element
The code copy is as follows: var a = [1,2,3,4,5];
a.slice(0,3);----->[1,2,3]
a.slice(3);----->[4,5]
a.slice(1,-1);----->[2,3,4]
a.slice(-3,-2);----->[3]
25. window.location = "//www.VeVB.COM/";//can be used to open web pages
26. SetTimeout()//Two parameters, one is the callback function, and the other is the callback time, indicating how many milliseconds it is to call this callback function, such as:
Copy the code as follows: setTimeout(function(){alert("Hello World");},2000);//The dialog box pops up after two seconds
clearTimeout(h);// Used to cancel the call to setTimeout, var h = setTimeout(func,time);
27. Click event for an element: var template = document.getElementById("xx");tempelement.onclick = function(){alert("Hello");};
28. var nowtime = new Date(); nowtime.toLocaleTimeString(); can display the current time (Note: it is time but does not include date)
29. setInterval(func,time);//It is used to register the function called repeatedly after the specified time. Func is the function called repeatedly, time is the specified time, unit milliseconds
And clearInterval(h); is an event used to unregister, where h is var h = setInterval(func,time);
30. Whether it is setTimeout or setInterval to setTimeout, it is not executed immediately, but put this method into the queue and wait for the previous state to be executed before execution.
31. window.location.href// can get the URL address loaded by the current document, window.location.search// can get the content after the ? characters in the current document, which is generally used for
Detect the situation where key-value pairs, name=value
32. The navigator attribute of window can contain multiple attributes:
1) appName----->The full name of the web browsing area
2) appVersion----->Browser Manufacturer and Version
3) userAgent----> Usually contains appVersion and other information, without a certain format
4) platformfrom----->Operation system that runs the browser, it is possible to even make the hardware
5) onLine----->If this property exists, it means whether the current browser is connected to the network.
6)geolocation----->Interface used to determine user geolocation information
33. Screen object can obtain information about the size of the window display and the number of available colors.
34. Windows provides three dialog boxes
1) alert-->Prompt dialog box
2)confirm()----> It also displays a message, but requires the user to confirm or cancel, example: var correct = confirm("hello world"); if(correct){return true}
3)prompt()----->Show a message, wait for user input and return to that string
35. window provides a display scheme for modal dialog boxes: showModalDialog(param1,param2,param3)//
Parameter 1: Used to specify the URL that provides the HTML content of the dialog box
Parameter 2: It can be an arbitrary value. This value can be accessed through the value of the window.dialogArguments property in the script in the dialog box.
Parameter 3: It is a non-standard list, containing name=value pairs separated by semicolons. If this parameter is provided, you can configure the dimensions and other properties of the dialog box, and use dialogwidth and dialogheight to
Set the size of the dialog window, use "resizable = yes" to allow the user to change the window size
36. The name attribute can also be used to obtain certain elements, but the name attribute is only valid in a few html elements: form <form>, form element, <iframe> and <img> elements
document.getElementsByName();//Get an array composed of all name attributes
37. Due to historical reasons, only <form>, <img> and those <a> with href attributes have document.forms.id//name as the form tag id, and other elements are not allowed.
38. getElementsByClassName(); can get the child nodes of the same className under the parent node
39. The important attributes of Node nodes are explained as follows:
1) parentNode----->To the parent node of the node, if it is a document object, its parent node is null
2) childNodes----->Read-only class array node, it is a child node of that node
3) firstChild and lastChild, the first child and the last child of the node
4) nextSibling, previousSibling, the previous and next ones of the brother nodes of this node
5) nodeType---->The node type of this node is to return a numerical value, 9 represents document node, 1 represents Element node, 3 represents Text node, 8 represents Comment node, and 11 represents DocumentFragment node
6) nodeValue----->text node or Comment node text content
7) nodeName----->The label name of the element, expressed in capital form
40. You can create an editable area element in HTML. Method:
The code copy is as follows: <div id="editor" contenteditable>Click to Edit</div>, and the contents can be obtained through the innerHTML attribute;
var editor = document.getElementById("editor"); alert(editor.innerHTML);//Show all content, including <br>, etc.
41. Methods for loading two functions in window.onload window.onload=function(){
// Write two functions here
func1();
func2();
}
42. Methods to set css attributes for Element:
Copy the code as follows: var tip = document.createElement("dd");//Create an element
tip.style.cssText = "position:absolute;bottom:0;height:20px;width:380px;padding:10px;color:#fff;background:#fff;";//Set the css attribute of the element
To view more JavaScript syntax, you can follow: "JavaScript Reference Tutorial" and "JavaScript Code Style Guide". I also hope that everyone will support Wulin.com more.