JavaScript Miscellany

JavaScript Miscellany

JavaScript Miscellany Feb 10, 202 Properties of functions Functions are objects, and have properties length the number of formal parameters arguments the Arguments object, which is like an array of actual parameters Note: length is not necessarily equal to arguments.length caller the function that invoked this one, or null if the

function was invoked from the top level prototype (for constructor functions) an object that defines properties and methods of functions created with this constructor 2 The loop This kind of loop has the syntax for (variable in object) statement Each property of the object (or, if an array, each index of the array) is assigned in turn to variable Properties will be assigned in an arbitrary order

It turns out, however, that the loop does not loop through all properties of an object Built-in methods, and many built-in properties, are flagged as nonenumerable All built-in properties of functions are nonenumerable There are lots of little surprises like this in JavaScript 3 Global and local variables A variable is local to a function if

It is a formal parameter of the function It is declared with var inside the function (e.g. var x = 5) Otherwise, variables are global Specifically, a variable is global if It is declared outside any function (with or without var) It is declared by assignment inside a function (e.g. x = 5) 4 Methods I First we construct an object:

function Point(xcoord, ycoord) { this.x = xcoord; // keyword "this" is mandatory this.y = ycoord; } myPoint = new Point(3, 5); A method is a function that is associated with, and invoked through, an object (hence can use this) Here is a function that makes no sense by itself: function distance(x2, y2) { function sqr(x) { return x * x; } return Math.sqrt(sqr(this.x x2) + sqr(this.y y2)); } 5 Methods II

We can turn this function into a method, like so: Now this inside the function refers to myPoint, and we can say: document.write("The distance is " +, 6, 9)); Or: document.write("The distance is " + myPoint.dist(6, 9)); If we dont want to permanently associate the function with myPoint, but just use it briefly, we can say:

myPoint.dist = distance; document.write("The distance is " + dist.apply(myPoint, [6, 9])); The difference between these two Function methods is: call takes an object and an arbitrary number of actual parameters apply takes an object and an array of actual parameters 6 The End 7

Recently Viewed Presentations

  • Agenda - Riverside City College

    Agenda - Riverside City College

    Female deputy from San Bernardino County Sheriff's Office. Missing woman from Monrovia, CA. ... Crime that is not reported to the police and that remains unknown to officials. Special categories of crime. Stalking. Cyberstalking. Crime against the elderly.
  • 4.1 Translations -

    4.1 Translations -

    Warm-up. Open the following two files on 4.1 Translations.ggb. 4.1 Translations Geogebra Activity.doc. Using the geogebra file, answer the questions in the google doc. Ask questions, work together and be ready to talk about it in 10 minutes.
  • Permanent-magnet motor-generator or starter-generator Review & Renew J.

    Permanent-magnet motor-generator or starter-generator Review & Renew J.

    We now discuss the basic functions of "electronic speed control" (ESC). First, commutation is a relatively low-frequency switching which matches phase operation to rotor position. Thus, commutation accommodates. the existing rotational speed without . controlling. the speed per se.


    To view our template tutorials, go online to PosterPresentations.comand click on HELP DESK. When you are ready to print your poster, go online to Need assistance? Call us at 1.510.649.3001. QUICK START. Zoom in and out. As you work...
  • Liver transplantation for colorectal cancer liver metastases Vivian

    Liver transplantation for colorectal cancer liver metastases Vivian

    Cancer Immunol Immunother. 2017;66:1563-1575. Shaler CR et al. MAIT cells launch a rapid, robust and distinct hyperinflammatory response to bacterial superantigens and quickly acquire an anergic phenotype that impedes their cognate antimicrobial function: Defining a novel mechanism of superantigen-induced immunopathology...
  • World War I, aka "The Great War" Long and Short-term Causes

    World War I, aka "The Great War" Long and Short-term Causes

    First Moroccan Crisis (1905) Wilhelm II sends fleet down to Morocco makes a speech to advocate for Moroccan independence. As a response, the Algerciras Conference was held to discuss French claims of Morocco. Everybody supported France, except for Austria-Hungary. In...
  • MCAS REVIEW-Physical Science

    MCAS REVIEW-Physical Science

    Which is heaviest, 1 kilogram of gold or 1 kilogram of feathers? Now think how big a 1 kg bag of feathers would be. Compare it to the size of 1 kg of gold. The 1kg bag of feathers would...
  • Periodic Law - Physical Science

    Periodic Law - Physical Science

    Periodic Law PERIODIC LAW states that many of the physical and chemical properties of the elements tend to recur in a systematic manner with increasing atomic number. Properties of elements are periodic functions of their atomic numbers. Trends on the...