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.