Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-19-2009, 09:41 AM   PM User | #1
lee_wen
New to the CF scene

 
Join Date: Nov 2009
Location: BeiJing
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
lee_wen is an unknown quantity at this point
Angry Help on JavaScript constructor property

I had read from books that the constructor property of object is inherited from its prototype. And the prototype can be changed dynamically. New property can be added to or deleted from object even it was created before the prototype change. But I got confused on below codes.
Code:
function A()
{
	this.title = "A";	
}

function B()
{
	this.title = "B";	
}

document.write("<br>");
var b = new B();

document.write(b.constructor);
document.write("<br>");
document.write(B.prototype.constructor);
document.write("<br>");

B.prototype = new A();
document.write(b.constructor); // Suppose to output "function A() ..."
document.write("<br>");
document.write(B.prototype.constructor);
document.write("<br>");

B.prototype.constructor = B;
document.write(b.constructor);
document.write("<br>");
document.write(B.prototype.constructor);
document.write("<br>");
But the actual result (both IE and firefox) is
Code:
function B() { this.title = "B"; }
function B() { this.title = "B"; }
function B() { this.title = "B"; }
function A() { this.title = "A"; }
function B() { this.title = "B"; }
function B() { this.title = "B"; }
Please help me. thanks.

Last edited by Kor; 11-19-2009 at 09:52 AM.. Reason: wrap the code [code][/code]
lee_wen is offline   Reply With Quote
Old 11-19-2009, 02:34 PM   PM User | #2
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
The change is read in the instances and in the prototypes,
not in the functions.

function A(){this.title='A'};
function B(){this.title='B'};
B.prototype=new A;

var b=new B();

alert(b.constructor); // returns function A
alert(B.prototype.constructor); // // returns function A

But since title property is in the constructor function, and not the prototype, b.title is 'B'.

If you define a property or method in the prototype it will be inherited
A.prototype.type='A';
alert(b.type);

Last edited by mrhoo; 11-19-2009 at 02:48 PM..
mrhoo is offline   Reply With Quote
Old 11-19-2009, 03:58 PM   PM User | #3
lee_wen
New to the CF scene

 
Join Date: Nov 2009
Location: BeiJing
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
lee_wen is an unknown quantity at this point
Smile

Quote:
Originally Posted by mrhoo View Post
The change is read in the instances and in the prototypes,
not in the functions.

function A(){this.title='A'};
function B(){this.title='B'};
B.prototype=new A;

var b=new B();

alert(b.constructor); // returns function A
alert(B.prototype.constructor); // // returns function A

But since title property is in the constructor function, and not the prototype, b.title is 'B'.

If you define a property or method in the prototype it will be inherited
A.prototype.type='A';
alert(b.type);
Maybe I didn't clearly describe my issue.
I create instance b first by b = new B();
then I change B's prototype B.prototype = new A();
As b itself doesn't have constructor property and it inherits the property from its prototype, so b.constructor should be identical with B.prototype.constructor, i.e. both of them should be "function A() ...". But the test result is b.constructor is still "function B() ...".

I also tested creating the object after setting the prototype, both of them are "function A() ...".

I am wondering what happened behind the invocation order change.

Last edited by lee_wen; 11-19-2009 at 04:02 PM..
lee_wen is offline   Reply With Quote
Old 11-19-2009, 09:32 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by lee_wen View Post
As b itself doesn't have constructor property and it inherits the property from its prototype, so b.constructor should be identical with B.prototype.constructor, i.e. both of them should be "function A() ..."
no, b does have a constructor: B.
in the code you posted, A does comes up for both, as you expected.

Code:
function A(){this.title='A'};
function B(){this.title='B'};
B.prototype=new A;

var b=new B();

alert(b.constructor); // returns function A
alert(B.prototype.constructor); // // returns function A
is there anything you're still unclear on?
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Old 11-20-2009, 05:33 AM   PM User | #5
lee_wen
New to the CF scene

 
Join Date: Nov 2009
Location: BeiJing
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
lee_wen is an unknown quantity at this point
Smile

Quote:
Originally Posted by rnd me View Post
no, b does have a constructor: B.
in the code you posted, A does comes up for both, as you expected.

Code:
function A(){this.title='A'};
function B(){this.title='B'};
B.prototype=new A;

var b=new B();

alert(b.constructor); // returns function A
alert(B.prototype.constructor); // // returns function A
is there anything you're still unclear on?
I know it works well in your case.
I am talking about the case that b is created before reassign the prototype.
lee_wen is offline   Reply With Quote
Old 11-20-2009, 09:43 AM   PM User | #6
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
in this case:
Code:
function B(){this.title='B'};
var b=new B();

alert(b.constructor); // returns function B
alert(B.prototype.constructor); // // returns function B

the first alert is obvoius.
the second one results from the default prototype object used for constructors;
an empty object of the type as the constructor for objects, the function itself for functions, undefined for primitives, etc...
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Tags
constructor, inherit, prototype

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 01:38 PM.


Advertisement
Log in to turn off these ads.