/    Sign up×
Community /Pin to ProfileBookmark

Constructor value when inheriting

So I’m doing some inheritance:

[CODE]First = function()
{
this.type = “First”;
}

Second = function()
{
this.type = “Second”;
alert(“Second: this.constructor: n”+this.constructor);
}
Second.prototype = new First();

onload = function()
{
var test = new Second();
}[/CODE]

Now… can someone explain to me why the above displays:

[QUOTE]

Second: this.constructor:
function()
{
this.type = “First”;
}

[/QUOTE]

Seems a little odd to me!

Oh, and if I move the alert to the onload function instead and alert(test.constructor) I still get the same thing. It still thinks the constructor is First instead of Second.

Thanks!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoOct 19.2005 — First off, this is a JavaScript forum, not a Java forum. Secondly, where as Java uses classes, constructors, and methods, Javascript only uses functions. You can use constructors, but they don't always perform as needed.

take a look at the link below:

[URL=http://www.crockford.com/javascript/inheritance.html]Javascript and Inheritance[/URL]
Copy linkTweet thisAlerts:
@tabzterOct 19.2005 — konithomimo you should really find out what your talking about before accusing jshellman. Even in the example link you have given it shows that javascript can have inheritance.

Javascript is loosely based on java - having the ability to make objects and apply inhreitance to these objects in a object hierarchy like java.
Copy linkTweet thisAlerts:
@konithomimoOct 20.2005 — konithomimo you should really find out what your talking about before accusing jshellman. Even in the example link you have given it shows that javascript can have inheritance.

Javascript is loosely based on java - having the ability to make objects and apply inhreitance to these objects in a object hierarchy like java.[/QUOTE]


Or maybe you should read what I wrote. i never said that JS didnt support inheritance . . . otherwise I wouldnt have titled my link "Javascript and Inheritance". What I said is that JS doesnt have all of the features of Java in that JS uses functions. One can mimick java classes, methods, and constructors, but those are not predesigned JS features.

Think, and read, then think and read again before commenting please.

Also, I wasnt accusing anyone of anything. I was telling him how JS is.
Copy linkTweet thisAlerts:
@jshellmanauthorOct 26.2005 — Did you look at the results, though? Doesn't that seem very odd that I should create:

var obj = new MySomething()

And obj.constructor is NOT MySomething? Shouldn't it be? And if not... is there some way I can get MySomething from obj?
Copy linkTweet thisAlerts:
@Orc_ScorcherOct 26.2005 — No, that's what should happen if you modify the prototype chain. constructor is writeable, so if you want to, you can set |this.constructor = Second| yourself.
×

Success!

Help @jshellman 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.18,
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,
)...