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