javascript - Why does Google's closure library not use real private members? - Stack Overflow

I've been a JavaScript developer for a while now, and I've always thought that the correct wa

I've been a JavaScript developer for a while now, and I've always thought that the correct way to implement private members in JavaScript is to use the technique outlined by Doug Crockford here: .html.

I didn't think this was a particularly controversial piece of JavaScript wisdom, until I started using the Google Closure library. Imagine my surprise... the library makes no effort to use Crockford-style information hiding. All they do is use a special naming convention and note "private" members in the documentation. I'm in the habit of assuming that the guys at Google are usually on the leading edge of software quality, so what gives? Is there some downside to following Mr. Crockford's advice that's not obvious?

I've been a JavaScript developer for a while now, and I've always thought that the correct way to implement private members in JavaScript is to use the technique outlined by Doug Crockford here: http://javascript.crockford./private.html.

I didn't think this was a particularly controversial piece of JavaScript wisdom, until I started using the Google Closure library. Imagine my surprise... the library makes no effort to use Crockford-style information hiding. All they do is use a special naming convention and note "private" members in the documentation. I'm in the habit of assuming that the guys at Google are usually on the leading edge of software quality, so what gives? Is there some downside to following Mr. Crockford's advice that's not obvious?

Share Improve this question asked Apr 3, 2010 at 18:40 Derek ThurnDerek Thurn 15.4k9 gold badges45 silver badges67 bronze badges 1
  • see the discussion in the ments to stackoverflow./questions/1437712/… for my opinion – Christoph Commented Apr 3, 2010 at 18:50
Add a ment  | 

3 Answers 3

Reset to default 8

There are a lot of examples of pseudo-privacy in main-stream JavaScript libraries. Facebook Connect's JavaScript library has the same structure.

The main reason developers go that route is for performance. Hiding things in closures can be slower and use more memory. Closure-hiding can also be less flexible, as true privacy can't be carried between files without some clever hacks. Closure-hiding is more conceptually pure, IMO, but when performance is a concern, using pseudo-privacy is the way to go.

The other reason is that a lot of Google programmers have backgrounds in Python, where there are no private anythings and the underscore prefix is the accepted munity standard.

Their inheritance model using goog.inherit() and goog.base() simply copy prototype members from the superclass to the subclass.

You can see that the sugar function from Doug Crockford does the same. I have personally faced lots of problems while inheriting a privileged member (this.property).

With both methods of inheritance, the private variables simply disappear as unlike C++ or Java where you have no access to the superclass' private members but they are still inherited. This has to be the major reason they prefer this approach.

There's also more to the JSDOC notation than meets the eye--when you use the google closure piler, those "@private" tags are parsed and enforced. If any external objects tries to access one of these variables, a pile error is generated. They do in fact have a philosophical objection to the general Crockford inheritance pattern: http://www.bolinfest./javascript/inheritance.php

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744718732a4589780.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信