This problem is very basic, in fact, in JavaScript the meaning of variables and properties and other imperative language (pure functional language without variables) no difference.
In fact, the common denominator of variables and attributes is obvious, that is, they can be stored data, and can be accessed through a certain way, most of the cases, we also use them so, but with the study of JavaScript in depth, the problem is followed, then what is the difference? Are they the same?
Let's look at their main differences:
To facilitate the description, we give the variable the name variable, name the attribute attr, and specify a premise: JavaScript only functions can be run, That is, JavaScript code written in global scope is assumed to be written in the outermost function provided by the browser.
1. How to define
A. Variables are defined by var variable in the function definition.
There are two points to note, one is that the variable is only declared in the function definition, and of course here we use the premise that is assumed earlier. Second, it can only be defined by the VAR keyword.
B. Properties are defined by this.attr or objname.attr, or attr defined (. operator can be replaced by [""])
2. How to access
2.1 Accessibility
A. Variable: A function that contains this variable in a closure can access this variable (a more detailed explanation will be given later in the article)
B. Properties: As long as there is a reference to the object (including this), the property can be accessed through the object
2.2 Delete operation
A. Variables cannot be deleted
B. Properties can be delete
3. Attribution
A. Variables: variable objects that belong to the execution context (execution context) (Variable object)
B. Properties: Attribution to the object in which it resides
Let's talk about attribution in detail below.
Always feel that JavaScript research in depth, there are many just when the introduction of the seemingly simple, very superficial problems, to be held up, it becomes less simple.
property is relatively simple, it belongs to its object, regardless of the scene, under what circumstances, the object can be this.attr to define or access variables, objects outside the object can be referenced objname.attr to define or access variables.
And the variable is relatively complex, from the outside it appears that Var attr a variable, if the current function of the scope chain has attr, you can access. If you have an understanding of execution context, you will know that the variable is actually a special attribute: A property of the variable object (VO).
If a variable is actually a property, then it is obvious how it is accessed, and who can access Vo, who can access the variable. So who can access Vo? Regardless of the global word, when the function is executed, the function's scope chain has the VO of the property, then the function can access this property.
Summarize:
In fact, variables and attributes are essentially attributes, except that they are accessed on the JavaScript engine in a different way, and the properties of the visible object are a way to access the JavaScript itself to the developer, and the variable is actually a property of the invisible object inside the JavaScript engine. So it can only be accessed through other means provided by the JavaScript engine,
That is, by executing the function, the JavaScript engine automatically accesses the variable from the closed packet where the function is found.
Two unspoken rules = an interesting thing:
1. If you declare a variable separately, the default is to declare a property that is called a global object.
2. Global Object The VO is its own. According to our conclusion, the variables and attributes one is the property of the visible object and the other is the property of Vo, so for Global, VO is itself, that is, its VO is externally visible. So what's going to happen?
An interesting thing to do:
Under Browser Global is window
var a = "a";
b = "B";
alert (WINDOW.A); A
alert (WINDOW.B); B
Is there such an example in every JavaScript book? Will tell you this is a rule of javascript? But why is that?
Now that you know why both A and B are properties of window, A is because it is placed in the properties of VO during the pre-execution phase, and Vo is global;b because it has no object specified, so it is declared to global by default.
About variables and properties