为什么Google的封闭库不使用真正的私有成员? [英] Why does Google's closure library not use real private members?

查看:61
本文介绍了为什么Google的封闭库不使用真正的私有成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直是JavaScript开发人员,我一直认为在JavaScript中实现私有成员的正确方法是使用Doug Crockford概述的技术: http://javascript.crockford.com/private.html

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.com/private.html.

在我开始使用Google Closure库之前,我认为这不是一个特别有争议的JavaScript智慧。想象一下我的惊喜......图书馆不遗余力地使用Crockford风格的信息隐藏。他们所做的只是使用特殊的命名约定,并在文档中注明私人成员。我习惯于假设谷歌的人通常处于软件质量的前沿,那么是什么呢?遵循Crockford先生的建议是不是有一些不利之处?

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?

推荐答案

有很多伪隐私的例子主流JavaScript库。 Facebook Connect的JavaScript库具有相同的结构。

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

开发人员走这条路线的主要原因是性能。隐藏在闭包中的东西可能会更慢并且使用更多内存。关闭隐藏也可能不太灵活,因为没有一些聪明的黑客。关闭隐藏在概念上更纯粹,IMO,但是当性能受到关注时,使用伪隐私是可行的方法。

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.

另一个原因是很多Google程序员拥有Python的背景,其中没有私有的任何东西,下划线前缀是公认的社区标准。

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 community standard.

这篇关于为什么Google的封闭库不使用真正的私有成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