JavaScript var is used to define a variable, which is commonly used to define or invoke a property or method. However, in the global scope, this and var define variables are equivalent.
Window
Console.log (' window: 'var name = ' Stone 'this. Name = ' Niqiu '; Console.log ( ' var: ', name); Console.log (' this: ',this. name);
The Window object has the Name property, the initial value is empty, but how to get the value when the first console. The last value was found for this window record. Reopen the page and execute only console.log (' window: ', name); The output is empty.
Console.log (' window: ', name); var name = ' Stone ';
Refresh two times after exporting stone. So the window object was not recreated when it was refreshed. Window Yes {} object. No constructor, no new.
function with {}
Inside a function, VAR defines a local variable, and this defines the property. The two are not covered. However, the newly created object cannot be fetched to the local variable.
varfool =function () { varname = ' Stone '; varNikename = ' Stoneniqiu '; This. Name = ' Niqiu '; This. GetName =function() {Console.log (' var ', name); Console.log (' This ', This. Name); }; This. Getnickname =function() { returnNikename; }; };
// Niqiu Console.log (F.nikename); // undefined Console.log (F.getnickname ()); // Stoneniqiu
However, if you use {} to define an object, the internal variable equals all attributes.
varblock ={name:' Stone ', Nickname:' Stoneniqiu ', GetName:function () { //return This.name;//stone returnName//' Windows name ';}, Getnickname:function() { return This. Nickname; }, Printallname:function() {Console.log ( This. Getnickname ()); Console.log ( This. GetName ()); } }; Console.log (block.name);//stone Console.log (Block.getname ()); //windows name
Like the Window object, {} Creates an object that cannot be new because there is no constructor.
Therefore, if the object is reused, larger, or in the form of function, separate private variables and public properties or methods, the internal module of function can be in the form of {}.
The method through this to obtain the context in which they belong is called a public method . This "super" delay binding allows the function to be reused for this when the bind to the object occurs at the time of invocation.
This refers to the currently executing object, in order to avoid this change. Use a variable to save this.
var self= This
Magical callbacks.
Here's an example of a callback method defined in the object's parameters, but we passed in a method of fool itself, actually executing it.
varfool =function(config) {varParmas ={callback:function() {Console.log (' Raw callback '); } }; Parmas=$.extend (parmas, config); varname = ' Stone '; varNikename = ' Stoneniqiu '; This. Name = ' Niqiu '; This. GetName =function() {Console.log (' var ', name); Console.log (' This ', This. Name); }; This. Getnickname =function () { returnNikename; }; This. dosomething =function() {parmas.callback (); }; }; varf =NewFool ({callback:function() { $("#bt"). Click (function() { f.getname () }); }}); F.dosomething ();
Run without error, click button to trigger the GetName method. But if you write directly below, you will get an error, which is very common sense.
var New Fool ({callback:f.getname ()});
Again, the anonymous function is used to wrap up and run.
var New Fool ({callback:function() { (function() { f.getname (); }) (); }}); F.dosomething ();
Why?
var and this,{} and function small memory