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.
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.
Comment