reflection in JavaScript: the principle of reflection in a programming language is the same, by manipulating metadata (the language of the description language) to accomplish some of the features that are difficult to implement in a language that does not have a reflection function. In a static language, reflection is a tall thing, such as dynamically creating methods and calls at run time, delay binding, and so on, and so on, when the first C # reflection was used eight or nine years ago, that excitement was not to mention. However, in the dynamic language world, many functions are not necessarily reflected by reflection, so the reflection of JavaScript becomes relatively simple, so the main usage of reflection in JavaScript is a few such as TypeOf, Instanceof,hasownproperty
varAlteral={name:"A lteral"};console.log (typeofAlteral);//ObjectConsole.log (Alteral.hasownproperty ("name"));//trueConsole.log (Alteral.hasownproperty ("gender"))//false;varFun =function(){varsubfun=function(){return NULL};};typeofConsole.log (fun);instanceofFunction);//true
Global variables:
In JavaScript, you can arbitrarily define a global variable anywhere. Small application seems very convenient, code into scale, after splitting the file, it is finished. Compiler regardless, we can tube.
Two different ways:
1. All written together, such as the top of the program, good management.
2. Write a container of global variables and throw all the global variables in when you use them. Such benefits are also unified management for convenient retrieval.
var variablebag={}
Callbacks are implemented in JavaScript in the form of passing a function as an argument to another function, which invokes the passed argument at the appropriate time. We use an example of a code to explain:
functionMainfunction (callback) {//defines a host function that receives a callback function as a parameter. //do things like wait 1 seconds and pass the current time to callback varStartTime =NewDate (). GetTime (); while(NewDate (). GetTime () < StartTime + 1000); varnow =NewDate (). GetTime (); Callback (now);//call the passed in parameter};functionAfunction (Time) {//defining the callback functionConsole.log ("Afunction is called"); Console.log (time);}; Mainfunction (afunction);//calling the host function
The result of executing this code is that the callback function afunction is called by the host function after waiting for one second, and the host function passes a parameter to Afunction, which is very important. Because in reality, the parameters that are passed are the callback functions that are dependent on the correct execution of the content.
Some knowledge fragments of JavaScript (2)-reflection-global variable-callback