First knowledge of Javascript__javascript

Source: Internet
Author: User
Tags finally block

1, JavaScript is often used in combination with HTML, use the following:

A:

<script>

JavaScript goes here

</script>

B:

<script src= "Example.js" ></script>

2, JavaScript does not have the concept of a block, all the variables in the function are global variables, even if you create in the IF, switch statements are also global variables, this is very different from other languages

Such as:

In the following example, the definition of color two times, which may cause some kind of error in other languages, but not in JS, but for the readability of the program, it is best not to repeat the definition of the variable

var color= "Blue";
if (color) {
var color= "purse";
Console.log (color);
}
Console.log (color);

Its output is: purse purse


var color= "Blue";

function Printcolor () {
var color= "purse";
Console.log (color);
}
Printcolor ();
Console.log (color);

Its output is: purse Blue


3, the function must be created when the keyword function

4, the definition of variables without declaring the type, only need to use the VAR statement

Such as:

var x = 3;

function Numsquare (x) {
return x*x;
}
var sentence = "The square of" + x + "is equal to" + numsquare (x);

Console.log (sentence);

5, from the call function, they run independently, do not have to call, can run themselves (since the call function in jquery used many)

((function Selfprint () {
Console.log ("This function would automatically print this statement");
})())

6, the closure function, you can access its own variables, you can access the global variables, you can access the external function of the variables, without passing the parameters of his

Such as:

function ShowName (firstname,lastname) {
var nameinfo = "Your name is";
function Makefullname () {
return nameinfo +firstname+ "" +lastname;
}
return Makefullname ();
}

Console.log (ShowName ("Princess", "Wendy"));

Output: Your name is Princess Wendy



function Celebrityname (firstName) {
var nameinfo= "This celebrity is:";


function LastName (thelastname) {
Console.log (nameinfo + firstName + "" + thelastname);
}


return lastName;
}
var myname = celebrityname ("wd");


MyName ("Y");

Output is: this celebrity is:wd y


function Thelocation () {
var city = "Wuhan";


return{
Get:function () {console.log (city);},
Set:function (newcity) {city=newcity;}
}
}
var myplace = thelocation ();
Myplace.get ();
Myplace.set ("Luoyang");
Myplace.get ();

Output is: Wuhan
Luoyang


7. Anonymous recursive function:

var FAC = function (n) {
Return! (n>1) 1:arguments.callee (n-1) *n;//Arguments.callee repeatedly invoke anonymous function keywords
}
Console.log (FAC (3));


8, abnormal

var a = 100;
var b = 0;


try{
    if (b = = 0) {
        throw ("Divide by zero error.");
   }
    Else
    {
        var c = A/b
        A Lert ("c =" +c);
   }
}


catch (E) {
    alert ("Error:" + E);
}


finally{
    Alert ("Finally block would always execute");
}

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.