Basic knowledge of JavaScript basics _

Source: Internet
Author: User
Tags current time numeric value setinterval

1, var msg;//declare a variable, before assigning this variable a value, the variable is named: undefined

2, msg = "Hello";/Not applicable VAR can declare a global variable, but this variable because it is global, so it is easy to cause other calls some problems, it is not recommended

3. JavaScript has 5 basic data types, Undefined, Null, Boolean, number, and string; and a complex data type: Object

4, the Var num=076;//representative of the octal system, with 0 beginning, if the following value beyond the range of 8, for example: 08, 079, and so on, the decimal resolution

5. var num=0x22;//represents hexadecimal

6, Nan->not a number;//non-numeric, NaN is not equal to any value, Bauto itself; Determines whether a value can be converted to numeric values: isNaN (*), * for the data to be transformed

7, to number conversion when,false->0;true->1; "" ->0; " 00022 "->22;" HelloWorld "->nan; Note: Using the number () method

8, if the use of parseint () method of conversion and 7 (for strings) different, the basic principles are as follows:

1 The first non-whitespace character of string parsing, if the space is skipped, if it is not a number or minus sign, then directly return nan;
2 The character of the docking continues to parse until it encounters a non-numeric or analytic completion, returning the content that has already been resolved;
3 If the non-space character parsing to the string starts with a 0x and then adds a 16-character to the 16-system, if 0 starts with a 8-plus character, it is converted to 8;

Example: "1234blue"->1234; "" ->nan; " 0xA "->10;" 070 "->56;"   22.5 "->22;" 457 Blue "The space that appears in the->12; resolution only skips 1)

9, can also adopt parseint (*,*) way, the following variable represents the conversion of the system, optional 16,10,8,2, example parseint ("ten")->16, no need to add 0x or 0

10. The toString () method generally does not have to use the parameter,true-> "true" to be converted by a string, but when converting from a number to a string, you can add parameters to set the system

Example: var num = 10->string-------num.tostring ()-> "Ten", Num.tostring (2)-> "1010"; num.tostring (8)-> "12";

11, in JavaScript in 32-bit data to represent a number, left << does not change the sign bit, example: 2<<5;-->64 namely: 10->1000000

12, right shift is divided into two: signed right and unsigned Right move:

1 symbol Right Move:>> only move data bits, do not move symbol bit: -64>>5---->-2
2 unsigned Right move:>>> all move, so negative numbers move to 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, or it can be a string or even an expression

13, in JavaScript function () can not add parameters, for it is actually by arguments[] received

14, JavaScript does not have block-level scope, the following example:

Copy Code code as follows:
if (true)
{
var color = "Blue";
}
alert (color);

Will print out blue, this only alert statement is in the global environment, but not in Class C, which is generally destroyed except for circular functions such as for functions, which are also destroyed in JavaScript.

15. The instance of statement is used to determine whether it belongs to a data type, or to an object: Person instanceof object//variable person is object? If it returns True

16. Setting the variable value to null unlocks the reference to this variable, which is recycled by the JavaScript garbage collector the next time it is run

17, reference type and class, but not the same! There are two ways to create a reference type:
1)

Copy Code code as follows:
var person = new Object ();//object is one of the most commonly used reference types in JavaScript
Person.name = "Zhangsan";
Person.age = 30;

2)

Copy Code code as follows:
var person = {
Name: "Zhangsan",
AGE:30//Note there is no ","!
};

3) is a combination of 2 and 1)

Copy Code code as follows:
var person = {};
Person.name = "Zhangsan";
Person.age = 30;

The second of these 3 methods is the most commonly used

18, there are three kinds of objects: user-defined-object (user-defined objects), Native-object (built-in objects) and Host-object (host object)
Where Native-object is an object built into JavaScript, such as array, math, and date, and Host-object is the object that the browser provides.

19, in JavaScript to open a new window with: window.open (url,name,features), these three parameters are optional, described below:

1 URL is the address of the window to be opened
2 name is a new window, you can communicate with the new window by name
3 Features is a "," split string, such as "height:300,width:200", whose contents are various properties of the new window

20, you can call the JavaScript function directly in the HTML document, this has to use javascript:xxxxxx way, where JavaScript is called JavaScript Pseudo protocol
Example, there is a method: function Test () {}
<a href= "Javascript:test ()" >test</a>//This approach is the use of JavaScript pseudo protocol to invoke JavaScript methods, which is very not advocated, Because different browsers support this kind of pseudo protocol is not the same.

21, the content of many nodes is not in its value, such as: <p id= "desc" >hello World</p>, at this time you can use the var text = document.getElementById ("desc"). Firstchild.nodevalue;
Here text gets the value: Hello World

22, insert a new element in the HTML document method:

1 Create a new element (including its contents, etc.)
2 Insert the new element into the node number

Description: 1 method to create a new element: var para = document.createelement ("P");//Create a <p></p>
At this point, this element already has all of the properties of <p>, but there is no content in it and it is not inserted into the document
Then use the var txt = document.createtextnode (text) method to create a text node content to insert this text node content into the <p>

2 Parent.appendchild (Child)/method you can insert a child element under the parent element
Consists of two parts of the work: 1-Insert the text node under the <p> node, para.appendchild (TXT), 2-Insert the <p> node under the other node

23, set and get methods can also set properties for an object, called the Memory property, it can implement more complex operations, such as:

