Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Object syntax...

    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?
    *this message will self destruct in n-seconds*

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function myObject(message){
    this.message = message;
    }
    myNewObject = new myObject('hi')
    alert(myNewObject.message);

    bobsNewObject = new myObject('bye')
    alert (BobsNewObject.message);
    Last edited by jamescover; 08-28-2004 at 10:24 AM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aha... so what are you getting at, btw?
    *this message will self destruct in n-seconds*

  • #4
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function jamescover(question)
    {
    return {answer:question}
    }
    var paging = alert;
    paging(jamescover("What was your point?").answer)
    ;
    *this message will self destruct in n-seconds*

  • #5
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    link relevant to this thread, Object Hierarchy and Inheritance in JavaScript:
    http://developer.netscape.com/docs/m...nicator/jsobj/

  • #6
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's cool and all, but seems to have no discussion pertaining to my questions, so...
    *this message will self destruct in n-seconds*

  • #7
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok.. other not so relevant links but links relevant to people who would want to understand your question better:
    http://devedge.netscape.com/library/...t.html#1009450
    http://devedge.netscape.com/library/...guide/obj.html

    * crawls under a rock *

  • #8
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Those are closer to what I was looking for; thanks.
    I'll let the rest be a matter of trust in my own conclusions...
    *this message will self destruct in n-seconds*

  • #9
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's some old stuff from DevEdge:


    The new operator:

    http://devedge.netscape.com/library/...ce/frames.html

    Functions:

    http://devedge.netscape.com/library/...ce/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
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #10
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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/.../function.html

    New operator:

    http://devedge.netscape.com/library/...s.html#1055898


    -james
    Last edited by jamescover; 08-29-2004 at 05:24 AM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #11
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jamescover
    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 ).
    *this message will self destruct in n-seconds*

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    <!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.

    Code:
    <!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.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #13
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...
    *this message will self destruct in n-seconds*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •