代码改变世界

JavaScript Patterns 4.9 Configuration Objects

2014-06-17 09:00 by 小郝(Kaibo Hao), 369 阅读, 0 推荐, 收藏, 编辑
摘要:Passing a large number of parameters is not convenient. A better approach is to substitute all the parameters with only one and make it an object. 阅读全文

JavaScript Patterns 4.8 Function Properties - A Memoization Pattern

2014-06-16 23:17 by 小郝(Kaibo Hao), 339 阅读, 0 推荐, 收藏, 编辑
摘要:A Memoization Pattern is a kind of JavaScript Pattern you can use to cache the result of a function with the arguments of the function as the key of the cache. 阅读全文

JavaScript Patterns 4.7 Init-Time Branching

2014-06-15 23:41 by 小郝(Kaibo Hao), 321 阅读, 0 推荐, 收藏, 编辑
摘要:When you know that a certain condition will not change throughout the life of the program, it makes sense to test the condition only once. Browser sniffing (or feature detection) is a typical example. 阅读全文

JavaScript Patterns 4.6 Immediate Object Initialization

2014-06-12 23:58 by 小郝(Kaibo Hao), 281 阅读, 0 推荐, 收藏, 编辑
摘要:protect the global namespace while performing the one-off initialization tasks. 阅读全文

JavaScript Patterns 4.5 Immediate Functions

2014-06-11 23:44 by 小郝(Kaibo Hao), 351 阅读, 0 推荐, 收藏, 编辑
摘要:Immediate Functions helps you wrap an amount of work you want to do without leaving any global variables behind. All the variables you define will be local to the self-invoking functions and you don’t have to worry about polluting the global space with temporary variables. 阅读全文

JavaScript Patterns 4.4 Self-Defining Functions

2014-06-10 22:34 by 小郝(Kaibo Hao), 303 阅读, 0 推荐, 收藏, 编辑
摘要:If you create a new function and assign it to the same variable that already holds another function, you’re overwriting the old function with the new one.This pattern(lazy function definition) is useful when your function has some initial preparatory work to do and it needs to do it only once. A drawback of the pattern is that any properties you’ve previously added to the original function will be lost when it redefines itself. 阅读全文

JavaScript Patterns 4.3 Returning Functions

2014-06-09 22:54 by 小郝(Kaibo Hao), 280 阅读, 0 推荐, 收藏, 编辑
摘要:Use closure to store some private data, which is accessible by the returned function but not to the outside code. 阅读全文

JavaScript Patterns 4.2 Callback Pattern

2014-06-08 23:37 by 小郝(Kaibo Hao), 287 阅读, 0 推荐, 收藏, 编辑
摘要:This post introduces the classis callback usage in JavaScript. 阅读全文

JavaScript Patterns 4.1 Functions Background

2014-06-06 20:13 by 小郝(Kaibo Hao), 196 阅读, 0 推荐, 收藏, 编辑
摘要:This post introduces the top class objects Functions's Background with its functionality, variable scope and Names and Hoisting. 阅读全文

JavaScript Patterns 3.8 Error Objects

2014-06-05 23:56 by 小郝(Kaibo Hao), 337 阅读, 0 推荐, 收藏, 编辑
摘要:The post introduces how to customize the error object. 阅读全文

JavaScript Patterns 3.7 Primitive Wrappers

2014-06-03 09:16 by 小郝(Kaibo Hao), 305 阅读, 0 推荐, 收藏, 编辑
摘要:One reason to use the wrapper objects is when you want to augment the value and persist state. Because primitives are not objects, they cannot be augmented with properties. 阅读全文

JavaScript Patterns 3.6 Regular Expression Literal

2014-06-02 12:08 by 小郝(Kaibo Hao), 652 阅读, 0 推荐, 收藏, 编辑
摘要:Te Regular Expression literal also creates new objects in ECMA Script 5. And one last note that calling RegExp() without new(as a function, not as a constructor) behaves the same as with new. 阅读全文

JavaScript Patterns 3.5 JSON

2014-06-01 16:19 by 小郝(Kaibo Hao), 278 阅读, 0 推荐, 收藏, 编辑
摘要:The only syntax difference between JSON and the object literal is that property names need to be wrapped in quotes to be valid JSON. In object literals the quotes are required only when the property names are not valid identifiers, for example, they have spaces {"first name": "Dave"}. 阅读全文

JavaScript Patterns 3.4 Array Literal

2014-05-30 23:03 by 小郝(Kaibo Hao), 446 阅读, 0 推荐, 收藏, 编辑
摘要:To avoid potential errors when creating dynamic arrays at runtime, it's much safer to stick with the array literal notation. 阅读全文

JavaScript Patterns 3.3 Patterns for Enforcing new

2014-05-29 13:24 by 小郝(Kaibo Hao), 310 阅读, 0 推荐, 收藏, 编辑
摘要:When your constructor has something like this.member and you invoke the constructor without new, you’re actually creating a new property of the global object called member and accessible through window.member or simply member.This post introduces how to enforce "new an object" with the constructor even if there is no new in front of the constructor. 阅读全文

JavaScript Patterns 3.2 Custom Constructor Functions

2014-05-28 22:56 by 小郝(Kaibo Hao), 290 阅读, 0 推荐, 收藏, 编辑
摘要:When you invoke the constructor function with new, the following happens inside the function: • An empty object is created and referenced by this variable, inheriting the prototype of the function. • Properties and methods are added to the object referenced by this. • The newly created object referenced by this is returned at the end implicitly (if no other object was returned explicitly). 阅读全文

JavaScript Patterns 3.1 Object Literal

2014-05-27 12:30 by 小郝(Kaibo Hao), 275 阅读, 0 推荐, 收藏, 编辑
摘要:Don't use new Object(); use the simpler and reliable object literal instead.This will help you get readable and reliable code. 阅读全文

JavaScript Patterns 2.12 Writing API Docs

2014-05-26 13:57 by 小郝(Kaibo Hao), 304 阅读, 0 推荐, 收藏, 编辑
摘要:Process of generating API documentation • Writing specially formatted code blocks • Running a tool to parse the code and the comments • Publishing the results of the tool, which are most often HTML pages 阅读全文

JavaScript Patterns 2.11 Writing Comments

2014-05-26 13:43 by 小郝(Kaibo Hao), 287 阅读, 0 推荐, 收藏, 编辑
摘要:Document all functions, their arguments and return values, and also any interesting or unusual algorithm or technique. Think of the comments as hints ... 阅读全文

JavaScript Patterns 2.10 Naming Conventions

2014-05-25 17:29 by 小郝(Kaibo Hao), 327 阅读, 0 推荐, 收藏, 编辑
摘要:This post introduces how to name the functions, constructors and properties correctly. 阅读全文
上一页 1 2 3 4 5 6 ··· 11 下一页