Javascript basics-language features

Source: Internet
Author: User

Javascript is a function-based language. Everything is an object, but it is quite special

reference

A reference is a pointer to an actual object, which is the same as a pointer to a C/C ++ object, C # and Java objects are also references

function overloading

// a simple function for sending message

function sendmessage (MSG, OBJ) {

// if both a message and an object are provided

If (arguments. length = 2)

// send the message to the object

obj. alert (MSG);

// otherwise, assume that only a message was Provided

else

// so just display the default error message

alert (MSG );

}

// both of these function callwork

sendmessage (" hello, world! ");

sendmessage (" how are you? ", Window);

Scope

The scope is divided by functions, not blocks. It is different from other languages.

// Set a global variable, Foo, equal to test

VaR Foo = "test ";

 

// Within an if Block

If (true ){

// Set Foo equal to 'new Test'

// Note: This is still within the global scope!

VaR Foo = "new test ";

}

 

// As we can see here, as foo is now equal to 'new Test'

Alert (FOO = "new test ");

 

// Create a function that will modify the variable foo

Function Test (){

VaR Foo = "old test ";

}

 

// However, when called, 'foo' remains within the scope

// Of the Function

Test ();

 

// Which is confirmed, as foo is still equal to 'new Test'

Alert (FOO = "new test ");

 

JavascriptThe global scope is window.Object

// A globally-scope variable, containing the string 'test'

VaR test = "test ";

 

// You'll notice that our 'global' variable and

// Property of the window object are identical

Alert (window. test = test );

 

// A function in which the value of foo is set

Function Test (){

Foo = "test ";

}

 

// Call the function to set the value of foo

Test ();

 

// We see that foo is now globally scoped

Alert (window. Foo = "test ");

Closure

The closure means that the inner function can reference the variables in the function that contains it, even if the execution of the outer function has been terminated.

Http://jibbering.com/faq/faq_notes/closures.html

No need to generate global functions:

// Create a new anonymous function, to use as a wrapper

(Function (){

// The variable that wowould, normally, be global

VaR MSG = "thanks for visiting! ";

 

// Binding a new function to a Global Object

Window. onUnload = function (){

// Which uses the 'hidd' variable

Alert (MSG );

};

 

// Close off the anonymous function and execute it

}());

Context

This function is fully used in JavaScript.

 

VaR OBJ = {

Yes: function (){

// This = OBJ

This. Val = true;

},

No: function (){

This. Val = false;

}

};

 

// We see that there is no Val property in the 'obj 'object

Alert (obj. Val = NULL );

 

// We run the Yes function and it changes the Val Property

// Associated with the 'obj 'object

OBJ. Yes ();

Alert (obj. Val = true );

 

// However, we now point window. No to the obj. No method and run it

Window. No = obj. No;

Window. No ();

 

// This results in the OBJ object staying the same (as the context was

// Switched to the window object)

Alert (obj. Val = true );

// And window Val property getting updated.

Alert (window. Val = false );

 

Some of the above processes are not quite the same. Javascript provides the call and apply methods to implement this function:

// A simple that sets the color style of its context

Function changecolor (color ){

This. style. Color = color;

}

// Calling it on the window object, which fails, since it doesn' t

// Have a style object

Changecolor ("white ");

 

// Find the element with an ID of main

VaR main = Document. getelementbyid ("Main ");

// Set its color to black, using the call Method

// The call method sets the context with the first argument

// And passes all the other arguments as arguments to the Function

Changecolor. Call (main, "black ");

 

// A function that sets the color on the Body element

Function setbodycolor (){

// The apply method sets the context to the body Element

// With the first argument, the second argument is an array

// Of arguments that gets passed to the Function

Changecolor. Apply (document. Body, arguments );

}

// Set the background color of the body to black

setbodycolor ("black");

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.