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 ...
随机推荐
- 在PL/SQL DEV里面有把锁一样的按钮,点击它会跳出“these query result are not updateable,include the ROWID to get updateab
在PL/SQL DEV里面有把锁一样的按钮,点击它会跳出“these query result are not updateable,include the ROWID to get updateab ...
- 洛谷——P3919 【模板】可持久化数组(可持久化线段树/平衡树)
P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目背景 UPDATE : 最后一个点时间空间已经放大 标题即题意 有了可持久化数组,便可以实现很多衍生的可持久化功能(例如:可持久化并查集 ...
- 【Codeforces 584C】Marina and Vasya
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 设cnt表示s1和s2不同的字符的个数 如果cnt>2t 因为这cnt个位置肯定至少有一边不同 显然肯定会有一个f(s,S)的值大于t的 ...
- Java基础学习总结(81)——如何尽可能的减少Java代码中bug
Java编程语言的人气自然无需质疑,从Web应用到Android应用,这款语言已经被广泛用于开发各类应用及代码中的复杂功能. 不过在编写代码时,bug永远是困扰每一位从业者的头号难题.在今天的文章中, ...
- POJ 2217 Secretary
Secretary Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- mongodb数据库的一些常用命令列表
超级用户相关:use admin #增加或修改用户密码db.addUser(ixigua,'pwd') #查看用户列表db.system.users.find() #用户认证db.auth(ixigu ...
- [luoguP2896] [USACO08FEB]一起吃饭Eating Together(DP)
传送门 由于 Di 只有 3 种情况,那么就很简单了 f[i][j][0] 表示前 i 个,且第 i 个变成 j 的 递增序列最小修改次数 f[i][j][1] 表示前 i 个,且第 i 个变成 j ...
- 转载 - Python里面关于 模块 和 包 和 __init__.py 的一些事
出处:http://www.cnblogs.com/tqsummer/archive/2011/01/24/1943273.html python中的Module是比较重要的概念.常见的情况是,事先写 ...
- codeforces gym 100357 K (表达式 模拟)
题目大意 将一个含有+,-,^,()的表达式按照运算顺序转换成树状的形式. 解题分析 用递归的方式来处理表达式,首先直接去掉两边的括号(如果不止一对全部去光),然后找出不在括号内且优先级最低的符号.如 ...
- RabbitMQ消息队列阻塞导致服务器宕机
最近工作中存储服务器由于压力太大无法及时消费消息.这个过程中,导致RabbitMQ意外挂掉,无法访问.下面是部分问题分析过程. 麒麟系统服务器分析 1.服务器异常信息: [root@localhost ...