Amazing JavaScript object-oriented (1)

Source: Internet
Author: User

 

 

There is a popular saying that JavaScript is object-based and event-driven. How should we understand the meaning of "Object-based?

"Proficient in JavaScript" tells us that objects are the foundation of JavaScript, and even "javascript is completely object-oriented ".

 

I don't know how to judge such a statement. JavaScript does have object-oriented features, but its representation is similar to other object-oriented features.Programming LanguageVery different.

 

 

Before talking about JavaScript object-oriented, I 'd like to talk about the scope of JavaScript. I think only by figuring out this problem can we better understand the following content. I will use my own understanding to compare the object-oriented features in JavaScript with General object-oriented features.ProgramSome titles of the design language (Java/C ++, etc.) correspond.

 

 

First, clarify two issues:

1. What is a global variable?

Global variables in Javascript actually refer to the object attributes under the window object.

2. Scope division.

The scope in Javascript is based on context and divided by functions, rather than blocks.

 

Next let's look at an example (the original example is from "proficient in JavaScript", with some changes ):

< Script Type = " Text/JavaScript " >

// Set the global variable Foo and set it to "test"
VaR Foo =   " Test " ;

If ( True )
{
//Note: It is still in the global scope.
VaRFoo= "New Test";
}

// As we can see, foo is now equal to 'new test '.
Alert (FOO );

// Create a new function that modifies the foo variable.
Function Test ()
{
// Variables defined in the function do not affect global variables.
VaR Foo =   " Old Test " ;
// Implicitly define global variables
Val =   ' Hello! ' ;
}

// However, when calling the test function, foo only works within the function scope.
Test ();

// Check whether foo is equal to 'new test'
Alert (FOO );

// The global variable is actually a property in the window.
Alert (window. Foo );

// The global variables implicitly defined in the function.
Alert (VAL );

< / SCRIPT>

 

Now you probably have a preliminary understanding of the Javascript scope.

The following two points should be emphasized:

1. In the same scope, JavaScript allows repeated definitions of variables, and the latter will overwrite the previous definition.

2. If the variable defined by the keyword VaR is not added to the function, the global variable is used by default.

 

 

I will write it here today, and I will organize some object-oriented features in Javascript tomorrow to share with you.

 

 

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.