Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Constructor object

Status
Not open for further replies.

UNIMENT

Programmer
Feb 15, 2002
302
0
0
US
[COLOR=000000]If you mess around with JavaScript and forms a lot, or even just a little, you might come across needing to use the [COLOR=aa0000]
Code:
new Option()
[/color] bit of code to create new options for a select box. I've noticed with this constructor a very strange thing: It's an object.

[COLOR=aa0000]
Code:
alert(typeof Option);
[/color]
The above code will tell you that it's an "object" and not a "function". The following code will give an error:
[COLOR=aa0000]
Code:
alert(typeof Option());
[/color]
That's because Option is not a function, and accessing it as if it were a function is not allowed in JavaScript. But then, the following code does not give an error:[COLOR=aa0000]
Code:
alert(typeof new Option());
[/color]
Strangely enough, [COLOR=aa0000]
Code:
Option
[/color] is a constructor that is not a function. It can be called correctly with the [COLOR=aa0000]
Code:
new
[/color] keyword but it cannot be called as a normal function. How did that happen, and how do I make my own like it?

Best regards[/color]
Code:
- UNIMENT
 
I think that there's no any contradiction.
This thing comes form the Option's definition:
"[tt]Object. A Select object option created using the Option() constructor.[/tt]"

Also,
"[tt]Each option created using the Option() constructor is an object and has the same properties as elements of the options array.[/tt]"
(from Netscape JS1.2 documentation)

good luck
 
The normal way to create a constructor:[COLOR=aa0000]
Code:
function myObject() {
 this.myProp="blah";
 this.myProp2=2;
}
[/color]

The normal way to create an instance:[COLOR=aa0000]
Code:
myInstance=new myObject();
[/color]
Now, with the above code, if you [COLOR=aa0000]
Code:
alert(typeof myObject)
[/color], an alert box will appear telling you that the [COLOR=aa0000]
Code:
myObject
[/color] constructor is a "function". If you [COLOR=aa0000]
Code:
alert()
[/color] the type of ANY constructor, it will tell you "function" -- that is, ANY constructor OTHER than [COLOR=aa0000]
Code:
Option
[/color]. Finding the type of [COLOR=aa0000]
Code:
Option
[/color] tells you that it is an "object".

Mess around with some code for me... You'll see what I mean.
Code:
- UNIMENT
 
well despite this being a bit old I'd like to add my two cents.

the new keyword is useless. You don't need it at all.

function car(color)
{
this.color = color
this.report = function()
{
alert(this.color)
}
}

var myRedCar = car("red")

myRedCar.report()

alert(typeof myRedCar)

Same with Option() or Object(). You don't need the new keyword. Gary Haran
 
In Javascript, functions (and strings, and integers, and floating point numbers, and everything else) are objects, too, and getting a typeof response that something is a function is just moving it away one level from the Object() object. User-defined objects/functions aren't as close to the core, so to speak, as intrinsic objects, and that's why you don't get the same response from Javascript with typeof.
 
trollacious, Yes I know all variables are supposedly objects. They all have [COLOR=aa0000]
Code:
toString()
[/color] and [COLOR=aa0000]
Code:
valueOf()
[/color] functions. In Netscape, you can [COLOR=aa0000]
Code:
watch()
[/color] and [COLOR=aa0000]
Code:
unwatch()
[/color] as well.

xutopia, have you ever tried calling the [COLOR=aa0000]
Code:
Option
[/color] constructor without using the [COLOR=aa0000]
Code:
new
[/color] keyword? You get an error! Try it!

The point I'm trying to make is, the [COLOR=aa0000]
Code:
Option
[/color] constructor is unusual. How the heck do I create a constructor that will not be a function? And, how to make a function at all where the constructor is not [COLOR=aa0000]
Code:
Function
[/color].
Code:
- UNIMENT
 
I believe all user-defined objects are constructed as function objects, so the only way to create an object is through a function.

The Math object doesn't seem to be a function, either, and you can't create any with new either. The other "core" built-in objects of Javascript (Array(), Boolean(), Date(), Number(), and a few others) are different from UDOs, too.
 
Based on your posts, I can safely conclude that ... You don't quite understand what I'm talking about and you aren't quite capable of answering my question.

The [COLOR=aa0000]
Code:
Math
[/color] object, if you haven't noticed, is not a constructor.

The [COLOR=aa0000]
Code:
Option
[/color] object, if you haven't noticed, is a constructor. Also, if you haven't noticed, it's not a [COLOR=aa0000]
Code:
Function
[/color].

Try this code for me, will you?
[COLOR=aa0000]
Code:
alert("Object's constructor:\n\n"+Object.constructor);
alert("Function's constructor:\n\n"+Function.constructor);
alert("Math's constructor:\n\n"+Math.constructor);
alert("Option's constructor:\n\n"+Option.constructor);
[/color]

Please be observant of the results.
Code:
- UNIMENT
 
In Netscape 4.7, all of them in this list say that they're Functions except the Math object. IE 5.01 says that Option.constructor is undefined, and Math is an Object, and the rest are Functions. So, it looks like it's up to the browser designers to decide what's what, and it's not a standard that you can count on for anything.

alert("Object's constructor:\n\n"+Object.constructor);
alert("Function's constructor:\n\n"+Function.constructor);
alert("Math's constructor:\n\n"+Math.constructor);
alert("Option's constructor:\n\n"+Option.constructor);
alert("Array's constructor:\n\n"+Array.constructor);
alert("Number's constructor:\n\n"+Number.constructor);
alert("Boolean's constructor:\n\n"+Boolean.constructor);
alert("String's constructor:\n\n"+String.constructor);
alert("Date's constructor:\n\n"+Date.constructor);
 
Thinking more about this, it appears that Netscape is consistent with how it implements Javascript objects, and IE 5.01 (and whatever browser Uniment is using) possibly has a bug in it in this area. Since Netscape has either followed or set the ECMA standard, and Microsoft has its own standards for JScript, this could be the root of the inconsistency/error.
 
Yeah... I tested in IE6 only.

However, the results of this test show that it may be possible in JavaScript or even JScript to create a constructor that is not created by the [COLOR=aa0000]
Code:
Function
[/color] function. For quite a while (must be months by now) I've made constructors, but only by creating functions. I'd like to know the alternate way(s).

Why don't you register your nickname in Tek-Tips? You sure do a lot of work for being only a visitor.
Code:
- UNIMENT
 
UMINENT:

I was interested in trying after you mentionned that I would get errors if calling Option() without the new keyword. So I did. I could be wrong after all. However :

var myOption = Option('test','test')
alert(myOption)

did not result in any errors in browsers I use.

IE 6 alerts : [object]
Mozilla 0.9.9 : [object HTMLOptionElement]
Netscape 6.2 : [object HTMLOptionElement]

There is one peculiarity though that if you call Option() without the two arguments it requires you will get an error.

alert(typeof Option('','')) // will alert : object
alert(typeof Option()) // will throw an error

That is perhaps what made you believe that you couldn't call Option() withouth the new keyword. Gary Haran
 
It appears you can do what Uniment is talking about IF you write your own scripting engine for Javascript. However, other than that, it appears that the core Option() object as implemented by Microsoft has a bug in it. I wouldn't build any features into code based on that. I've read about all the old DOS ASM programmers who wrote code based on undocumented features/bugs that were fixed later on, and their programs ceased to work.

As far as joining the group, I would if they gave frequent flyer miles, and I liked flying. :)#
 
Okay... Now that it has been determined that the [COLOR=aa0000]
Code:
Option
[/color] constructor can still be called as a function as long as parameters are passed to it, I'd like to know how you create functions that aren't constructed with [COLOR=aa0000]
Code:
Function
[/color] (and without using the [COLOR=aa0000]
Code:
function
[/color] keyword)?

thanks
Code:
- UNIMENT
 
Like I wrote, to do something like that, you need to write your own script engine like Microsoft did with that bug in it specifically.
 
Maybe it's not a bug, who knows?
Code:
- UNIMENT
 
Since it's not consistent across browsers, it's not something you can build anything solid on. If the browsers that more strictly follow ECMAScript standards do it one way, and IE is inconsistent with them (in whatever ways), it's either a bug of some kind or MS setting their own standards (or lack of them).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top