473,382 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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
6 2198
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
16
by: Martin Vorbrodt | last post by:
some butthole asked me an interview question: can you delete "this" pointer in a member function of a class, like this: class C { public: void foo() { delete this; } }; my answer was......
5
by: tom | last post by:
Hi, I'm overriding my operator new and operator delete for two classes, one inherited from the other, so I can use my own memory pool. A simplified version of what I have is below: class...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
6
by: Nethali | last post by:
Hi all, Please explain me the following: I have struct as follows: struct Link { void* data; Link* next;
8
by: chromis | last post by:
Hi, I'm writing a contacts section for a cms on a website, I've decided to write the section in OO code. So far I have my Contacts object and a page structure I would use for a procedural site. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.