java qr code generator library B.3.3 Borrowing functions from other objects in Java

Drawing QR Code JIS X 0510 in Java B.3.3 Borrowing functions from other objects

B.3.3 Borrowing functions from other objects
Painting Denso QR Bar Code In Java
Using Barcode encoder for Java Control to generate, create Denso QR Bar Code image in Java applications.
www.OnBarcode.com
QR Code 2d Barcode Recognizer In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Giving functions first-class object status alters the capabilities of a language significantly. Furthermore, an understanding of these alterations is important when coding GUI event handling, so most Ajax programmers would do well to understand it. So what are these new capabilities Well, first off, one object can borrow another s function and call it on itself. Let s define a class to represent species of tree in a taxonomic system:
Painting EAN-13 Supplement 5 In Java
Using Barcode creation for Java Control to generate, create EAN13 image in Java applications.
www.OnBarcode.com
Draw GS1 - 12 In Java
Using Barcode creator for Java Control to generate, create UPC A image in Java applications.
www.OnBarcode.com
function Tree(name, leaf, bark){ this.name=name; this.leaf=leaf; this.bark=bark; }
Encode EAN13 In Java
Using Barcode generation for Java Control to generate, create European Article Number 13 image in Java applications.
www.OnBarcode.com
Generating Barcode In Java
Using Barcode generator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Next, we ll add a function to it that provides a quick description of the tree:
Data Matrix Creation In Java
Using Barcode creation for Java Control to generate, create ECC200 image in Java applications.
www.OnBarcode.com
Painting ISBN In Java
Using Barcode creator for Java Control to generate, create ISBN - 13 image in Java applications.
www.OnBarcode.com
Tree.prototype.describe=function(){ return this.name+": leaf="+this.leaf+", bark="+this.bark; }
Quick Response Code Scanner In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Create Quick Response Code In Visual C#.NET
Using Barcode maker for .NET Control to generate, create QR image in .NET applications.
www.OnBarcode.com
If we now instantiate a Tree object and ask it to describe itself, we get a fairly predictable response:
QR Code JIS X 0510 Maker In Java
Using Barcode printer for Android Control to generate, create QR Code JIS X 0510 image in Android applications.
www.OnBarcode.com
Painting PDF417 In None
Using Barcode generator for Font Control to generate, create PDF-417 2d barcode image in Font applications.
www.OnBarcode.com
var Beech=new Tree("green, serrated edge","smooth"); alert(Beech.describe());
Read Barcode In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
GS1 - 12 Generator In Visual Basic .NET
Using Barcode generation for .NET Control to generate, create Universal Product Code version A image in .NET applications.
www.OnBarcode.com
The alert box will display the text Beech: leaf=green, serrated edge, bark=smooth. So far, so good. Now let us define a class to represent a dog:
Decode Barcode In Visual Basic .NET
Using Barcode Control SDK for .NET Control to generate, create, read, scan barcode image in .NET framework applications.
www.OnBarcode.com
UCC.EAN - 128 Maker In None
Using Barcode printer for Software Control to generate, create USS-128 image in Software applications.
www.OnBarcode.com
function Dog(name,bark){ this.name=name; this.bark=bark; }
Code 128B Creation In None
Using Barcode encoder for Software Control to generate, create Code 128C image in Software applications.
www.OnBarcode.com
PDF-417 2d Barcode Maker In None
Using Barcode drawer for Software Control to generate, create PDF-417 2d barcode image in Software applications.
www.OnBarcode.com
Methods and functions
Code-39 Creator In None
Using Barcode creator for Font Control to generate, create Code-39 image in Font applications.
www.OnBarcode.com
Recognizing UPC Code In VB.NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
and create an instance of our Dog class:
var Snowy=new Dog("snowy","wau! wau!");
Snowy wants to show us his bark, but, although we ve defined it for him, he has no function through which to express it. He can, however, hijack the Tree class s function:
var tmpFunc=Beech.describe; tmpFunc.call(Snowy);
Remember, the first argument to function.call() is the context object, that is, the object that the special variable this will resolve to. The previous code will generate an alert box displaying the text Snowy: leaf=undefined, bark=wau! wau!. Well, it s better than nothing for the poor dog. So what s going on here How can a dog call a function that really belongs to a tree The answer is that the function doesn t belong to the tree. Despite being peppered with references to this, assigning the function to the Tree prototype binds it only inasmuch as it enables us to use the shorter MyTree.describe() notation. Internally, the function is stored as a piece of text that gets evaluated every time it is called, and that allows the meaning of this to differ from one invocation to the next. Borrowing functions is a neat trick that we can use in our own code, but in production-quality code, we d prefer to see someone implement a bark() method for Snowy of his very own. The real reason for discussing this behavior is that when you are writing event-handling code, the web browser will do it for you behind the scenes.
B.3.4 Ajax event handling and function contexts
Ajax event handlers are pretty much the same as most GUI toolkit languages, with specialized categories for mouse and keyboard events, as we saw in chapter 4. Our example uses the onclick handler, fired when a mouse is clicked on a visible element. A full discussion of DHTML event handling is beyond the scope of this book, but let s take the time here to highlight a particular issue that can often trip up the unwary. Event handlers can be declared either as part of the HTML markup; for example
<div id='myDiv' onclick='alert:alert(this.id)'></div>
or programmatically; for example:
APPENDIX B
JavaScript for object-oriented programmers
function clickHandler(){ alert(this.id); } myDiv.onclick=clickHandler;
Note that in the programmatic case, we pass a reference to the Function object (that is no () after the clickHandler). When declaring the function in the HTML, we are effectively declaring an anonymous function inline, equivalent to
myDiv.onclick=function(){ alert(this.id); }
Note that in both cases, the function has no arguments assigned to it, nor is there any way for us to pass in arguments with the mouse click. However, when we click on the DOM element, the function is called with an Event object as the argument and the element itself as the context object. Knowing this can save a lot of grief and puzzlement, particularly when you re writing object-based code. The key source of confusion is that the DOM node is always passed as context, even if the function is attached to the prototype of a different object. In the following example, we define a simple object with an event handler for a visible GUI element that it knows about. We can think of the object as the Model in MVC terms, with the event handler taking the role of Controller, and the DOM element being the View.
function myObj(id,div){ this.id=id; this.div=div; this.div.onclick=this.clickHandler; }
The constructor takes an internal ID and a DOM element, to which it assigns an onclick handler. We define the event handler as follows:
myObj.prototype.clickHandler=function(event){ alert(this.id); }
So, when we click on the GUI element, it will alert us to the ID of that object, right In fact, it won t, because the myObj.clickHandler function will get borrowed by the browser (just as our wayward dog borrowed a method from the tree object in the previous section) and invoked in the context of the element, not the Model object. Since the element happens to have a built-in id property, it will show a value, and, depending on your naming conventions, it may even be the same as the Model object s ID, allowing the misunderstanding to continue for some time. If we want the event handler to refer to the Model object that we ve attached it to, we need another way of passing the reference to that object across. There
Copyright © OnBarcode.com . All rights reserved.