On Mar 23, 2011, at 8:33 AM, Tony Wang wrote:

Hi Smith ,

for my opinion , in your case
var doSomething = app.doSomething;
doSomething(); // boom if the 'this' references are left in tact.


I think it's a fault for invoker, If I am developing with it ,
I will not forget to use call or apply to the app when you are plan to do something like that.

Like
var doSomething = app.doSomething;
doSomething.call(app);

I don't see a benefit to this. Storing a function reference that must be call()ed from the host context is wasteful. app.doSomething() is less code.


It's hard to design the function if you assume the invoker do KNOW NOTHING but they want to DO ANYTHING.

And it do totally has different meaning for me between app.doSomething() and doSomething().

My point is about 'this'. If there is one app object in the system, there's no benefit in using 'this'. 'this' should always be the same. So you can use 'this' for the sake of possibly creating another app object in the future (in which case, it should be a prototype), or you can use app and allow implementation code to use the methods via local vars.

For example:
if (MyApp.utils.isWhoozit( someObject )) { ... }

vs
var isWhoozit = MyApp.utils.isWhoozit;

if (isWhoozit( someObject )) {
}

readability is not lost, and the code is shorter. There are tradeoffs, sure. If readability is impacted, the namespaced function can be referenced as though it required 'this'. That said, it's been my experience that methods on static objects do not benefit from 'this'.

L

Best Regards,
Tony

2011/3/23 Luke Smith <lsm...@lucassmith.name>

On Mar 23, 2011, at 6:43 AM, Poetro wrote:

2011/3/23 pnbv <p.bacelar.vasconce...@gmail.com>:
Considering a single instance object:
app = {
  foo: 'afoo',
  bar: 'abar',

  callbackLib: {
      replyFoo: function () {
          console.log(this.foo);
      },
      replyBar: function () {
          console.log(app.bar);
      }
  },

  doSomeThing: function (){
      this.callbackLib.replyFoo.call(this);
      this.callbackLib.replyBar();
  }
}

app.doSomeThing();

What would be the preferred way of doing this (if there really is a
need for nested objects:)?
Thanks,
pedro
If you just want to get rid of the reference to this, you could do:

app = {
 foo: 'afoo',
 bar: 'abar',

 callbackLib: {
     replyFoo: function () {
         console.log(app.foo);
     },
     replyBar: function () {
         console.log(app.bar);
     }
 },

 doSomeThing: function (){
     app.callbackLib.replyFoo();
     app.callbackLib.replyBar();
 }
}
app.doSomeThing();

Although this would trigger errors in the following case:

bar = app;
app = {};
bar.doSomeThing();

As app is now no more.

There's a failure case either way.
var doSomething = app.doSomething;

doSomething(); // boom if the 'this' references are left in tact.

I find it preferable for non-instance collections of functions (i.e. object literals with methods as shown) to reference the object rather than 'this' as they are often utility functions or apply to the application as a whole, and are therefore more useful as function refs that can be passed around or assigned to shortcut vars. Shortcut vars can be compressed better as well.

Short form: use 'this' in prototype methods, the containing var name in "static" methods. As with most things, this is a guide, not a rule.


L

--
Poetro

--
To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

--
To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com


--
To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

--
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to