473,492 Members | 4,301 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Eliminating Redundancy when Constructing Javascript Objects

2 New Member
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):

Expand|Select|Wrap|Line Numbers
  1. function w(tag) {
  2.        this.t = tag; this.s = "<"+tag+">"; this.e = "</"+tag+">";
  3.        this.c = function (con) { return this.s+con+this.e; }
  4.        this.d = function (sty, con) { return "<"+this.t+" "+sty+">"+con+this.e; }
  5.        this.i = function (src) { return "<"+this.t+" src='"+src+"'>"; }
  6.        this.j = function (src, sty) { return "<"+this.t+" src='"+src+"' "+sty+">"; }
  7.    }
  8.    ul=new w("ul"); li=new w("li"); tr=new w("tr"); th=new w("th"); td=new w("td");
  9.    p=new w("p"); table=new w("table"); img=new w("img");
  10.  
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:

Expand|Select|Wrap|Line Numbers
  1.    ul=new w("ul"); li=new w("li"); tr=new w("tr"); th=new w("th"); td=new w("td");
  2.    p=new w("p"); table=new w("table"); img=new w("img");
  3.  
...Seems VERY redunant! I want those lines to look like this...

Expand|Select|Wrap|Line Numbers
  1.    ul=new w(); li=new w(); tr=new w(); th=new w(); td=new w(); p=new w();
  2.    table=new w(); img=new w();
  3.  
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:

Expand|Select|Wrap|Line Numbers
  1.     function example(name) {
  2.          this.name=name;
  3.     }
  4.  
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.
Sep 6 '07 #1
2 1420
acoder
16,027 Recognized Expert Moderator MVP
Welcome to TSDN!

Changed the thread title. Please use a good thread title. Also remember to use code tags when posting code. Thanks!
Sep 6 '07 #2
RBPierce
7 New Member
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 anthromorphizing 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. )
Sep 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
4360
by: Lyle Fairfield | last post by:
I am preparing to write a small utility which will identify redundant objects in an ADP and its SQL db. Suppose my code finds two objects, A and B, which refer only to each other, that is each...
9
6029
by: Simon | last post by:
Hi, I have written an ActiveX object to resize images and upload them to a database, this all works fine but when I close internet explorer the process iexporer.exe is still running in my task...
8
1762
by: Rasmus Kromann-Larsen | last post by:
The With Conundrum I'm currently writing a master thesis on (preparations for) static analysis of JavaScript, and after investigating the with statement, it only even more evident to me that the...
50
3826
by: Martin Rinehart | last post by:
How much javascript would you have when you decide that minifying is called for?
0
7118
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.