Re: Newbie question about "NEW: Keyword



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
.



Relevant Pages

  • Re: array dimensioning
    ... Could someone please review the thread: "Array Declaration Problem" and my ... then with the Preserve keyword you can change only the upper bound of ... In this case, if you try to change either bound of the 1st dimension, ...
    (microsoft.public.excel.programming)
  • Re: perl grep problem
    ... > basically the script reads all the data from files in a directory into ... > an array. ... > works fine) and $ARGVis the user input word to search for. ... > inputs the keyword to search for and we loop step 2 as many times as ...
    (comp.lang.perl.misc)
  • Re: C/C++ typedef in C#
    ... in some situation typedef in C# can be done using "using" keyword ... it makes an alias for any type name ONLY locally to file where is has ... When porting please remember that .NET Framework is single class ... array of value objects is fully initialized after creaton of array it ...
    (rec.games.roguelike.development)
  • Re: KEYWORDS AND WHILE
    ... |>>> redundant parentheses and the fact that procedures are "nouns" rather ... |> analysis of languages where language keywords were not reserved. ... You can't tell whether WHILE is a keyword ... or a variable (assuming that you have an array called WHILE). ...
    (comp.lang.pl1)

Loading