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 for...in 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 for...in 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 " + dist.call(myPoint, 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

  • Of Mice and Men Storyboard - Humble Independent School ...

    Of Mice and Men Storyboard - Humble Independent School ...

    1.) Why is Candy so hesitant to have his dog killed? What theme is Candy emphasizing (look at your background notes) 2.) Why does Carlson want to kill Candy's dog? What theme does Candy's dog emphasize? 3.) How does the...
  • Defects in Solids

    Defects in Solids

    The Brillouin Zone. Is defined as the Wigner-Seitz primitive cell in the reciprocal lattice (smallest unique set of distance/area/volume in reciprocal space). Its construction exhibits all the wavevectors. k. which can be Bragg-reflected by the crystal. Also critical for understanding...
  • 2017 Guidelines of the American Thyroid Association for

    2017 Guidelines of the American Thyroid Association for

    In most regions, including the United States, women who are planning pregnancy or currently pregnant, should supplement their diet with a daily oral supplement that contains 150 . μg. of iodine in the form of potassium iodide. This is optimally...
  • Chapter 8 Concepts of Chemical Bonding

    Chapter 8 Concepts of Chemical Bonding

    But these numbers don't explain why the reaction of sodium metal and chlorine gas to form sodium chloride is so exothermic! 495 - 349= +146 kJ. ... Ionic compounds dissolve in water because the attraction of the ions to water...
  • Glass Fractures - Chemistry

    Glass Fractures - Chemistry

    Glass Fractures bsapp.com Glass How broken? Link a suspect to a crime scene Fingerprints Blood bsapp.com Glass Evidence Collect evidence from crime scene Analysis: Physical Properties (color, thickness, type) Optical Properties (refractive index: the speed of light in a vacuum...
  • Heat, Power and Biofuels from Biomass Agricultural Sustainable

    Heat, Power and Biofuels from Biomass Agricultural Sustainable

    Mainly derived from biomass or bio waste. These fuels can be used for any purposes, but the main use for which they have to be brought is in the transportation sector. The most important feature of biomass is that they...
  • Nobel Laureates - pitt.edu

    Nobel Laureates - pitt.edu

    Arthur Kornberg quotes Severo Ochoa Arthur Kornberg The Nobel Prize in Physiology or Medicine 1962 was awarded jointly to Francis Harry Compton Crick, James Dewey Watson and Maurice Hugh Frederick Wilkins "for their discoveries concerning the molecular structure of nucleic...
  • First Class Cadet Training The Royal Air Force

    First Class Cadet Training The Royal Air Force

    346 (Tynemouth) Squadron Other titles Arial Garamond Wingdings ATC First Class Cadet Training The RAF Mission The RAF - Vision Development of the Royal Air Force Slide 5 Development of the Royal Air Force The RAF during the inter-war years...