Amazing JavaScript object-oriented

Source: Internet
Author: User

JavaScript is based on objects and events. How should we understand the meaning of "Object-based? JavaScript does have the object-oriented feature, but its representation is quite different from that of other object-oriented programming languages.

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 forms are quite different from those of other object-oriented programming languages.

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 map the object-oriented features in JavaScript to some titles in general object-oriented programming languages (Java/C ++.

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 to "test"

Var foo = "test ";

If (true)

{

// Note: it is still in the global scope.

Var foo = "new test ";

}

// As we can see, foo is now 'new Test'

Alert (foo );

// Create a new function that will modify the variable foo

Function test ()

{

// The 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 ();

// Confirm whether foo is equal to 'new Test'

Alert (foo );

// The global variable is actually a property in the window.

Alert (window. foo );

// 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.

  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next Page

Related Article

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.