DOC PREVIEW
Penn CIT 597 - JavaScript Miscellany

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

JavaScript MiscellanyProperties of functionsThe for...in loopGlobal and local variablesMethods IMethods IIThe EndJan 14, 2019JavaScript Miscellany2Properties 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 constructor3The 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 4Global 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)5Methods 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));}6Methods IIWe can turn this function into a method, like so:myPoint.dist = distance;Now this inside the function refers to myPoint, and we can say:document.write("The distance is " + myPoint.dist(6, 9));If we don’t want to permanently associate the function with myPoint, but just use it briefly, we can say:document.write("The distance is " + dist.call(myPoint, 6, 9));Or: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 parameters7The


View Full Document

Penn CIT 597 - JavaScript Miscellany

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download JavaScript Miscellany
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view JavaScript Miscellany and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view JavaScript Miscellany 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?