Copy Code code as follows:
var m = {
x:1,//Data Properties
Y:1,

Get R () {return math.sqrt (this.x*this.x + this.y*this.y);},//Memory properties, 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, the operation of the array:

1) Length attribute
2) method of Join ():

Copy Code code as follows:
var a = [1,2,3];
A.join ()-----> "1,2,3"
A.join ("");-----> "1 2 3"
A.join ("");-----> "123"

3 reverse ()//reverse the order of the elements in the array
Copy Code code as follows:
var a = [1,2,3];a.reverse (). Join ()---> "3,2,1"

4 sort ()//The elements in the array are sorted and returned to the sorted array
Sort ()//ORDER by alphabetical list

Copy Code code as follows:
Sort (function (a,b) {//a, b refers to an element in an array
if (a>b)
{
return 1;
}
else if ()
{
Return-1
}
Else
{
return 0;
}
}
)//This method is free to define the sort of order that you want

5 concat ()//Creates and returns a new array, which is used to connect the array, which connects the elements in the array rather than the array itself

Copy Code 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, can write two parameters or a parameter, one parameter represents from the current position of the argument to the end, two parameters from the first argument to the second argument
Parameter-1, specifies the last element, the penultimate element, and-3 specifies the penultimate element

Copy Code code 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 = "http://www.jb51.net/";//Can be used to open web pages

26, settimeout ()///two parameters, one for the callback function, two for the callback time, the description is how many milliseconds after the call this callback function, such as:

Copy Code code as follows:
settimeout (function () {alert ("Hello World");},2000);//Two seconds later pop-up dialog box
Cleartimeout (h);//used to cancel settimeout calls, var h = settimeout (Func,time);

27. Click event for an element: var tempelement = document.getElementById ("xx"); Tempelement.onclick = function () {alert ("Hello");

28, var nowtime = new Date (), nowtime.tolocaletimestring (), can display the current time (note: Time does not include date)

29, SetInterval (func,time);//It is used to register a function that is called repeatedly after a specified time, Func is a function that is called repeatedly, time is specified, and the unit millisecond
and Clearinterval (h) is the event that is used to unregister, where h is var h = setinterval (Func,time);

30, whether it is settimeout or setinterval put time set to 0 o'clock, are not executed immediately, but put this method into the queue, waiting for the previous state execution before the execution

31, window.location.href//can get the current document load URL address, window.location.search//can get the current documents in the following characters, generally used for
Detecting key-value pairs that are Name=value

32. The Navigator property of the window can contain multiple properties:
1) appName----->web Browse Area Full Name
2 appversion-----> Browser Manufacturers and versions
3 useragent-----> usually contains appversion and other information, without a certain format
4 Platfrom-----> operating system in which the browser is running, may even make hardware
5 Online-----> This property, if present, indicates whether the current browser is connected to the network
6 geolocation-----> interface for determining user location information

33. The Screen object can get information about the size of the window display and the number of colors available

34, Window provides three kinds of dialog boxes
1) alert--> Prompt dialog box
2 confirm ()-----> also displays a message, but requires the user to determine or cancel, for example: var correct = confirm ("Hello World"); if (correct) {return true}
3 prompt ()-----> displays a message waiting for the user to enter and return the string

35, Window provides modal dialog box display scheme: ShowModalDialog (PARAM1,PARAM2,PARAM3)//
Parameter one: Specifies the URL that provides the HTML content of the dialog box
Parameter two: can be an arbitrary value, this value in the dialog box in the script can be accessed by the value of the Window.dialogarguments property.
Parameter three: is a non-standard list containing name=value pairs separated by semicolons, and if this argument is provided you can configure the dimensions and other properties of the dialog box, using Dialogwidth and dialogheight to
Set the size of the dialog window and use "resizable = yes" to allow the user to change the window size

36. The Name property can also be used to get some elements, but the Name property is only valid in a few HTML elements: Form <FORM>, table cell,<iframe> and elements
Document.getelementsbyname ()//Gets an array of all name properties

37, for historical reasons, only <form>, and those with the href attribute of the <a>, only document.forms.id//name as a form label ID, other elements are not allowed

38, Getelementsbyclassname (), can obtain the same classname under the parent node of the child node

39, the node nodes of the important attributes described below:
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, which is a child node of the node
3 FirstChild and LastChild, the first child node and the last child node of the node
4 nextSibling, Previourssibling, the node's sibling node's former one and the latter one
5 NodeType-----> The node type of the node, is to return a number, 9 represents the document node, 1 represents the element node, 3 represents the text node, 8 represents the comment node, and 11 represents the DocumentFragment node
6 NodeValue----->text node or comment node text content
7) The label name of the nodename-----> element, expressed in uppercase form

40. In HTML, you can create an editable area element by:

Copy Code code as follows:
<div id= "Editor" Contenteditable>click to Edit</div>, the contents of which can be obtained through the innerHTML property;
var editor = document.getElementById ("editor"); alert (editor.innerhtml);//display all content, including <br>

41, the method of loading two function in Window.onload window.onload=function () {
Write two function here
Func1 ();
Func2 ();
}

42. How to set CSS properties for element:

Copy Code 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 CSS properties for elements

See more JavaScript syntax, you can focus on: JavaScript reference tutorial, JavaScript Code style guide, and also hope that many people support the cloud-Habitat community.

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.