/    Sign up×
Community /Pin to ProfileBookmark

Doubt about prototype and constructor properties

Hi,

Recently I started working with the prototype property of functions, and running the following code I found a problem

[CODE]function p(param) {
this.y = param;
}

function f(param) {
this.x = param;
}

f.prototype = new p(2);
var obj = new f(1);

alert(f.prototype.constructor);
alert(obj.constructor);[/CODE]

I don´t know why a get the same result in both alerts. I mean, the second one tells me the obj constructor is p, instead of f, as I suppose it should be. Does anyone know why?

Thanks in advance.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@toicontienJan 29.2009 — On this line:
[CODE]f.prototype = new p(2);[/CODE]
The browser sets the f.prototype.constructor property to p.

On this line:
[CODE]var obj = new f(1);[/CODE]
That prototype.constructor property is copied over to the obj variable directly, thus f.prototype.constructor and obj.constructor are the same function. What did you expect them to be?
×

Success!

Help @manuel45 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.2,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...