Tài liệu Maintainable JavaScript doc

240 214 0
Tài liệu Maintainable JavaScript doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

[...]... for JavaScript It began as a simple utility for finding common problematic JavaScript patterns Over the years, it has evolved into a tool that not only finds potential errors but also warns about stylistic issues in your code Crockford wrote his ideas about JavaScript style in three different pieces: • “The Elements of JavaScript Style, Part 1” covers basic patterns and syntax • “The Elements of JavaScript. .. its own line, indented one level, then the closing brace on its own line For example: // Good var book = { title: "Maintainable JavaScript" , author: "Nicholas C Zakas" }; This is the format most commonly seen in open source JavaScript code Though it’s not commonly documented, the Google JavaScript Style Guide does recommend this format Crockford’s Code Conventions recommends using object literals over... recommend using semicolons Consider the following: Statement Termination | 7 // Original Code function getData() { return { title: "Maintainable JavaScript" , author: "Nicholas C Zakas" } } // The way the parser sees it function getData() { return; { title: "Maintainable JavaScript" , author: "Nicholas C Zakas" }; } In this example, the function getData() is intended to return an object containing some... Good: Break after operator, following line indented two levels callAFunction(document, element, window, "some string value", true, 123, navigator); // Bad: Following line indented only one level callAFunction(document, element, window, "some string value", true, 123, navigator); // Bad: Break before operator callAFunction(document, element, window, "some string value", true, 123 , navigator); In this... the way most JavaScript developers name their variables and functions The Google JavaScript Style Guide, the SproutCore Style Guide, and the Dojo Style Guide all specify use of camel case in most situations Even with the general naming convention of camel case in place, some more specific styles of naming are typically specified Another notation called Hungarian notation was popular for JavaScript around... little bit of thought as to how they should be used for optimum clarity Strings Strings are unique in JavaScript, in that they can be indicated by either double quotes or single quotes For example: // Valid JavaScript var name = "Nicholas says, \"Hi.\""; 14 | Chapter 1: Basic Formatting // Also valid JavaScript var name = 'Nicholas says, "Hi"'; Unlike other languages such as Java and PHP, there is absolutely... use of double quotes for strings The Google JavaScript Style Guide specifies the use of single quotes for strings I prefer using double quotes, because I tend to switch back and forth between writing Java and JavaScript frequently Because Java uses only double quotes for strings, I find it easier to switch between contexts by maintaining that convention in JavaScript This sort of issue should always... part of the JavaScript language but still works in all engines: // Bad var longString = "Here's the story, of a man \ named Brady."; Although this is technically invalid JavaScript syntax, it effectively creates a multiline string in code This technique is generally frowned upon because it relies on a language quirk rather than a language feature, and it is explicitly forbidden in the Google JavaScript. .. not require permission Incorporating a significant amount of example code from this book into your product’s documentation does require permission We appreciate, but do not require, attribution An attribution usually includes the title, author, publisher, and ISBN For example: Maintainable JavaScript by Nicholas Zakas (O’Reilly) Copyright 2012 Nicholas Zakas, 978-1-449-32768-2 If you feel your use... guidelines from various style guides: • The jQuery Core Style Guide specifies indents as tabs • Douglas Crockford’s Code Conventions for the JavaScript Programming Language specifies indents as four spaces • The SproutCore Style Guide specifies indents as two spaces • The Google JavaScript Style Guide specifies indents as two spaces • The Dojo Style Guide specifies indents as tabs I recommend using four spaces . x0 y0 w0 h1" alt="" Maintainable JavaScript Nicholas C. Zakas Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo Maintainable JavaScript by Nicholas. Loose Coupling? 54 Keep JavaScript Out of CSS 55 Keep CSS Out of JavaScript 56 Keep JavaScript Out of HTML 57 Keep HTML Out of JavaScript 59 Alternative

Ngày đăng: 12/02/2014, 12:20

Từ khóa liên quan

Mục lục

  • Table of Contents

  • Introduction

  • Preface

    • Conventions Used in This Book

    • Using Code Examples

    • Safari® Books Online

    • How to Contact Us

    • Part I. Style Guidelines

      • Chapter 1. Basic Formatting

        • Indentation Levels

        • Statement Termination

        • Line Length

        • Line Breaking

        • Blank Lines

        • Naming

          • Variables and Functions

          • Constants

          • Constructors

          • Literal Values

            • Strings

            • Numbers

            • Null

            • Undefined

            • Object Literals

            • Array Literals

Tài liệu cùng người dùng

Tài liệu liên quan