Eliminating Redundancy when Constructing Javascript Objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PeterAlt
    New Member
    • Sep 2007
    • 2

    Eliminating Redundancy when Constructing Javascript Objects

    Hello all! This is my first post here. I've been going crazy lately trying to master the art of working with and creating my own objects with Javascript. I see the HUGE power potential of object orientation within the scripts I could write, but I am having a real difficult time grasping what appears to be extremely simple concepts. As of now, I've pretty much got it, but not completely.

    Okay, here is an example of a segment of code I wrote (I will follow that with my specific question):

    [CODE=javascript]function w(tag) {
    this.t = tag; this.s = "<"+tag+">" ; this.e = "</"+tag+">";
    this.c = function (con) { return this.s+con+this .e; }
    this.d = function (sty, con) { return "<"+this.t+ " "+sty+">"+con+t his.e; }
    this.i = function (src) { return "<"+this.t+ " src='"+src+"'>" ; }
    this.j = function (src, sty) { return "<"+this.t+ " src='"+src+"' "+sty+">"; }
    }
    ul=new w("ul"); li=new w("li"); tr=new w("tr"); th=new w("th"); td=new w("td");
    p=new w("p"); table=new w("table"); img=new w("img");
    [/CODE]
    I wrote the above code as a shortcut to wrap content with HTML. Originally, this code was multitudes larger than it is now. Each time I get a better understanding of javascript object construction, I re-wrote the code above over and over again, each time shorting it. But, I know it is possible to shorten it even further. Here's precisely what I am talking about:

    [CODE=javascript] ul=new w("ul"); li=new w("li"); tr=new w("tr"); th=new w("th"); td=new w("td");
    p=new w("p"); table=new w("table"); img=new w("img");
    [/CODE]
    ...Seems VERY redunant! I want those lines to look like this...

    [CODE=javascript] ul=new w(); li=new w(); tr=new w(); th=new w(); td=new w(); p=new w();
    table=new w(); img=new w();
    [/CODE]
    The constructor SHOULD be able to know what's calling it and use that information to determine what 'tag' (in my example) I want each new 'w' (in my example) to use for these new objects being created!

    Also, here's another example:

    [CODE=javascript] function example(name) {
    this.name=name;
    }
    [/CODE]
    In the above example, the variable "name" is used 3 times. Tripple Redunant (in my opinion). There must be a way to reduce this redundancy as well!

    I look forward to your replies! Thank you in advance!

    Peter A.
    Last edited by acoder; Sep 6 '07, 11:13 AM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN!

    Changed the thread title. Please use a good thread title. Also remember to use code tags when posting code. Thanks!

    Comment

    • RBPierce
      New Member
      • Sep 2007
      • 7

      #3
      I think you are missing out on the purpose of Objects. Objects are black boxes, factories- you put something into them, you get something out of them. The object is blind to the context of where it gets stored (the name of the created object)- the input (parameters) are all it cares about. Thus, your statement that:
      The constructor SHOULD be able to know what's calling it and use that information to determine what 'tag' (in my example) I want each new 'w' (in my example) to use for these new objects being created!
      doesn't make sense. It doesn't matter to the Object what you NAME the object- what it cares about is what you GIVE it.

      (How's that for anthromorphizin g code?)

      (Yes, I know that there are caveats where the Object MAY care about context wrt global vars and the like, but not here- not with the name of the instantiated object. )

      Comment

      Working...