马永占,myz,mayongzhan

相当稳定的进步,欢迎回贴交流

马永占ID:mayongzhan
61341次访问,排名1588(-1)好友1人,关注者12
php,apache,mysql,linux,html,css,javascript
mayongzhan的文章
原创 149 篇
翻译 0 篇
转载 0 篇
评论 86 篇
myz的公告

XML聚合
feedsky
最近评论
Henry_Ma:这个笔记不错,我也这样记一下。
ZXS:收藏!
rm:请问如何分析当前打开的URL
比如一个网页的URL是不固定的

需要在每一个url打开的当前页里分析这个URL 其中有个参数是需要调用的 应该如何做呢?
mayongzhan:很深刻
mayongzhan:为什么黑大能知道这些而我不知道.同样是翻资料...哎差距啊...
文章分类
收藏
    相册
    存档
    订阅我的博客
    XML聚合  FeedSky

    原创 在javascript中使用php风格的$globals收藏

    新一篇: PHP6的新特性:Unicode和TextIterator - 马永占 译 | 旧一篇: php5中Iterator与smarty整合 - 马永占 译

    版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/mayongzhan - 马永占,myz,mayongzhan

    在javascript中使用php风格的$globals

    JavaScript有隐含的全局变量。当你不使用var来定义a = 1,而直接定义a=1时,这个变量a将成为一个全局变量。有的认为这是一个错误,应该避免全局变量,因为它们往往会在意想不到的地方出错,尤其是在项目参与人员很多的情况下。

    在PHP当中,默认的是局部变量。如果你需要一个全局变量,那么你必须要将要声明为全局变量的变量放到$globals这个数组中。

    怎样在javascripts中使用$globals呢?通过为全局公约在你的javascripts呢?在脚本文件顶部声明:
    $GLOBALS = {};
    那么每一次当你需要一个全局变量,您可以这样做:

    $GLOBALS[ ' myglob ' ] = 1 ; / /非常像PHP的样式

    或者如果你喜欢,也可以这样:

    $globals.myglob = 1 ;
    这样做的优点:

         *全局变量容易识别(甚至从飞机都可以看见)
         *如果变量不定义成$GLOBAL,那么它就是局部变量。如果变量没有使用var,那么它将产生一个错误

    缺点:

         *这种使用方法,不是官方规定的,不强制使用,只是一项约定俗成的方法。

     

     

    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 should 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 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

     

    发表于 @ 2008年03月13日 13:00:00|评论(loading...)|收藏

    新一篇: PHP6的新特性:Unicode和TextIterator - 马永占 译 | 旧一篇: php5中Iterator与smarty整合 - 马永占 译

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © myz