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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    When to declare 'new' object?

    Why and when I should use 'new' to declare a new object or variable. The book I'm reading doesn't explain this, it just says how to do it.
    When I first declare a variable, it's already new since it's the first mention of that variable, so why bother to write 'new'?

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Let's say you have the following code:

    Code:
    function MyConstructor() {
      // no code inside
    }
    
    var x = MyConstructor();
    
    var y = new MyConstructor();
    
    alert(x);  // alerts undefined
    alert(y);  // alerts [object Object]
    In the first case, the case with x, all you are doing is calling a function. Since the function does not explicitly return a value, it returns undefined.

    In the second vase, the case with y, you are telling the interpreter to allocate memory for an object and apply the code in MyConstructor to it, where any instance of the keyword this points to this object.
    Trinithis

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    If you are declaring a variable to be a type that is already defined- a string, or a number, say, the constructor is implied but not necessary-

    var s='Some text';
    var n= 100;

    var s=new String('Some text');
    var n= new Number(100);

    When you create an instance of a custom object, whose constructor
    you have defined, you need the new keyword-

    var c=new Collection(/^h\d$/)

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Actually these two versions do not generate the same code:

    Code:
    Quote: mrhoo
    
    var s='Some text';
    var n= 100;
    
    var s=new String('Some text');
    var n= new Number(100);
    The former version creates primitives. Primitives are passed by value.
    Edit:
    Though I think string primitives might behave oddly. I don't really remember how those are dealt with.


    The latter version creates objects and assign references to them to s and n, and the references are passed by value (meaning the objects at the end of the references are implicitly passed by reference).


    Almost always the primitive versions are better than the object wrapper versions for performance reasons.
    Last edited by Trinithis; 05-01-2008 at 06:20 AM.
    Trinithis

  • #5
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay I understand a little... but why do I see other examples in my book like:
    var sObject = new String("Sample string");

    if you're going to allocate memory to a var so the var is usable later on, then why assign it a value immediately? If you're assigning the value immediately, then why do you need 'new'?

    How is that example code different from:
    var sObject = "Sample string";

  • #6
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Just follow the following general rule.

    When creating variables of any of the following types:
    • Boolean
    • Function
    • Number
    • String

    Don't create it as an object (aka via new).


    Don't use:
    Code:
    var abc = new XXX(...);
    Just do this:
    Code:
    var abc = ...
    Otherwise, new is 99.9% of the time what you want.
    Trinithis


  •  

    Posting Permissions

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