/    Sign up×
Community /Pin to ProfileBookmark

constructor functions

[I]
Constructor functions typically do not have return values. They initialize the object passed as the value of this and return nothing. However, a constructor is allowed to return an object value, and, if it does so, that returned object becomes the value of the new expression. In this case, the object that was the value of this is simply discarded.[/I]

Can someone explain what that meant?, and if it is what i think it means, then why isnt it applying to this?(why does it not assign 10 to rect1 and why does it still have the width value) :

[code]
<html>
<body>
<script>
function rectangle(w,h)
{
this.width=w;
this.height=h;
return 10;
}

var rect1 = new rectangle(10,20);
alert(“rect:”+rect1+”width:”+rect1.width);
</script>
</body>
</html>
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jun 20.2008 — I thought that the return was ignored actually. Where did you get the definition from?
Copy linkTweet thisAlerts:
@Orc_ScorcherJun 20.2008 — Because 10 is a primitive value, not a object value. Try [b]return new Number(10)[/b] instead.
Copy linkTweet thisAlerts:
@forrestgumpauthorJun 20.2008 — I thought that the return was ignored actually. Where did you get the definition from?[/QUOTE]

OReilly.JavaScript.The.Definitive.Guide.5th.Edition.Aug.2006
Copy linkTweet thisAlerts:
@forrestgumpauthorJun 20.2008 — Because 10 is a primitive value, not a object value. Try [b]return new Number(10)[/b] instead.[/QUOTE]

? Thnx dude....
Copy linkTweet thisAlerts:
@KorJun 20.2008 — In fact the called [B]new[/B] token makes the difference between using a function as a constructor or not:
<i>
</i>function myFunction(){
return 10;
}
function myConstructor(){
return [COLOR="Blue"]new[/COLOR] Number(10);
}

var obj={}
obj.x= myFunction();
obj.y=[COLOR="Blue"]new[/COLOR] myConstructor();
alert(obj.x);
alert(obj.y);
Copy linkTweet thisAlerts:
@NandemJun 20.2008 — In fact the called [B]new[/B] token makes the difference between using a function as a constructor or not:
<i>
</i>function myFunction(){
return 10;
}
function myConstructor(){
return [COLOR="Blue"]new[/COLOR] Number(10);
}

var obj={}
obj.x= myFunction();
obj.y=[COLOR="Blue"]new[/COLOR] myConstructor();
alert(obj.x);
alert(obj.y);
[/QUOTE]



Exactly, the "new" keyword makes a different call to a function, but

remember, the "return" keyword used on a constructor, will ONLY be ignored when the return value is a "ValueType", if the return value is a "ReferenceType" will be returned by the constructor instead:


<i>
</i> [COLOR="Blue"]function[/COLOR] Constructor1()
{
this.a = 1;
this.b = 2;
return 10;
}

<i> </i>[COLOR="Blue"]function[/COLOR] Constructor2()
<i> </i>{
<i> </i> this.a = 1;
<i> </i> this.b = 2;
<i> </i> return [COLOR="Blue"]new[/COLOR] [COLOR="Blue"]Object[/COLOR]();
<i> </i>}
<i> </i>window.onload = [COLOR="Blue"]function[/COLOR]
<i> </i>{
<i> </i> [COLOR="Blue"]var[/COLOR] obj1 = [COLOR="Blue"]new[/COLOR] Constructor1(); [COLOR="SeaGreen"]//&lt;--WILL BE A OBJECT WITH a,b properties[/COLOR]
<i> </i> [COLOR="Blue"]var[/COLOR] obj2 = Constructor1(); [COLOR="SeaGreen"]// &lt;-- WILL BE A VALUETYPE[/COLOR]

<i> </i> [COLOR="Blue"]var[/COLOR] obj3 = [COLOR="Blue"]new[/COLOR] Constructor2(); [COLOR="SeaGreen"]// &lt;-- ALWAYS WILL RETURN A EMPTY OBJECT[/COLOR]
<i> </i> [COLOR="Blue"]var[/COLOR] obj4 = Constructor2(); [COLOR="SeaGreen"]// &lt;-- ALWAYS WILL RETURN A EMPTY OBJECT[/COLOR]
<i> </i>}



cya
×

Success!

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