/    Sign up×
Community /Pin to ProfileBookmark

what’s the diff between these 2 methods setup approach?

Hi, what’s the diff between the follow way of setting up a method for an object?

[code]
function Class1 (){
this.method = class1Method;
}

function class1Method(param1, param2){
//code do something…
}
[/code]

second approach….

[code]
function Class2(){
}//end class2

function class2Method(param1, param2){
//code do something …
}

Class2.prototype.method = class2Method;
[/code]

thank you ?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorAug 03.2006 — I dont'd really sense which is your object...

What about JSON?
<i>
</i>&lt;script type="text/javascript"&gt;
var myobj={
dothis:alert('I did!')
}
onload=function(){
myobj.dothis
}

-------

or maybe:
<i>
</i>&lt;script type="text/javascript"&gt;
var myobj={
dothis:goanddo('I did!')
}
onload=function(){
myobj.dothis
}
function goanddo(p){
alert(p)
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@sirpelidorauthorAug 04.2006 — I dont'd really sense which is your object...
[/QUOTE]


sorry for not being clear, was trying save space by being lazy heh.

Here's the long lengthy detail version, let me try again:

I'm going to make a class call "Circle", its' constructor take 1 paramater "radius" and it has a method "Area()" which will return the area of the circle...

approach 1:
<i>
</i>function Circle(radius){
this.r = radius;
this.PI = 3.1416;
}

function Circle_Area(){
return this.PI * this.r * this.r;
}

Circle.prototype.Area = Circle_Area; //connect Circle method to a function



and we have approach 2:
<i>
</i>function Circle(radius){
this.r = radius;
this.PI = 3.1416;
this.Area = Circle_Area;
}

function Circle_Area(){
return this.PI * this.r * this.r;
}


and now, I'm going to use the Circle class i just created...

var c = new Circle(5); //create an instance of Circle class

var area = c.Area();

alert(area);

when run this code, doesn't matter which approach, it'll still alert: 78.54, which is the circle Area I'm expecting.

which brings me back to my question, what's the diff between approach 1, and approach 2... when one use is better then the others?

Sorry for the long lengthy post ?
Copy linkTweet thisAlerts:
@sirpelidorauthorSep 01.2006 — I finally figure out the diff between those 2 ways of setting up a method for a Class and I'd like to share to anyone who may be interested:


the second approach uses a technique call "function literals", that means if we have 1000 circle objects, that Circle_Area() function will have to involve 1000 times (This can be very expensive on memory).


the first approach uses a technique call "prototype". Prototype means that if a class is inherited from another class, then it can borrow all of its methods, so we don't need to redefine them. Any class that we create has a prototype. This prototype is available to all of the objects of the class. Which means if we have 1000 circle objects, that function will only have to involve once (cheaper on memory).
×

Success!

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