search
HomeWeb Front-endJS TutorialTalking about project practice from the JavaScript language itself_javascript skills

从javascript语言本身谈项目实战
                                             
dulao5  2005-1-15

       随着ajax的升温,javascript越来越得到人们的重视。重要的是,ajax在一定程度上带来了web软件架构上的变化,人们把越来越多的功能分配到客户端实现,javascript子项目规模越来越大。如何更高效的使用javascript,如何更科学的组织javascript,如何更顺利的保证项目进展?我想就我的经验谈一点浅见。

一。 开发人员需要认真学习javascript语言本身
       由于javascript是“世界上最被误解的语言”, 大部分人对javascript语法并没有全面了解过,只是凭借看起来很像c或者java的关键字按照自己的理解写javascript代码。其实 javascript是一种很独特的语言,和c++/java有非常大的区别,要想用javascript做大一些的项目,开发人员必须老老实实的学习 javascript的语法。真正掌握了语法后,我们才不会把delete看成释放内存对象,才不会为到底参数传递是值传递还是引用传递而烦恼。真正理解了javascript的基于原型的OO方式,才可能写出具有良好架构的javascript程序。
       《javascript权威指南》是一本最合适的书,郑重推荐。另外ECMA262 文档可以作为参考。网上流行的jscript手册chm版本使用起来比较方便,不过这是微软的jscript实现,和标准的javascript略有区别,使用时应该注意上面的注脚信息。关于javascript的原型和OO,网上已经有很多文章介绍了,在此不再多说。

二。 良好的代码来源于良好的设计
       只有设计优良,代码才会写的漂亮。现在的javascript子项目已经不是以前web项目中的“边角料”和散兵游勇了,在较大的ajax项目内, javascript将非常复杂,ajax的异步模型也和以前顺序执行的程序设计有所区别。所以建议做javascript前首先做好设计。推荐使用用例驱动的方式,把用例分析清楚,以便全局考虑所有可能的页面交互过程,绘出页面内一些对象之间的交互图,分析一些数据对象的状态,作出精细的 javascript设计。

三。 使用设计模式,复用其他领域的设计经验
        如果javascript非常复杂,可以考虑使用一些模式。我想大部分做javascript的开发者都不是“javascript科班”出身吧:) 掌握了javascript的语言本质,就可以复用我们在其他领域的经验了。使用javascript框架或者ajax框架,使用单例模式做一个全局的数据缓冲池,或者使用观察者模式把界面对象和数据对象分离,使用命令模式实现用户的操作队列等等。

四。 调试代码的技巧
        javascript的代码不太好调试,这是由于:

  • 一般的开发人员对javascript语言本身不太精通。也就是上面提到的。
  • web项目包含较多的因素,复杂性加剧。服务端脚本、模板、html、js等很多环节都可能增加调试难度。
  • 浏览器存在兼容性问题。有可能在一个细节问题上IE、Mozilla、opera等浏览器都有差异。
  • 工具的缺乏。虽然mozilla的jsdebugger非常好用(还有bug,比如eval时调试器有些问题),但是其他浏览器环境下调试工具就不怎么样了。ms系统自带的script debug工具调试本地代码还可以,直接调试网站js代码表现欠佳。opera除了javascript控制台外我没有找到其他调试工具。

       在此我推荐几个调试技巧:

  1. Use the jsdebugger plugin for Mozilla firefox . I won’t say more about this, it’s the most classic js debugging tool. Online debugging of remote site JavaScript is great.
  2. Isolate the problem, create local html files and js files, use ms script debug debugging tool to debug. If the js module is relatively independent, you can use this tool. If you are writing hta projects, this tool is of course the first choice.
  3. httpWatch This is a plug-in for IE. It is very easy to use. It can monitor any http session in IE and see the original text of the http session. You can use this tool to find out whether your program has a session with the server, and what the parameters & returned data are.
  4. Create a textarea for debugging in the webpage
    You can create a textarea in the webpage to accept the js statement you want to run, and then add a button to use the eval function of js to execute what you input code.
    This method is very suitable for online debugging. After an error occurs on the web page, write code to output the object value in the page. It is recommended to write some dump tool functions to use together for better results.
    I like this method very much. You can use the switch to open the hidden textarea in the page for debugging at any time. It feels like connecting a terminal to a server, and then using the shell to do anything:) Functions can be redefined here , you can arbitrarily operate any element in the interface, call any function of any object, and output any run-time value you need.
  5. Use exceptions and assertions
    Using the try{}catch(e){} structure can not only shield error messages, but also make the interface more friendly. Our programs can use exceptions and throw exceptions to build a better error handling mechanism.
    There is such a story. I wrote this code casually when using the string.localeCompare function:
    var iRe = str1.localeCompare(str2);
    switch(iRe){
    0: return ....
    1: return ....
    -1:return ....
    defalut:throw "error:localeCompare return other value"
    }
    Forget it after writing it Unexpectedly, when my colleague used firefox under linux, an exception was thrown. Then we learned that localeCompare under linux firefox returns not just 0/1/-1, but a specific value.
    This exception throw effectively detects imperfections in the code.

    It is very good to get more detailed call stack information after exception dump under firefox. IE's exception information is not so detailed.

    Exceptions and assertions can also be combined into a very effective debugging tool.
    Assertion (assert) is a very effective debugging tool in other languages. It often appears in this form:
    assert();
    When the program is in debug state, when When the condition is false, the system aborts and reports this assertion. Since the assertion is defined by ourselves, we can easily determine where the error is and find the bug.
    The JavaScript language does not provide macros, nor does it provide assert. We can simulate it like this
    if(_is_debug) assert = function(expression, strLable){
    if(!expression) throw Error(strLable);
    }
    else assert = function(){};//_is_debug is a global variable
    This allows the program to throw an exception in debug mode when "impossible things" happen, and then release Ignore it in the version.

    You can output the call information of the current stack like this to make up for the defect that the exception object in IE does not have stack information just mentioned:
    function callStackInfo(){
    var s="",line="";
    var cer=arguments.callee.caller;
    while(cer){
    var sf=cer.toString();
    s =line sf.substring(sf.indexOf('function'),sf.indexOf('{')) "n";
    line=".." line;
    cer=cer.caller;
    }
    return s;
    }

This article only discusses the use of JavaScript in web development, especially the development of Ajax. It mainly focuses on how to better use "pure JavaScript". There are many other aspects of web development, such as xml and Dom, which are actually closely related to javascript, but this article does not cover them, so please forgive me. Friends are welcome to provide more opinions on my discussion.
--
------------------------------------------------ ------------------------
Yours sincerely, dulao5

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),