Talk:JavaScript/Notes/Function: Difference between revisions

From Noisebridge
Jump to navigation Jump to search
No edit summary
No edit summary
Line 32: Line 32:
   this.dir = dir;
   this.dir = dir;
}
}
Slider.prototype.move = function(d) {
Slider.prototype.move = function(d, d2) {
   alert(this.dir + ", " + d);
   alert(this.dir + ", " + d + d2);
};
};


var d = new Slider("h").move;
var m = new Slider("h").move;
d (1); // Explain the result.
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");
var s = new Slider("h");
s.move(1)
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)
// equivalent to s.move(1)
d.call(s, 1);
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:
// 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:
Line 51: Line 54:


// slice converts the elements into an array of elementNodes (which is defined in the DOM host environment).
// 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);

Revision as of 20:32, 8 December 2013

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) 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);