Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 05-07-2009, 02:45 PM Holy crap!
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
I learned a lot about JavaScript today. Check out this little snippet of sweetness:
PHP Code:
<script type="text/javascript">
    (function() {
        
        
window.$w window.wayfarer = function(node) {
            return new 
wayfarer.prototype.elementChain(node);
        }
        
wayfarer.prototype = {
            
elementChain: function(node) {
                var 
arr = [], count 0;
                if (
node.nodeName) {
                    
this[0] = node;
                    
count 1;
                }
                else 
                    if (
typeof node == "array") {
                        
arr node;
                        
count arr.length
                    
}
                    else 
                        if (
typeof node == "object") {
                            for (var 
o in node) {
                                if (
node[o].nodeName) {
                                    
arr.push(node[o]);
                                    
count++;
                                }
                            }
                        }
                
                for(var 
0arr.lengthi++) this[i] = arr[i];
                
this.size count;
                
                return 
this;
            },
            
            
each: function(fn) {
                if (
typeof fn == "function") {
                    for (var 
node 0node this.sizenode++) {
                        
fn.call(this[node]);
                    }
                }
                return 
this;
            }
        }
        
        
wayfarer.prototype.elementChain.prototype wayfarer.prototype;
        
//thanks to jQuery for this idea
        
        
var staticMethods = {
            
inherit: function(obj1obj2) {
                for(var 
o in obj2) {
                    
obj1[o] = obj2[o];
                }
                return 
obj1 || {};
            }
        }
        
        
staticMethods.inherit(wayfarerstaticMethods);
        
    })();
</script> 
What you're looking at is a micro-library. I programmed it to react a lot like jQuery (my framework of choice). Although I did rip some of the ideas more or less directly, almost everything is original.

Obviously, there will eventually be more static methods, but the first static method allows objects to inherit the properties of other objects, which is something you'll find in virtually every framework in one form or another.

The wayfarer object (abbreviated $w) can take any DOM object array, or DOM node as an argument. It then returns the node list in a way that allows prototypes to be built on it, in a special way.

The core function is each(), which anyone who uses jQuery alot knows well. Although the jQuery each() is much more complex, I built mine to function, at least on the most basic level, in the same way. Which means, it takes a function as an arguement, and uses the keyword "this" to refer to the node that is being looped over. My first ever test of this looks like this:

HTML Code:
<html>
<head>
<title>My First Program</title>
</head>

<body>
<a href="/index.php">Home</a><br />
<a href="/about.php">About</a>

<script type="text/javascript">
    $w(document.getElementsByTagName("a")).each(function() {
        this.onclick = function() {
            alert(this.href);
            return false;
        }
    })
</script>
</body>
</html>
Anyhoo...
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
 
Register now for full access!
Old 05-07-2009, 03:29 PM Re: Holy crap!
Sleeping Troll's Avatar
Ultra Talker

Posts: 351
Name: Butch Begy
Trades: 0
Very cool, although I believe most will not appreciate it until they have an application... I happen to be working with such at present, annnd... I could use some advice on using the onclick event (or href whatever works) on objects that I apply move to. I will post my inquiry, hope to hear from you!
__________________
Sleeping Troll, EMUSE, Mind Expansion...Truly serendipity!
Sleeping Troll is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Holy crap!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.45699 seconds with 12 queries