Some knowledge fragments of JavaScript (2)-reflection-global variables-callback
Reflection in JavaScript: the reflection principles in programming languages are the same. It is difficult to implement some languages that do not have the reflection function by operating metadata. In a static language, reflection is a tall thing, such as dynamic method creation and calling during runtime, and delayed binding. When C # reflection was used for the first time before Year 89, don't mention that excitement. However, in the Dynamic Language world, many functions do not need to be implemented through reflection, so javascript reflection will become relatively simple. Therefore, the main usage of reflection in javascript is as follows: typeof, instanceof, hasOwnProperty var alteral = {name: "a lteral"}; console. log (typeof alteral); // object console. log (alteral. hasOwnProperty ("name"); // trueconsole. log (alteral. hasOwnProperty ("gender") // false; var fun = function () {var subfun = function () {return null };}; typeof (fun); console. log (fun instanceof Function); // true global Variable: In javascript, you can define a global variable anywhere. The small application seems very convenient. The code is scaled. After the file is split, it will be finished. No matter the compiler, we can handle it. Two methods: 1. Write all together, such as program top, good management. 2. Write a global variable container and throw all global variables into the container during use. This advantage is also the convenience of unified management and retrieval. The implementation form of var variableBag = {} callback in JavaScript is as follows: pass a function as a parameter to another function, which will call the passed parameter when appropriate. Let's use a code example to explain: function mainfunction (callback) {// defines a host function, which receives a callback function as a parameter. // Do something, for example, wait 1 second and pass the current time to callback var startTime = new Date (). getTime (); while (new Date (). getTime () <startTime + 1000); var now = new Date (). getTime (); callback (now); // call the input parameter}; function afunction (time) {// defines the callback function console. log ("afunction is called"); console. log (time) ;}; mainfunction (afunction); // The result of calling the host function to execute this code is that the callback function afunction is called by the host function after one second, at the same time, the host function passes a parameter to afunction, which is very important. In reality, the passed parameters are all the content that the callback function must correctly execute.