Use $ globals-ma yongzhan in Javascript

Source: Internet
Author: User

Copyright statement: original works can be reproduced. During reprinting, you must mark the original publication, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://blog.csdn.net/mayongzhan-ma yongzhan, myz, mayongzhan

Use PHP-style $ globals in Javascript

Javascript has implicit global variables. If you do not use VaR to define a = 1, but directly define a = 1, this variable A will become a global variable. Some think this is an error and global variables should be avoided, because they often encounter errors in unexpected places, especially when there are many project participants.

In PHP, local variables are used by default. If you need a global variable, you must put the variable to be declared as a global variable in the $ globals array.

How to use $ globals in javascripts? What about using global conventions in your javascripts? Declare at the top of the script file:
$ Globals = {};
When you need a global variable every time, you can do this:

$ Globals ['myglob'] = 1; // very similar to the PHP Style

Or, if you like:

$ Globals. myglob = 1;
Advantages:

* Global variables are easy to recognize (even visible from airplanes)
* If the variable is not necessarily defined as $ global, it is a local variable. If the variable does not use VaR, it will generate an error.

Disadvantages:

* This method is not officially specified and is not mandatory. It is just a conventional method.

 

 

Stoyan Stefanov's blog: PHP-style $ globals in JavaScript?

Javascript has implied globals. when you skip the VaR in Var A = 1; and go a = 1;, then a becomes a global variable. some consider this an error in the language. global variables shoshould be avoided because they tend to overwrite each other in unexpected places, especially if the project grows in loc and number of developers.
In PHP on the other hand, variables are local. If you need a global variable, then you have to be explicit about it using the $ globals superglobal array.
So how about this: Adopt the $ globals convention in your javascripts? At the top of the script you go:
$ Globals = {};
Then every time you need a global variable, you do:
$ Globals ['myglob'] = 1; // very PHP-like
Or if you prefer:
$ Globals. myglob = 1;
Benefits of the approach:
• Global variables easy to spot (even from an aeroplane)
• If it's not $ global, it's meant to be local. If it's missing the VaR, it's an error
Drawback:
• It's a convention, so it can only help, but not enforce any coding practices

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.