JavaScript Patterns 1 Introduction
1.1 Pattern
"theme of recurring events or objects… it can be a template or model which can be used to generate things" (http://en.wikipedia.org/wiki/Pattern).
• Design patterns - Elements of Reusable Object-Oriented Software.
• Coding patterns - JavaScript-specific patterns and good practices related to the unique features of the language, such as the various uses of functions.
• Antipatterns - An antipattern is not the same as a bug or a coding error; it's just a common approach that causes more problems than it solves.
1.2 JavaScript: Concepts
None-Objects: Primitive types - number, string, boolean, null, and undefined
1.2.1 Object- Oriented
Activation Object which is a global object which has attributes.
Object: a collection of named properties, a list of key-value pairs. Some properties could be functions.
Objects types
- Native
Described in the ECMAScript standard
- Host
Defined by the host environment (for example, the browser environment, e.g. window and all the DOM object) .
Objects can also be categorized by:
- Build-in (e.g. Array, Date).
- User-defined (e.g. var o ={}).
1.2.2 No Classes
There are no long parent-child inheritance chains.
There are no classes and object composition is what you do anyway.
1.2.3 Prototypes
prototype is an object (not a class or anything special) and every function has a prototype property.
1.2.4 Environment
- Browser patterns
- Practical applications of a pattern
1.3 ECMAScript 5
Strict mode - for backward compatible.
function my() {
"use strict";
// rest of the function...
}
This means the code in the function is executed in the strict subset of the language. For older browsers this is just a string not assigned to any variable, so it's not used, and yet it's not an error.
In this sense ES5 is a transitional version—developers are encouraged, but not forced, to write code that works in strict mode.
Principle on writing code under strict mode
• Ensuring the offered code samples will not raise errors in strict mode
• Avoiding and pointing out deprecated constructs such as arguments.callee
• Calling out ES3 patterns that have ES5 built-in equivalents such as Object.create()
1.4 JSLint - A kind of tool for grammar check.
1.5 The console - fire bug
JavaScript Patterns 1 Introduction的更多相关文章
- JavaScript Patterns 7.1 Singleton
7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.6 Mix-ins
Loop through arguments and copy every property of every object passed to the function. And the resul ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns
In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...
- JavaScript Patterns 5.9 method() Method
Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...
随机推荐
- CAD由一个自定义实体事件中的id得到自定义实体对象(com接口VB语言)
由一个自定义实体事件中的id得到自定义实体对象.该函数只能在自定义实体事件中调用. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 ...
- 15 AJAX
AJAX AJAX 简介 AJAX 是 异步 JavaScript 及 XML(Asynchronous JavaScript and XML)的缩写.AJAX 不是一种新的编程语言,而是一种用于创 ...
- 我的第一个随笔——MarkDown的简单用法!
目录 我的第一个笔记 1. 学习简单的markdown语法 1.1 标题 1.2 引用 1.3 倾斜与加粗 1.4无序列表 1.5有序列表 1.6图片和网页 1.7分割线 1.8代码块 1.9结尾 2 ...
- 一篇入门MongoDB
目录 1.MongoDB 基本介绍 2.MongoDB 基本概念 3.数据库操作 4.集合操作 5.文档操作 6.查询条件 7.索引 1.MongoDB 基本介绍 (1)安装 MongoDB 简单来说 ...
- 关于js中的事件委托小案例
需求:页面上有一个按钮,和一个空的ul,要求点击按钮,会给ul中动态添加li元素,然后,点击动态添加的元素,在控制台上输出,这是第几个元素 <ul> </ul> <but ...
- The Falling Leaves(建树方法)
uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...
- [bzoj3671][Noi2014][随机数生成器] (贪心+位运算+卡空间)
Description Input 第1行包含5个整数,依次为 x_0,a,b,c,d ,描述小H采用的随机数生成算法所需的随机种子.第2行包含三个整数 N,M,Q ,表示小H希望生成一个1到 N×M ...
- Navicat premium连接Oracle报ORA-12541错误
1:ORA-12541 原因:Oracle TNS监听服务没开 解决:
- String类的获取功能
/* * String类的获取功能: * int length():获取字符串的长度,其实也就是字符个数 * char charAt(int index):获取指定索引处的字符 * int index ...
- HDU 2242 连通分量缩点+树形dp
题目大意是: 所有点在一个连通图上,希望去掉一条边得到两个连通图,且两个图上所有点的权值的差最小,如果没有割边,则输出impossible 这道题需要先利用tarjan算法将在同一连通分量中的点缩成一 ...