Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Amazing Functions in JavaScript

  1. #1
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default Amazing Functions in JavaScript

    Here is one amazing function that is a use in JavaScript that one may LOVE to use instead of document.getElementById():

    Code:
    <!-- Makes coding SO much easier -->
    function getObj(obj)
    {
    if(document.all)            return document.all[obj];
    else if(document.layers) return document.layers[obj];
    }
    I may have accidently reused it, but I have some others that you guys might like:

    Code:
    <!-- These are plain stealing and calling them your own, however it is convenient in writing shorter code -->
    function random()
    {
    return Math.random();
    }
    
    function acos(int)
    {
    return Math.acos(int);
    }
    Code:
    <!-- Love these -->
    function setVal(obj,str)
    {
    getObj(obj).value = str;
    }
    
    function setHtml(obj,str)
    {
    getObj(obj).innerHTML = str;
    }
    See what functions can do for you? Feel free to take these and make them your own. Change 'em up too!

    -magicyte

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by magicyte View Post
    Here is one amazing function that is a use in JavaScript that one may LOVE to use instead of document.getElementById():
    I can't see why, neither of those two methods are required in version 5+ browsers. No current browser uses document.layers, and if you were to have some antique around that did, it should be the first test, not the second as you have it there - to avoid possible errors.

    Seems like a bit of extra typing at first to save little, combined with having to memorize the other way to do things. In the case of the first one, it will fail to retrieve the element in many cases due to its lack of support for document.getElementById(). And it really isn't getting an object (when it works at all) as its name implies unless that object happens to be an element.

    Functions are great if they work, but generally more than a few lines are required to deliver much utility.
    Last edited by jscheuer1; 07-31-2008 at 06:14 PM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default getObj()

    I am sorry if what I posted isn't useful for those of you with IE 5+ (I have IE 4 w/ WINDOWS XP). I just posted it because I find it easier to use than document.getElementById(), considering I use it a lot.

    You are right. getObj doesn't get an object. But, man oh man, I wish that JavaScript was compiled so that everything could be compatible with other browser applications.

    -magicyte

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I find it easier to use than document.getElementById(), considering I use it a lot.
    I just define this at the beginning of my file:

    Code:
    var $ = function(e) {return document.getElementById(e);};
    Simple.
    - Mike

  5. #5
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Does it involve PHP? I noticed you used $...

    -magicyte

  6. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Quote Originally Posted by magicyte View Post
    Does it involve PHP? I noticed you used $...

    -magicyte
    You can declare a variable in JS whose name is $ need not be in PHP

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by magicyte View Post
    I have IE 4 w/ WINDOWS XP
    Um, Windows XP came with IE 6 I think, at least 5 or 5.5, what happened? Most pages on the web won't render well, if at all in IE 4, and most scripts will either not work, or will degrade to a very simplistic level. IE 4 will often report script errors on pages that technically have none. Many commercial sites require at least IE 5.5 if you are using IE. How on earth do you get by with such a relic?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I just posted it because I find it easier to use than document.getElementById(), considering I use it a lot.
    You shouldn't. Both the methods it is capable of using to look up an element are deprecated. You should use document.getElementById(). You may assign a reference with a shorter name if you like. However, that name should not be $.
    You can declare a variable in JS whose name is $ need not be in PHP
    You can, but should not. Use of the $ character in identifiers is reserved for machine-generated code (ECMA-262 section 7.6).

    My own favourite utility functions are:
    Code:
    function map(f, a) {
      for (var i = a.length - 1, r = []; i >= 0; --i)
        r[i] = f(a[i], i);
    
      return r;
    }
    
    function filter(f, a) {
      if (typeof f === "string")
        f = (function(f) {
          return function(a) {
            return !!a[f];
          };
        })(f);
    
      for (var i = 0, r; i < a.length; ++i)
        if (f(a[i]))
          r.push(a[i]);
    
      return r;
    }
    
    function reduce(f, a, t) {
      if (!a.length)
        return t;
    
      t = t || a[0];
    
      for (var i = 0, n = a.length; i < n; ++i)
        t = f(t, a[i]);
    
      return t;
    }
    
    function compose(f1, f2) {
      return function() {
        return f1(f2.apply(this, Array.prototype.slice.call(arguments)));
      };
    }
    
    function bind(obj, fun, args) {
      return function() {
        if (obj === true)
          obj = this;
    
        var f = typeof fun === "string" ? obj[fun] : fun;
    
        return f.apply(obj, Array.prototype.slice.call(args || [])
            .concat(Array.prototype.slice.call(arguments)));
      };
    }
    ... as well as a bunch of curryable operator functions. I have a few more, nicely modularised, but they're all on my desktop, which is currently kaput. If I found myself using many of these, I suspect I would switch over to the Functional Javascript library.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Code:
    <!-- Makes coding SO much easier -->
    function getObj(obj)
    {
    if(document.all)            return document.all[obj];
    else if(document.layers) return document.layers[obj];
    }
    Have you ever used anything but I.E.? The above function would return undefined in Mozilla and others.

    Code:
    function random()
    {
    return Math.random();
    }
    
    function acos(int)
    {
    return Math.acos(int);
    }
    These are pointless and only de-objectify an object orientated language.
    Code:
    <!-- Love these -->
    function setVal(obj,str)
    {
    getObj(obj).value = str;
    }
    
    function setHtml(obj,str)
    {
    getObj(obj).innerHTML = str;
    }
    See what functions can do for you?
    Absolutely nothing; The phrase "ashtray on a motorbike" comes to mind.

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Quote Originally Posted by Twey
    You can, but should not. Use of the $ character in identifiers is reserved for machine-generated code (ECMA-262 section 7.6).
    Okay, that's fair. But can you show me an example of the machine-generated reserved code?
    - Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •