/    Sign up×
Community /Pin to ProfileBookmark

Is there any simple way of getting a objects type.
using the sample below I would like to determine its a “Person’ object.
Is this possible without parsing through the .constructor property?

[code=php]
//Obj definition
function Person()
{
this.name;
this.age;
}

//create new
var p = new Person();
p.name = “Drew”;
p.age = 25;

//get Obj type??
alert(p.constructor);

[/code]

Thanks,
Drew

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@mrhooFeb 07.2008 — Somebody has to parse the string to get the name out of the constructor function-

but you can let it be a method, and save some typing:

[CODE]Function.prototype.namer= function(){
var tem= this.toString().match(/functions+(w+)(/);
return (tem)? tem[1]: '';
}[/CODE]


alert(p.constructor? p.constructor.namer(): typeof p)
Copy linkTweet thisAlerts:
@KorFeb 07.2008 — I don't get it... What do you mean by object's [B]type[/B]?
Copy linkTweet thisAlerts:
@cgishackauthorFeb 07.2008 — Kor,

I would like to determine if its a ''Person' object or something different. (Using the sample above)
Copy linkTweet thisAlerts:
@KorFeb 07.2008 — Objects have no type. Variables have type (they might be objects, strings, attributes, numbers, undefined, Boolean and so on, according to the value assigned...), and the method is [B]typeof[/B], if this is what you are looking for... I am still a litle bit confused about what you aim could be... ?
Copy linkTweet thisAlerts:
@cgishackauthorFeb 07.2008 — One aim is to have an overloaded function where I could pass in many different objects.

I am trying to format XML based on what object is passed to the function.

The XML changes depending on the object.

Drew
Copy linkTweet thisAlerts:
@Jeff_MottFeb 07.2008 — Kor, I'm sure that by "type," cgishack means the function that created and initialized the object.

In which case, cgishack, the property "constructor" is a reference to the function itself, which means you can do:if (p.constructor == Person)
alert("I'm a person!");
Copy linkTweet thisAlerts:
@cgishackauthorFeb 07.2008 — Jeff,

Thank you!

I never thought of doing it that way.
Copy linkTweet thisAlerts:
@KorFeb 07.2008 — Kor, I'm sure that by "type," cgishack means the function that created and initialized the object.

In which case, cgishack, the property "constructor" is a reference to the function itself, which means you can do:if (p.constructor == Person)
alert("I'm a person!");
[/QUOTE]

Oh, I see now... Probably the more intuitive could be (untested):
<i>
</i>if (p.constructor == window['Person']())

As functions can be treated as the window object's properties...
Copy linkTweet thisAlerts:
@Jeff_MottFeb 07.2008 — Assuming you leave off the parentheses (i.e., window['Person'][color=red]()[/color]), then it will work just as well.

Though intuitiveness, of course, is subjective. I prefer leaving off the reference to the window object. I don't think it hurts clarity, and I think the code even looks cleaner and simpler without it.
Copy linkTweet thisAlerts:
@KorFeb 07.2008 — Yes, you might be right about the parenthesis, as [B]constructor[/B] returns the name of the function as a variable. I was thinking about the real call of a function, not about it's name.

Sure, there is no reason for using window object if there is only one function (as window is the ultimate root parent anyway). I was only to explain [B]cgishack[/B] the principle of handling dynamically the functions/global variables as properties of the window object... ?
Copy linkTweet thisAlerts:
@cgishackauthorFeb 07.2008 — Thanks Guys,

You have helped a lot!

Drew
Copy linkTweet thisAlerts:
@Jeff_MottFeb 07.2008 — ... as [b]constructor[/b] returns the name of the function as a variable.[/quote]Well... no. The constructor property holds a [i]reference[/i] to the function that created it. From cgishack's code, you can try these test cases.alert( typeof p.constructor ); // alerts "function"
alert( p.constructor.toString() );
/* alerts...
* function Person() {
* this.name;
* this.age;
* }
*/



I was only to explain cgishack the principle of handling dynamically the functions/global variables as properties of the window object...[/quote]The principle was fine. It was only the intuitiveness part that I commented on.
Copy linkTweet thisAlerts:
@KorFeb 07.2008 — Well... no. The constructor property holds a [i]reference[/i] [/QUOTE]
a variable [I]is[/I] a reference...? Or I am wrong? Well, let it be, as it could become an academical talk... ?
Copy linkTweet thisAlerts:
@Jeff_MottFeb 07.2008 — Erm... no, it isn't. (Unless you're thinking of a different definition for the word. I'm thinking of it in the way it's typically used with programming languages: a safer kind of pointer.) Any kind of object will of course be a reference, but variables hold more than just objects; JavaScript does have primitive types as well.

And in any case, even if we ultimately agree that the constructor property references the function, that reference doesn't directly get you the name of the function, as you said. As mrhoo showed earlier, you'd need to do some custom parsing to get the function's name.

(Sorry. I started replying before I saw your edit.)
Copy linkTweet thisAlerts:
@mrhooFeb 07.2008 — The constructor property can get murky-


If you construct an object and make its prototype some other object,

as when you create a class of [B]Collection[/B] objects that inherits methods from a more general class named [B]Group[/B], the constructor property will return the Group constructor for a Collection object.

After you set the new Collection prototype to inherit from the 'parent' object, you can set the constructor for new Collections specifically:

[B]Collection.prototype= new Group;

Collection.prototype.constructor= Collection;[/B]


Or you can go around the constructor issue and use instanceof-

if c is a collection object, each of these will return true:

[B]c instanceof Object

c instanceof Group

c instanceof Collection.[/B]


(excuse the word 'class' in the above comments- I know it isn't accurate, but a 'set of objects instantiated by the same constructor' seems a little verbose.)
×

Success!

Help @cgishack spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...