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

  • Chapter 8 Introduction to Metabolism The Energy of

    Chapter 8 Introduction to Metabolism The Energy of

    A cell does this by switching on or off the genes that encode specific enzymes or by regulating the activity of enzymes. Regulation of enzyme activity helps control metabolism. Allosteric regulation may either inhibit or stimulate an enzyme's activity.
  • How to be Highly Effective People - Presentasi

    How to be Highly Effective People - Presentasi

    Habit. Synergize. Synergize. Combining our strengths with others to create something together.Not to compete or defeat others. Always do. means. The last habit is about. SELF Renewal. Third Part. ... How to be Highly Effective People Subject: Slide Inspiratif
  • Open Storage Federation

    Open Storage Federation

    Double your sale into virtual environments with HP StoreVirtual VSA. BONUS: Raise the overall solution gross margin with the addition of VSA and hard drives! Example of deal with customer that has 15 remote offices. VSA is a great way...
  • Protecting Yourself from Fraud including Identity Theft

    Protecting Yourself from Fraud including Identity Theft

    Prevent business practices that are anticompetitive, deceptive or unfair to consumers. Federal Trade Commission. Consumer Financial Protection Bureau. Federal Drug Administration. Federal Communications Commission. Most Common Government Protection Agencies .
  • Sources of Growth - World Bank

    Sources of Growth - World Bank

    Torfinn Harding and Beata Smarzynska Javorcik, Note on the effect of FDI on export diversification in Central and Eastern Europe, 2007. Javorcik, Beata (2004), "Does Foreign Direct Investment Increase the Productivity of Domestic Firms? In Search of Spillovers through Backward...
  • Working Together in The Local Church

    Working Together in The Local Church

    1) Means: Oneness-Like-minded, same love, one accord, one mind (2) Basis: Lowliness-esteem others better than self (3) Action: Helpfulness-Looking out for others Example: Christ (5-11)-Started in the mind, forgot himself (rank, privilege), served, humbled self, obeyed completely, exalted by Father...
  • The Feasts of Israel - Bible Study Resource Center

    The Feasts of Israel - Bible Study Resource Center

    , square round about; and fifty cubits round about for the suburbs thereof. 3 And of this measure shalt thou measure the length of five and twenty thousand, and the breadth of ten thousand: and in it shall be the...
  • CAUTIONS AND LIMITATIONS  Not for use in  Failure

    CAUTIONS AND LIMITATIONS Not for use in Failure

    DESCRIPTION. For one-time escape use only - The W65 Self-Rescuer is a one-time device and must be discarded after use. Never use the respirator for other than escape through areas containing carbon monoxide in otherwise breathable air; do not use...