codegoboom
08-28-2004, 08:08 AM
Are these essentially the same, except for:
#1 requires the 'new' operator & 'this' keyword
#2 requires the 'return' statement
-----------------------------------
function myObject1(string)
{
this.message = string;
}
alert(new myObject1('hi').message);
-----------------------------------
function myObject2(string)
{
return {message:string};
}
alert(myObject2('hi').message);
-----------------------------------
It seems like intrinsic objects would be constructed more like #2, because they can be invoked without the 'new' operator... true?
jamescover
08-28-2004, 09:09 AM
function myObject(message){
this.message = message;
}
myNewObject = new myObject('hi')
alert(myNewObject.message);
bobsNewObject = new myObject('bye')
alert (BobsNewObject.message);
codegoboom
08-28-2004, 09:28 AM
Aha... so what are you getting at, btw?
codegoboom
08-28-2004, 11:42 AM
function jamescover(question)
{
return {answer:question}
}
var paging = alert;
paging(jamescover("What was your point?").answer); ;)
link relevant to this thread, Object Hierarchy and Inheritance in JavaScript:
http://developer.netscape.com/docs/manuals/communicator/jsobj/
codegoboom
08-28-2004, 12:15 PM
That's cool and all, but seems to have no discussion pertaining to my questions, so...
ok.. other not so relevant links but links relevant to people who would want to understand your question better:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/ident.html#1009450
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/obj.html
* crawls under a rock *
codegoboom
08-28-2004, 12:37 PM
Those are closer to what I was looking for; thanks. :)
I'll let the rest be a matter of trust in my own conclusions...
jamescover
08-29-2004, 01:43 AM
Here's some old stuff from DevEdge:
The new operator:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html
Functions:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html
function jamescover(question)
{
return {answer:question}
}
var paging = alert;
paging(jamescover("What was your point?").answer);
Sorry, about not answering. I was really tired last night, and didn't want to get into anything too involved. In the example that I gave, I was just implying that there was a difference between an object, an object name, and a reference (variable) to an object.
I don't think your second example fits the user-defined object construct, like in the example I posted previously.
-james
jamescover
08-29-2004, 01:48 AM
Hey...
disregard the links above--they are in frames, and the anchors don't work. Below is a direct link:
Functions:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/function.html
New operator:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/ops.html#1055898
-james
codegoboom
08-29-2004, 04:07 AM
I don't think your second example fits the user-defined object construct, like in the example I posted previously.
I don't know what that means, but thank you for the pointers... (was only kidding with 'answer:question' thing--playing off of your enigmatic reply ;)).
Roy Sinclair
08-30-2004, 07:21 PM
Maybe I'm just going to put my foot in my mouth but your examples are so simplistic that they underperform and don't leave any room for a difference in how they work to be demonstrated. This seems to work ok:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Testing</title>
</head>
<body>
<script type="text/javascript">
function myObject1(string)
{
this.message = string;
}
var s1 = new myObject1('hi from myObject1');
alert(s1.message)
//-----------------------------------
function myObject2(string)
{
return {message:string};
}
var s2 = myObject2('hi from myObject2');
alert(s2.message);
</script>
</body>
</html>
and this also works so they do seem to be functionally equivalent.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Testing</title>
</head>
<body>
<script type="text/javascript">
function myObject1(string1,string2)
{
this.firststring = string1;
this.secondstring = string2;
}
var s1 = new myObject1('hi from myObject1','Good to see you');
alert(s1.firststring + '\n' + s1.secondstring);
//-----------------------------------
function myObject2(string1,string2)
{
return {firststring:string1,secondstring:string2};
}
var s2 = myObject2('hi from myObject2','Good to see you');
alert(s2.firststring + '\n' + s2.secondstring);
</script>
</body>
</html>
With the first form though what you're doing seems to be more apparent so IMO it's preferable. I will leave open the option that there's probably a good case for using the second form in some cases.
codegoboom
08-30-2004, 11:55 PM
Thanks.
Yup, I somehow presented this question in such a way that its real purpose could only be comprehended by me (something I seem to have a talent for).
At any rate, I've at least figured out the futility of asking... :rolleyes: