> Next, in evaluation of statements, the IfStatement is evaluated. So
> what is `"a" in window"`? result? In a web browser, the global object
> its he global variable object is the window. Thus `"a" in window` is
> true.
>
> So `"a" in window` is true, and the Logical NOT operator (!) converts
> that to false. And:
> | if( !("a" in window) )
>
> Must be false!
I just tested this out in firebug's console and I'm a little bit confused.
`"a" in window` returned false, and `!("a" in window)` returned true.
Can you give a bit of clarification?
Sorry for asking this, but its really confusing. Thanks.
On Fri, Dec 17, 2010 at 2:44 PM, Garrett Smith <[email protected]>wrote:
> On 12/16/10, Balázs Galambosi <[email protected]> wrote:
>
> Learn interleaved reply; do not top post.
>
>
> [...]
>
>
> >
> > I think Google doesn't use closures for private variables because of
> > the small performance penality it has (variables are searched up the
> > scope-chain). As far as I know they're utilizing Closure Compiler's
> > ability to guard private members (using @private):
> >
> <touchy>
> A more likely explanation is that the authors of Closure COmpiler do
> not understand [[Scope]].
> </touchy>
>
> > /**
> > * A simple private memeber of goog.
> > * @private
> > * @type {String}
> > */
> > goog._secret = "cannot be accessed directly from outside";
> >
> Running that example, comments and all, through CC at:
> http://closure-compiler.appspot.com/home
>
> The result is:
> | goog._secret="cannot be accessed directly from outside";
>
> Identifier `_secret` can be accessed directly from outside.
>
> Similarly for the example in the article you've linked, which states
> about an example:
> "The compiler will ensure that the private member _birthDay isn’t read
> or write through out the whole application."
>
> When I run that example through CC, I get the output:
>
> function User(){this._birthDay=new
> Date}User.prototype.getBirthYear=function(){return
> this._birthDay.getYear()};var me=new
> User;document.write(me.getBirthYear().toString());
>
> The _birthDay property is not private:
> | var u = new User;
> | alert(user._birthDay);
>
> The `_birthDay` property is not private at all.
>
> > You can read more about Google CC OOP features at:
> >
> http://calendar.perfplanet.com/2010/coding-better-object-oriented-javascript-with-closure-compiler/
> >
>
> Oh boy. HEre we go.
>
> | Only JavaScript Ninjas survive?
> |
> | So this is why everyone is trying so hard to hire a JavaScript ninja
> or become one
> |
> | However, is it really necessary to write JavaScript like this?
> |
> | if (!("a" in window)) {
> | var a = 1;
> | }
> | alert(a);
>
> What does he think that does? Realize that if `window` is the global
> object (and it is in web browsers), then `a = 1` can never be
> evaluated.
>
> Does he explain what that code does? No! He just says its all to
> confusing and instead:
> "Let Closure Compiler make you a JavaScript Samurai!"
>
> I mentioned javascript library blog FUD yesterday and this is a fine
> example. They want to make cross browser scripting seem so scary and
> complicated. This stuff is not that hard. This is a tricky example,
> here but let me break that down and you may likely have one up on the
> author of this article.
>
> Example:
> | if (!("a" in window)) {
> | var a = 1;
> | }
> | alert(a);
>
> There are two passes of the code here. First, there is Variable
> Instantiation. This is where identifiers with variable statement,
> FunctionDeclaration, and parameters (for function code) are added to
> the variable object (VO), and given attribute {DontDelete}. The
> {DontDelete} attribute means that if you try to delete a variable, it
> will fail, e.g.
>
> var a;
> var deleted = delete a; // false (except in eval code)
>
> The second pass is evaluation of Statements, such as the IfStatement
> or the assigmentExpression `a = 1`.
>
> In Variable Instantiation, `a` is added to the VO, if it does not
> already exist, and given value `undefined`. If the VO already has an
> `a` property, its value is unchanged.
>
> Next, in evaluation of statements, the IfStatement is evaluated. So
> what is `"a" in window"`? result? In a web browser, the global object
> its he global variable object is the window. Thus `"a" in window` is
> true.
>
> So `"a" in window` is true, and the Logical NOT operator (!) converts
> that to false. And:
> | if( !("a" in window) )
>
> Must be false!
>
> The result of the program is the creation of a global identifier `a`
> with value `undefined` and attributes {DontDelete}. JScript 5.8 and
> below, IIRC, add also {DontEnum} (you are not expected to know that
> bug).
>
> Instead of explaining what that toy example does, the author goes on
> to tell you you need Closure Compiler to be a "Samurai".
>
> Garrett
> > - Balázs
> >
> > 2010/12/16 bawigga <[email protected]>:
> >> What is the correct nomenclature for the following ways of writing JS. I
> >> understand how each example works, but I don't know how to put the
> >> following
> >> patterns into words. I also don't know when it's best to use one pattern
> >> over the other (perhaps just preference)?
> >> I personally prefer ex 2/3 as I feel as though it keeps my code a lot
> >> cleaner, and easier to maintain. It would seem ex1 doesn't really help
> in
> >> code maintainability as you still have a bunch of seemingly random
> >> unrelated
> >> functions within a single namespace.
> >> I also found that the google closure lib uses ex 1, and jquery seems to
> >> use
> >> ex 2/3. Why would they choose one over the other?
> >>
> >> Google Closure -
> >>
> http://code.google.com/p/closure-library/source/browse/trunk/closure/goog/base.js?r=544
> >> - namespace pattern?
> >> jQuery - http://code.jquery.com/jquery-1.4.4.js - using object literal?
> >>
> >> // ex 1 (namespace pattern?)
> >> goog.a = function() {
> >> //do something
> >> };
> >> goog.b = function(){
> >> //do something
> >> };
> >> // ex2 (module pattern?)
> >> goog = (function(){
> >> return {
> >> a: function(){
> >> // do something
> >> },
> >> b: function() {
> >> // do something
> >> }
> >> };
> >> })();
> >> // ex 3 (literal pattern?)
> >> goog = {
> >> a: function(){
> >> // do something
> >> },
> >> b: function() {
> >> // do something
> >> }
> >> }
> >>
> >> --
> >> To view archived discussions from the original JSMentors Mailman list:
> >> http://www.mail-archive.com/[email protected]/
> >>
> >> To search via a non-Google archive, visit here:
> >> http://www.mail-archive.com/[email protected]/
> >>
> >> To unsubscribe from this group, send email to
> >> [email protected]<jsmentors%[email protected]>
> >>
> >
> > --
> > To view archived discussions from the original JSMentors Mailman list:
> > http://www.mail-archive.com/[email protected]/
> >
> > To search via a non-Google archive, visit here:
> > http://www.mail-archive.com/[email protected]/
> >
> > To unsubscribe from this group, send email to
> > [email protected]<jsmentors%[email protected]>
> >
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]<jsmentors%[email protected]>
>
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]