[jQuery] Classes
[jQuery] Classes
In many cases, creating a class or class-like object at all is overkill.
There are often simpler ways to solve problems using closures and functional
programming. You will see a lot of this in jQuery and code that uses jQuery.
Here is how I would port the Prototype code. It doesn't use a class at all,
but barring any bugs, I'm pretty sure it does exactly the same thing with
one exception - it inserts both the BR and A elements using the DOM instead
of using innerHTML for the BR tag. It's about half the code and much
simpler:
function User( name ) {
document.write( '<div id="msg">Welcome ' + name + '!</div>' );
setTimeout( function() {
$('#msg').append(
$.BR(),
$.A({ href: "account.php?u=" + name }, name + "'s Account" )
);
}, 200 );
}
Note that you can use either:
new User("Sean");
Or:
User("Sean");
It makes no difference which. Because the code doesn't use a class, you'd
typically leave out the "new".
The code uses jQuery and my DOM creation plugin:
http://mg.to/2006/02/27/easy-dom-creation-for-jquery-and-prototype
(Depending on the version of the DOM plugin, you may need to write $.BR({})
instead of $.BR() - try them both.)
Now, I admit that doesn't answer your question about how to use class-like
inheritance with jQuery. Post some code that benefits from using a class and
we'll figure something out.
-Mike