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 ...
随机推荐
- git学习(2)----入门
一.git.github和gitlab的区别 Git诞生于2005年,大神Linus的作品,Github诞生于2008年,没有Git就没有GitHub,Github已成为全球最大的代(tong)码(x ...
- Maven 项目debug调试时报Source not found.异常
正如异常描述,那么解决方法当然是指定源码. 测试于:Maven 3.0.5, eclipse-jee-indigo-SR2-win32 异常信息: Source not found. 解决方法: 首先 ...
- 重置默认样式 css reset
html { overflow-x:auto; overflow-y:scroll; } body, dl, dt, dd, ul, ol, li, pre, form, fieldset, inpu ...
- UVA - 11175 From D to E and Back(思路)
题目: 思路: 如图E:图中a.b.c.d是有向图D中的顶点,如果ac.bc都指向cd,而ac又指向ce,那bc同样应该有一条指向ce的边不然就不能从图D转换来.所以直接枚举顶点就可以了. 代码: # ...
- 计算机网络篇(前端、HTTP)
全端工程师需知道的计算机网络知识 一.网络篇-http报文详解 1. 分类 请求报文 响应报文 2. 报文结构 (一).请求报文 一个HTTP请求报文由请求行(request line).请求头部(h ...
- ubuntu14.04 fcitx安装
先卸载ibus sudo apt-get remove ibus (也可尝试不卸载ibus,直接安装fcitx) 添加源 sudo add-apt-repository ppa:fcitx-team/ ...
- 7-26 Windows消息队列
7-26 Windows消息队列(25 分) 消息队列是Windows系统的基础.对于每个进程,系统维护一个消息队列.如果在进程中有特定事件发生,如点击鼠标.文字改变等,系统将把这个消息加到队列当中. ...
- noip模拟赛 铺瓷砖
[问题描述]有一面很长很长的墙. 你需要在这面墙上贴上两行瓷砖. 你的手头有两种不同尺寸的瓷砖, 你希望用这两种瓷砖各贴一行.瓷砖的长可以用分数表示,贴在第一行的每块瓷砖长度为A/B贴在第二行的每块瓷 ...
- js中防止输入为空,或者为字母
function checkNum(){ var num1=document.getElementById("num1").value; var num2=document.get ...
- 中文命名之Hibernate 5演示 - 使用注解(annotation)而非xml定义映射
前文中文编程:中文命名之Hibernate 4+MySQL演示最后留下了个Hibernate 5之后出现的问题, 于是在Hibernate社区提交了报告: Seemingly regression s ...