Talk:JavaScript/Notes/Function

From Noisebridge
Revision as of 20:56, 8 December 2013 by Johnyradio (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Johny Radio's notes

Uppercase function name indicates a constructor (just a convention, not interpreted). Every function is an instance of the Object function. Object has .prototype property, therefor every function has


Internal prototype and protoype property are not the same thing. Prototype is a property of obj

All Functions have a Call. All User-Defined Functions have a Construct. Class automatically becomes reference to Object on new instance of UD Fx.

Reference: Something is a something of the variable environment.

All about why [this] would be undefined.

Call is internal, automatically invoked, not explicitly called. s.move invokes Call internally.

When evoked within the context of an [this] will All instances of an o

Every function has Function.prototype for it's internal prototype property.

Calling a property (eg .move) of an instance of an object (eg Slider) will first look in the Constructor for that property, which is actually getting to the property through the instance's prototype property. If not found, the call looks for the property definition (of .move) in a separate definition of that property (eg. Slider.prototype.move =...) , which internally is getting the definition from the global prototype property of Object.

You can redefine a function name to some other function or value later in the program.

When you create a new instance of a function, the

function Slider(dir) {

 this.dir = dir;

} Slider.prototype.move = function(d, d2) {

 alert(this.dir + ", " + d + d2);

};

var m = new Slider("h").move; m (1); // Explain the result. // new creates new instance of Slider just long enough to access the move function. Then, the Slider instance just created could be be garbage-collected, but the move function will live on in m.

var s = new Slider("h"); s.move(1)

// cannot say move(x, y) because, without a qualifier (eg s.) then the interpreter will look for move, which would have to exist as a defined var, fx, etc., which it does not.

// equivalent to s.move(1), except that s is not the base object of move, it's passed INTO move, so that any 'this' inside the definition of move will refer to s. move can be defined inside or outside the constructor, and result will be same. m.call(s, 1);

// say you want to convert a nodelist into an array, in order to get array sort and other list-management functions. You can use the slice function of Array using call: var nodelist = document.getelementsbytagname("*"); //or document.body.descendents?; var NodeArray = Array.prototype.slice.call(nodelist);

// slice converts the elements into an array of elementNodes (which is defined in the DOM host environment).

// say you have an array, and you need to pass it into a function as separate parameters. Use "apply" instead of "call". var myArray = [4,67]; m.apply(s, myArray);


// math is static methods, and does not have a prototype. var mon = new Date ("2/5/13"); var tue = new Date ("1/8/12"); var myDates = [mon, tue]; // Math methods do not need a 'this' var latest = Math.max.apply(null, myDates); var latestDate = new Date (latest);