Some knowledge fragments of JavaScript (2)-reflection-global variable-callback

Source: Internet
Author: User
Tags hasownproperty

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.