0

In my application,I have to build a standalone lib for other people use,so I create new object like this:

function MyService(){ //xxxxxxx... } MyService.prototype.login=function(name,pass){ //here } MyService.prototype.LoadDataFromServer(){ //use the ajax get data from server,when get the data,I will eval them : var data=parseData(request.responseText); //now,the parseData is a private method which should not be exposed to the user,but it need the reference of the MyService object(this),so I have to use the following code: var this_ref=this; function parseData(res){ this_ref.xxxx=..... } } 

MyService.prototype.parseData=function(res){ this.xxxxx=.... } 

This will make the paresData function to the user.

Now,I wonder which is better?

3
  • Which approach is "better" is a question only you can answer, really. You're comparing two pieces of code that don't have the same effects, so it all depends on what you need in your application. Commented Jul 14, 2011 at 14:16
  • well,I do not want for the "better" way,I just want to know which is the common way? ok? Commented Jul 14, 2011 at 14:19
  • Well, again, you're asking for a comparison between two things that are not the same. The first is the common way to make private local functions, and the second is the common way to expose functions to instances of a class. Because they're different, they serve different purposes. Commented Jul 14, 2011 at 14:22

2 Answers 2

2

If you want actually private data/methods you should be using closures better.

var MyService = (function() { // define "private" methods var _login = function(name, pass) { ... }, _loadDataFromServer = function() { .... }, _parseData = function(res) { ... }; //return "public" methods return { login: _login, loadDataFromServer: _loadDataFromServer }; }()); // execute function immediately 

MyService now only has the two "public" functions, login and loadDataFromServer you can still access the "private" functions from the public functions but you cannot directly access any of the "private" methods MyService._login('test','pass'); will fail but MyService.login('test','pass'); will work. See this example http://jsfiddle.net/EXrDW/

Sign up to request clarification or add additional context in comments.

1 Comment

I just want to say that this approach albeit correct has some overhead performance-wise. Every instance of the object will create new functions in memory. Placing them on the .prototype only creates one for all instances. If you plan for your objects to get instanced frequently and or many times do not use approaches like this.
0

There is no "better" answer, only what you feel more comfortable with. What a lot of people seem to have adopted is the practice of putting underscores in front of methods that shouldn't be accessed by users.

1 Comment

which probably is the worst thing you can do to create a pseudo 'privacy'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.