/    Sign up×
Community /Pin to ProfileBookmark

function test(){function test(){}}

[CODE]version1
function test(){function test(){…}}
test=new test()[/CODE]

[CODE]version2
function test(){}
test.prototype.test=function(){…}
test=new test()[/CODE]

are both versions equal ?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@QuentinMar 03.2006 — You can't have a function declared inside of a function this way.

[CODE]function test() {
function test() {}
}[/CODE]


You can however use

[CODE]function test() {}
test.prototype.test = function() {}[/CODE]


or

[CODE]function test() {
this.test = function() {}
}[/CODE]


and get the same result.
Copy linkTweet thisAlerts:
@gert_cuykensauthorMar 03.2006 — hmm i think i will just stick with the prototype version ?
Copy linkTweet thisAlerts:
@BrainEaterMar 03.2006 — function CustomObject(){
var privateVar='private';
var self=this;
this.publicVar='public';
function privateFunc(){alert('private functions can access variable and functions,n both '+privateVar+' and '+self.publicVar+' (though theynneed the "self" trick to get the public/priviledge ones)')}
this.priviledgedFunc=function(){
alert('priviledged functions can access variable and functions,n both '+privateVar+' and '+this.publicVar)
privateFunc()
}
}
CustomObject.prototype.publicFunc=function(){
try{
alert('public functions can access variable and functions,n both '+privateVar+' and '+this.publicVar)
}
catch(error){
alert('public functions can only access variablesnand functions that are '+this.publicVar+'. Trying tonget private variables gives the error:n"'+error.toString()+'"')
}
}

ob=new CustomObject();
ob.priviledgedFunc();
ob.publicFunc();
Copy linkTweet thisAlerts:
@gert_cuykensauthorMar 03.2006 — sometimes i hate javascript you know ?
Copy linkTweet thisAlerts:
@QuentinMar 03.2006 — Really the main difference between the two are this...

Lets say you create a class:

[CODE]function test() {}[/CODE]

and within this class you want some certain functions and variables. Then you want to inherit this class

[CODE]var mytest = new test();[/CODE]

but then you want to add a function to mytest without having to add this function to the entire test class. So thats where prototype comes into play.

[CODE]var mytest = new test();
mytest.prototype.test = function() {

}[/CODE]
Copy linkTweet thisAlerts:
@gert_cuykensauthorMar 03.2006 — i understand but it is so different from php and when using php to generate javascript its like driving a car from europe to england where europe is php and england js ?
Copy linkTweet thisAlerts:
@gert_cuykensauthorMar 05.2006 — you know after reading this treath 236 times it makes alot of sence actualy

prototypes are like the extentions of firefox you can download and install ?
×

Success!

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