Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > constructor for a derived object is same as that of its parent

Reply
Thread Tools

constructor for a derived object is same as that of its parent

 
 
VK
Guest
Posts: n/a
 
      12-01-2006
satyajit wrote:

> The version of
> ECMAScript standard that I got originally from ECMA's site was ECMA-262
> standard and that did not have reference to instanceof and
> isPrototypeOf(). Getting these concepts from the standard was very
> difficult.


The current standard is ECMA-262 3rd edition, so you must be reading
ECMA-262 2nd edition (same standard but the obsolete edition). The 3rd
(current) edition as available at
<http://www.ecma-international.org/publications/standards/Ecma-262.htm>

Please note though that - despite of what the name implies - this
standard describes an ECMAScript-compliant *script engine* so primary
targeted to UA producers and not to the end developers. This way
learning the JavaScript programming by ECMA-262 specs is like learning
to drive by car's technical specs: theoretically possible, practically
very difficult

>From the online resources linked at

<http://www.jibbering.com/faq/#FAQ3_2> I would suggest first MSDN
JScript 5.6 documentation as the most consistent (though still not
bug-free):
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/29f83a2c-48c5-49e2-9ae0-7371d2cda2ff.asp>

It is available for download (.chm help file, 2.8Mb) at
<http://www.microsoft.com/downloads/details.aspx?familyid=01592C48-207D-4BE1-8A76-1C4099D7BBB9>
A "Genuine Windows validation" is required, also chm help files need IE
installed to be viewed - so alas this download is useless for many
developers.

(JScript 5.6 is the one running on IE 6 and - with one minor
modification - on IE 7. It corresponds to Mozilla JavaScript 1.5 minus
all Mozilla/Microsoft extensions).


> However one thing I noticed in the ECMA standard that the String
> behaves differently when called as constructor and as function.


I'm not sure if I fully understand this statement. There can be string
primitive value and string object in JavaScript: the first one created
implicitly out of string literal, the second one created explicetly by
using String constructor: new String(value) The explicit String
constructor is very rarely used in JavaScript.

> I wanted to explore how to do that for my own functions that can be used
> as constructor or as conversion type. I tried to make sense of some
> earlier posts but could not do so.


In JavaScript the call context defines everything: so the very same
function can act as an object constructor or as a regular subroutine,
depending on how did you call it. Some formal description of your
actual task would be helpful.

 
Reply With Quote
 
 
 
 
satyajit
Guest
Posts: n/a
 
      12-05-2006
VK wrote:
<snip>
> In JavaScript the call context defines everything: so the very same
> function can act as an object constructor or as a regular subroutine,
> depending on how did you call it. Some formal description of your
> actual task would be helpful.


Thanks for the resources. To give an example (not a real example) of
what I was trying to find out is as follows:

function MimeType(string extension)
{
if ( /* its a constructor - this is what I want to know how to
check. */ )
{
// initialize MimeType. For jpeg, description is 'image/jpeg'
// Assume that getDescriptionFromExtension() returns a string
// with appropriate description
this.description = getDescriptionFromExtension(extension);
// other initializers..
}
else return getDescriptionFromExtension(extension);
}

I could use it like
var img1 = new MimeType(".jpeg");
img1.description would then be "image/jpeg"

another probable usage:

if ("image/jpeg" == MimeType(extension));

Any quick ideas?

Thanks.
-Satyajit

 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      12-05-2006
satyajit wrote:
> To give an example (not a real example) of
> what I was trying to find out is as follows:
>
> function MimeType(string extension)
> {
> if ( /* its a constructor - this is what I want to know how to
> check. */ )
> {
> // initialize MimeType. For jpeg, description is 'image/jpeg'
> // Assume that getDescriptionFromExtension() returns a string
> // with appropriate description
> this.description = getDescriptionFromExtension(extension);
> // other initializers..
> }
> else return getDescriptionFromExtension(extension);
> }
>
> I could use it like
> var img1 = new MimeType(".jpeg");
> img1.description would then be "image/jpeg"
>
> another probable usage:
>
> if ("image/jpeg" == MimeType(extension));
>
> Any quick ideas?


The first one is that I'm not excited by this approach IMO using
the same entity as object constructor and as subroutine depending on
circumstances is a bad practice: even if the used language technically
allows it. It obfuscates the source and it promises many problems with
inheritance (if anyone ever extends your library). You may flush my
opinion out though.

If function is called as constructor, [this] points to the newly
created object instance. (That's if no one is trying to cheat on your
program by substituting context: say by using MimeType.call(null,
args);
Otherwise [this] will point to the Global context which is equal to the
current window object in comparison operations (that's again for a
plain-vanilla situations without any special cases).
So if you insist on the current approach, you may check this == self :

function MimeType(ext) {
if (this == self) {
// presumably called as simple function
return 'foobar';
}
else {
// presumably called as constructor
this.description = 'foobar';
}
}

var foo = new MimeType();
var bar = MimeType();

alert(foo.description); // 'foobar'
alert(bar); // 'foobar'


P.S. I would still disassemble constructor from subroutine. Just try -
you may like it

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Need to pass a value from an IFrame to its parent to its parent PKalos@gmail.com Javascript 0 02-02-2006 10:02 AM
Derived class & objects to parent constructor Doug Java 7 05-13-2004 12:43 PM



Advertisments