Re: Newbie question about "NEW: Keyword
- From: Thomas 'PointedEars' Lahn <PointedEars@xxxxxx>
- Date: Wed, 15 Mar 2006 19:52:01 +0100
jshanman wrote:
MS wrote:
My original question - so anytime I want to create a new object I use
the NEW keyword?
Yes, anytime you want a NEW object (custom,Date,Error,Image,Array,
etc), use the NEW keyword.
Wrong if this absolute, as was already explained. Would you please care
to read before you post?
I can create any object with any name, right? Why would I want to do
this as part of Javascripting?
Say your creating a page with lots of addresses. You can create a
contructor function for your records...
function CreateRecord(fname,lname,add1,add2,city,state,zip,country) {
this.fname = fname;
this.lname = lname;
this.add1 = add1;
this.add2 = add2;
this.city = city;
this.state = state;
this.zip = zip;
this.country = country;
this.GetFullName = function() {return fname+" "+lname;}
}
//You then access this custom object contructor like this.
var AllRecordsObj = new Object();
AllRecordsObj["Record1"] = new CreateRecord("john","smith","12345
Main","Apt 10","NY","NY","55555","USA");
AllRecordsObj["Record2"] = new CreateRecord("jill","smithy","54321
Main","Apt 9","NY","NY","55555","USA");
However, the better approach here is to pass an object to the constructor
so you do not have to provide all the data for each record (not even empty
one).
The constructor identifier, which is the only identifier here that should
start with a capital letter (just code style), should identify the object,
not the action; it is obvious that something (an object) is created by it.
And the method GetFullName() should be a prototype method, so that its code
can be reused by objects, and there is only one Function object created for
it (runtime and memory efficiency). It MUST either be a prototype method,
or a reference to the calling object's properties must be used in it
instead of a reference to the arguments; otherwise the closure will not
allow it to return any other but the initial data.
function Record(data)
{
// the empty string is the default value for all properties, see below
this.fname = data.fname || "";
this.lname = data.lname || "";
this.add1 = data.add1 || "";
this.add2 = data.add2 || "";
this.city = data.city || "";
this.state = data.state || "";
this.zip = data.zip || "";
// if no value is specified or it is a false-value (such
// as the empty string), the default/ ("USA") is assigned
this.country = data.country || "USA";
}
Record.prototype.getFullName = function()
{
return this.fname + " " + this.lname;
};
Instead of
var AllRecordsObj = new Object();
AllRecordsObj["Record1"] = ...
[...]
one can safely use
var records = {
// this person lives in the USA despite it is not explicitly specified
Record1: new Record({
fname: "john",
lname: "smith",
add1: "12345 Main",
add2: "Apt 10",
city: "NY",
state: "NY",
zip: "55555"
}),
Record2: new Record({
// ...
})
};
nowadays (JavaScript 1.3+ [NN 4.06+, 1998+], JScript 3.0+
[IE 4.0+, Oct. 1997+], ECMAScript Ed. 3+ [Dec. 1999+]).
However, an Array object probably is more appropriate here,
to have each record "numbered automatically":
var records = [
new Record({
fname: "john",
lname: "smith",
add1: "12345 Main",
add2: "Apt 10",
city: "NY",
state: "NY",
zip: "55555"
}),
new Record({
// ...
})
];
//now loop through write all the first names...
for (prop in AllRecordsObj) {
This will iterate through all enumerable properties,
which is sometimes not desired. Search the archives.
The array data structure, encapsulated by the Array
object created through the above literal notation,
can be iterated as follows:
for (var i = 0, len = records.length; i < len; i++)
{
// ... records[i] ...
}
PointedEars
.
- References:
- Newbie question about "NEW: Keyword
- From: mscurto
- Re: Newbie question about "NEW: Keyword
- From: VK
- Re: Newbie question about "NEW: Keyword
- From: MS
- Re: Newbie question about "NEW: Keyword
- From: Lasse Reichstein Nielsen
- Re: Newbie question about "NEW: Keyword
- From: Thomas 'PointedEars' Lahn
- Re: Newbie question about "NEW: Keyword
- From: Lasse Reichstein Nielsen
- Re: Newbie question about "NEW: Keyword
- From: Thomas 'PointedEars' Lahn
- Re: Newbie question about "NEW: Keyword
- From: MS
- Re: Newbie question about "NEW: Keyword
- From: jshanman
- Newbie question about "NEW: Keyword
- Prev by Date: Re: javascript: in location bar
- Next by Date: Re: How to preprocess the html address before opening it?
- Previous by thread: Re: Newbie question about "NEW: Keyword
- Next by thread: Spacing Words in Header
- Index(es):
Relevant Pages
|
Loading