`

Do write portable code

阅读更多

最近遇到两个 bug ,都是因为代码写得不具有可移植性而造成的。Javascript 本身就由于浏览器的差异有许多需要注意的地方,写代码的时候一定要万分小心,多多测试。

数组常量值末尾多余的逗号

Javascript 里面用类似 [1,2,3] 的方法来初始化数组是众所周知的。但是有一个地方却比较容易犯错误。特别是对于熟悉 C 语言的人来说,C 语言里面明确说明,以下这种情况里面,多余的逗号是可以接受的:

int array[] = {1,2,};

然而作为一个弱类型的语言,语法相对会比较随意的 javascript ,在这里却有一个陷井。事实上,标准可以允许中间或着头部的位置出现多余的逗号,这样会把多余的 undefined 的对象加入到数组中。不过末尾的逗号却会因为浏览器的不同而产生不同的结果。例如

var arr = [1,2,3,];
alert(arr.length);

在 IE6 下会得到 4 ,而在 Firefox 2 下得到的是 3 。且不论谁对谁错,但这已经是不得不去面对的事实了。这次我就遇到了这个问题,事实上,我是在 rails 的 ERB 模板里面生成一个 javascript 的数组常量。大概像这个样子:

var data = [
    <% for entry in @entries -%>
        <%= format_entry entry %>,
    <% end -%>
    ];

这样产生了末尾多余的逗号,导致了在 Firefox 和 IE 下产生了不同的结果。最后对最后一个元素特殊对待才解决了问题:

var data = [
    <% for entry in @entries[0..-2] -%>
        <%= format_entry entry %>,
    <% end -%>
    <%= format_entry @entries[-1] %>
    ];

时间的格式

我要在 javascript 里面通过字符串解析出时间来。在 Ruby 里面的 Time 类有一个方便的 to_s 方法可以直接把时间转化为字符串,它得到的格式是这样的 Tue Sep 04 13:48:25 +0800 2007 。我便直接使用了这个格式,一直都是好好的,后来拿到 IE 下面去跑的时候,根本跑不出结果来。调试了很久才发现 IE 下面没有能把那个时间解析出来,IE 不认那个 “+0800″ 。最后我用 strftime 格式化成了一种通用的格式把这个问题解决了。

分享到:
评论

相关推荐

    10 Things You Can Do To Write Better Code 写好代码的十个秘诀

    7. **可移植性(Portable Code)**:尽量使代码平台无关,遵循标准和最佳实践,以便在不同的操作系统或硬件环境中运行。 8. **可读性(Readability)**:使用有意义的变量名,添加必要的注释,保持代码的整洁和一致...

    ExceptionalCpp_47EngineeringPuzzlesProgrammingProblemsSol.chm

    Exceptional C++: 47 Engineering Puzzles, ...Try your skills against the C++ masters and come away with the insight and experience to create more efficient, effective, robust, and portable C++ code.

    Dimeric.Virtual.Database.

    This makes code written to VDB more portable across SQL dialects. (See Vendor Neutral SQL) - Connection Pooling. If you set a few properties in a configuration string, VDB will pool database ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    However, implementation code properly belongs in .cc files, and we do not like to have much actual code in .h files unless there is a readability or performance advantage. If an inline function ...

    cocos2d-x c++的iconv.rar

    But gcc's #include_next is not portable. Thus, once libiconv's iconv.h has been installed in /usr/local/include, there is no way any more to include the original /usr/include/iconv.h. We simply ...

    Intermediate Perl.pdf

    Test your code to ensure it does what you want it to do. Chapter 18, Advanced Testing Test complex aspects of code and meta-code things such as documentation and test coverage. Chapter 19, ...

    Loving Lisp

    This syntax makes it easy to write and manipulate code programmatically. - **Dynamic Typing**: Unlike statically typed languages like Java and C++, Lisp supports dynamic typing, allowing for more ...

    CanFestival3. Version 3.0 The CANOpen stack manual

    2. **Implement the Node**: Write the node application code, utilizing the CanFestival library to handle the CANopen protocol. 3. **Testing and Validation**: Test the node in a controlled environment ...

    Hello.Android.4th.Edition

    compiler or Dalvik virtual machine, so your code will be portable across ARM, x86, and other architectures. Support for a variety of input methods is included, such as keyboards, game pads, touch, TV ...

    Python4DelphiPart-1-7139726.pdf

    - **Perceived Productivity and Ease of Learning**: Python’s simple syntax and readability make it easy to learn and write code quickly. - **Complementary Strengths with Delphi**: Delphi’s strength ...

    JLink_Windows_V648.zip

    ), 16-byte IDCODE (OSIS) could be rejected even though the correct code was given. Fixed. DLL: Renesas S7G2: QSPI flash programming did not work for QSPI flashes &gt;= 16MB. Fixed. DLL: Resets during ...

    i-vector的工具箱

    The MCR is a standalone set of shared libraries that enables the execution of compiled Matlab applications or components on computers that do not have Matlab installed. The MCR installer can be ...

    数位板压力测试

    Writing simple programs should require only a few lines of code and a minimal understanding of the en-vironment. On the other hand, more advanced features and functions should be available to those ...

    word自动转化为chm

    The capitalized term “Software” used below refers to Macrobject Word-2-CHM 3.0, any updates to the software, any supplemental code provided to you by Macrobject Software, the user manual, any ...

    操作系统(内存管理)

    文将对 Linux™ 程序员可以使用的内存管理技术进行概述,虽然关注的重点是 C 语言,但同样也适用于其他语言。文中将为您提供如何管理内存的细节,然后将进一步展示如何手工管理内存,如何使用引用计数或者内存池来半...

    内存管理内存管理内存管理

    内存管理内幕 dragonimp's blog coder.developer.[designer].ArchitecturE.manager.^_^... posts - 29, comments - 121, trackbacks - 27 My Links Home Contact ...文中将为您提供如何管理内存的细节,然后...

Global site tag (gtag.js) - Google Analytics