Delete a Object inside a constructor while init?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rufnex

    Delete a Object inside a constructor while init?

    Hi,

    is there a delete for a object inside the constructor, while i init it?
    i will try something like that:

    var obj = function(a)
    {
    if (!a) delete this;
    this.a = a;
    }

    Thx for help !

    Rufnex

  • VK

    #2
    Re: Delete a Object inside a constructor while init?


    Rufnex wrote:[color=blue]
    > Hi,
    >
    > is there a delete for a object inside the constructor, while i init it?
    > i will try something like that:
    >
    > var obj = function(a)
    > {
    > if (!a) delete this;
    > this.a = a;
    > }[/color]

    [this] cannot be a subject of an operation, only an object. You cannot
    delete it, multiply, divide, assign a value to etc.

    You can use [this] only for reference and in the right side of
    assignments:
    var something = this;

    What are you trying to do?

    Comment

    • Rufnex

      #3
      Re: Delete a Object inside a constructor while init?

      > What are you trying to do?

      i will init the object

      var o = obj();

      and if the argument isn't exist, i will destroy the oject automaticly.

      Comment

      • VK

        #4
        Re: Delete a Object inside a constructor while init?


        Rufnex wrote:[color=blue]
        > i will init the object
        >
        > var o = obj();
        >
        > and if the argument isn't exist, i will destroy the oject automaticly.[/color]

        You don't need to be so rude with the caller :-)

        myConctructor(r equiredArg) {
        if (typeof requiredArg == 'undefined') {
        return null;
        // or:
        // throw new Error('Argument is not optional');
        }
        else {
        // construct an instance
        }
        }

        Comment

        • Rufnex

          #5
          Re: Delete a Object inside a constructor while init?

          Hey. thank you! The throw solution works fine for me ;o)

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Delete a Object inside a constructor while init?

            VK wrote:
            [color=blue]
            > Rufnex wrote:[color=green]
            >> i will init the object
            >>
            >> var o = obj();
            >>
            >> and if the argument isn't exist, i will destroy the oject automaticly.[/color]
            >
            > You don't need to be so rude with the caller :-)
            >
            > myConctructor(r equiredArg) {
            > if (typeof requiredArg == 'undefined') {
            > return null;
            > // or:
            > // throw new Error('Argument is not optional');
            > }
            > else {
            > // construct an instance
            > }
            > }[/color]

            This will not work as assumed by you. Not considering that the `function'
            keyword is missing, returning a value explicitly from or throwing an
            exception (which would either break the code where not supported or require
            an adequate fallback) in the constructor will _not_ prevent an object from
            being created.

            Simple test case:

            function Foo(x)
            {
            return x; // if you `throw' an exception, the return value
            // would be `undefined', as if it was not thrown;
            // the `new' keyword makes the difference
            this.foo = "bar";
            }

            var x = new Foo(null); // try with any value as argument

            // in JavaScript 1.5:
            //
            // "[Object object]
            // object
            // $constructor_co de
            //
            // undefined"
            alert([x, typeof x, x.constructor, x.foo, typeof x.foo].join("\n"));

            Of course you could throw the exception and handle it later:

            try
            {
            var x = new Foo(...);
            }
            catch (e)
            {
            // handle exception
            }

            However, as I wrote above, that requires exception support (JavaScript 1.5,
            JScript 5.0, ECMAScript 3). Especially, to identify the specific exception
            thrown, it could be required that the syntax

            try
            {
            var x = new Foo();
            }
            catch (e if instanceOf Error) // replace Error with any constructor
            {
            // handle exception
            }

            is supported, which it is only in JavaScript 1.5+.

            Therefore, for a general solution, I recommend that the returned reference
            is checked for distinct true-value properties that should have been created
            by the constructor (after the last conditional `return' or `throw'). If
            not present or a false-value, the creation can be considered "failed" and
            the respective entity referencing the object should be subject to the
            `delete' operation (has no effect on declared variables) or should be
            assigned `null'. The rest has to be left to the Garbage Collector.


            PointedEars

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Delete a Object inside a constructor while init?

              Thomas 'PointedEars' Lahn wrote:
              [color=blue]
              > However, as I wrote above, that requires exception support
              > (JavaScript 1.5, JScript 5.0, ECMAScript 3).[/color]

              Read: "at least a Mozilla/5.0 based browser, IE/5.0 for Windows,
              or Opera 6.0".
              [color=blue]
              > Especially, to identify the specific exception thrown,
              > it could be required that the syntax
              >
              > try
              > {
              > var x = new Foo();
              > }
              > catch (e if instanceOf Error) // replace Error with any constructor[/color]

              catch (e if e instanceof Error) // replace Error with any constructor
              [color=blue]
              > {
              > // handle exception
              > }
              >
              > is supported, which it is only in JavaScript 1.5+.[/color]


              PointedEars

              Comment

              Working...