With JavaScript Lint, you can check all your JavaScript source code for common mistakes without actually running the script or opening the web page.
JavaScript Lint holds an advantage over competing lints because it is based on the JavaScript engine for the Firefox browser. This provides a robust framework that can not only check JavaScript syntax but also examine the coding techniques used in the script and warn against questionable practices.
To see more what it does, go to http://www.javascriptlint.com/index.htm
Web 2.0, Ruby, Rails, TDD, BDD,
rSpec, XMPP, BOSH, REST,
Javascript, Ajax, Google Maps API,
Google Gears API, CSS,
Java, C#, Social Network, RMagick, Nginx
Thursday, August 28, 2008
Tuesday, August 19, 2008
JSONP
It seems not many people know about this. This is the name of the trick that posted in http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/ since 2005-12-05, three years ago.
JSON with Padding, or simply JSONP is a technique that allows you to transfer JSON data across multiple domains.
The way JSONP works is simple, but requires a little bit of server-side cooperation. Basically, the idea is that you let the client decide on a small chunk of arbitrary text to prepend to the JSON document, and you wrap it in parentheses to create a valid JavaScript document (and possibly a valid function call).
The client decides on the arbitrary prepended text by using a query argument named jsonp with the text to prepend. Simple! With an empty jsonp argument, the result document is simply JSON wrapped in parentheses.
JSON with Padding, or simply JSONP is a technique that allows you to transfer JSON data across multiple domains.
The way JSONP works is simple, but requires a little bit of server-side cooperation. Basically, the idea is that you let the client decide on a small chunk of arbitrary text to prepend to the JSON document, and you wrap it in parentheses to create a valid JavaScript document (and possibly a valid function call).
The client decides on the arbitrary prepended text by using a query argument named jsonp with the text to prepend. Simple! With an empty jsonp argument, the result document is simply JSON wrapped in parentheses.
Labels:
javascript
Wednesday, August 13, 2008
Invoking Overridden Methods in Javascript
When a subclass defines a method that has the same name as a method in the superclass, the subclass overrides that method. This is a relatively common thing to do when creating subclasses of existing classes. Anytime you define a toString( ) method for a class, you override the toString( ) method of Object, for example.
A method that overrides another often wants to augment the functionality of the overridden method instead of replacing it altogether. To do that, a method must be able to invoke the method that it overrides. Invoking an overridden method is more awkward than invoking a superclass constructor, however.
Let's consider an example. Suppose the Rectangle class had defined a toString( ) method (as it should have in the first place):
PositionedRectangle is a simple enough class that its toString( ) method can just return the values of all properties. But for the sake of example, let's handle the position properties and delegate to its superclass for the width and height properties. Here is what the code might look like:
The superclass's implementation of toString( ) is a property of the superclass's prototype object. Note that you can't invoke it directly. Invoke it with apply( ) so that you can specify the object on which it should be called.
A method that overrides another often wants to augment the functionality of the overridden method instead of replacing it altogether. To do that, a method must be able to invoke the method that it overrides. Invoking an overridden method is more awkward than invoking a superclass constructor, however.
Let's consider an example. Suppose the Rectangle class had defined a toString( ) method (as it should have in the first place):
Rectangle.prototype.toString = function( ) {
return "[" + this.width + "," + this.height + "]";
}
PositionedRectangle is a simple enough class that its toString( ) method can just return the values of all properties. But for the sake of example, let's handle the position properties and delegate to its superclass for the width and height properties. Here is what the code might look like:
PositionedRectangle.prototype.toString = function( ) {
return "(" + this.x + "," + this.y + ") " + // our fields
Rectangle.prototype.toString.apply(this); // chain to superclass
}
The superclass's implementation of toString( ) is a property of the superclass's prototype object. Note that you can't invoke it directly. Invoke it with apply( ) so that you can specify the object on which it should be called.
Labels:
javascript
Monday, August 11, 2008
Checking Property Existence in Javascript
The in operator can be used to test for the existence of a property. The left side of this operator should be the name of the property as a string. The right side should be the object to be tested. For example:
The in operator is not often needed, however, because if you query a property that does not exist, the undefined value is returned. Thus, the above code can usually be written like this:
Note that it is possible for a property to exist but still be undefined. For example, if you write:
the property x exists but has no value.
// If o has a property named "x", then set it
if ("x" in o) o.x = 1;
The in operator is not often needed, however, because if you query a property that does not exist, the undefined value is returned. Thus, the above code can usually be written like this:
// If the property x exists and is not undefined, set it.
if (o.x !== undefined) o.x = 1;
Note that it is possible for a property to exist but still be undefined. For example, if you write:
o.x = undefined
the property x exists but has no value.
// If the doSomething property exists and is not null or undefined,
// then assume it is a function and invoke it!
if (o.doSomething) o.doSomething();
Labels:
javascript
Subscribe to:
Posts (Atom)