467,878 Members | 1,264 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,878 developers. It's quick & easy.

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

Dec 3 '05 #1
  • viewed: 1976
Share:
6 Replies
VK

Rufnex wrote:
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;
}


[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?

Dec 3 '05 #2
> 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.

Dec 3 '05 #3
VK

Rufnex wrote:
i will init the object

var o = obj();

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


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

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

Dec 3 '05 #4
Hey. thank you! The throw solution works fine for me ;o)

Dec 3 '05 #5
VK wrote:
Rufnex wrote:
i will init the object

var o = obj();

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


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

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


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_code
//
// 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
Dec 3 '05 #6
Thomas 'PointedEars' Lahn wrote:
However, as I wrote above, that requires exception support
(JavaScript 1.5, JScript 5.0, ECMAScript 3).
Read: "at least a Mozilla/5.0 based browser, IE/5.0 for Windows,
or Opera 6.0".
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
catch (e if e instanceof Error) // replace Error with any constructor
{
// handle exception
}

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

PointedEars
Dec 3 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nimmi Srivastav | last post: by
5 posts views Thread by | last post: by
16 posts views Thread by Martin Vorbrodt | last post: by
15 posts views Thread by LuB | last post: by
6 posts views Thread by Nethali | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.