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.

// constructor

function Waffle() {

    this.tastes = "yummy";

}

// a new object

var good_morning = new Waffle();

console.log(typeof good_morning); // "object"

console.log(good_morning.tastes); // "yummy"

// antipattern:

// forgotten `new`

var good_morning = Waffle();

console.log(typeof good_morning); // "undefined"

console.log(window.tastes); // "yummy" 

This undesired behavior is addressed in ECMAScript 5, and in strict mode this  will no longer point to the global object. If ES5 is not available, there’s still something you can do to make sure that a constructor function always behaves like one even if called without new.

Naming Convention

uppercase the first letter in constructor names (MyConstructor) and lowercase it in “normal” functions and methods (myFunction).

Using that

function Waffle() {

    var that = {};

    that.tastes = "yummy";

    return that;

} 

function Waffle() {

    return {

        tastes: "yummy"

    };

} 

var first = new Waffle(),

      second = Waffle();

console.log(first.tastes); // "yummy"

console.log(second.tastes); // "yummy"

Disadvantage

The link to the prototype is lost, so any members you add to the Waffle()prototype will not be available to the objects.

Self-Invoking Constructor

In the constructor you check whether this is an instance of your constructor, and if not, the constructor invokes itself again, this time properly with new:

function Waffle() {

    if (!(this instanceof Waffle)) {
return new Waffle();
} this.tastes = "yummy";
} Waffle.prototype.wantAnother = true; // testing invocations var first = new Waffle(), second = Waffle(); console.log(first.tastes); // "yummy" console.log(second.tastes); // "yummy" console.log(first.wantAnother); // true console.log(second.wantAnother); // true

JavaScript Patterns 3.3 Patterns for Enforcing new的更多相关文章

  1. [Javascript] Introducing Reduce: Common Patterns

    Learn how two common array functions - map() and filter() - are syntactic sugar for reduce operation ...

  2. [Design Patterns] 01. Creational Patterns - Abstract Factory

    设计模式是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结,使用设计模式的目的是提高代码的可重用性,让代码更容易被他人理解,并保证代码可靠性.它是代码编制真正实现工程化. 四个关键元素 ...

  3. [Design Patterns] 03. Behavioral Patterns - Observer Pattern

    前言 参考资源 Ref: 史上最全设计模式导学目录(完整版) 观察者模式-Observer Pattern[学习难度:★★★☆☆,使用频率:★★★★★] 对象间的联动——观察者模式(一):多人联机对战 ...

  4. [Design Patterns] 02. Structural Patterns - Facade Pattern

    前言 参考资源 史上最全设计模式导学目录(完整版) 只把常用的五星的掌握即可. 外观模式-Facade Pattern[学习难度:★☆☆☆☆,使用频率:★★★★★] 深入浅出外观模式(一):外观模式概 ...

  5. [Regular Expressions] Find Repeated Patterns

    Regular Expression Quantifiers allow us to identify a repeating sequence of characters of minimum an ...

  6. Architecture Patterns

    This chapter provides guidelines for using architecture patterns. Introduction Patterns for system a ...

  7. 【JavaScript】直接拿来用!最火的前端开源项目(一)

    摘要:对于开发者而言,了解当下比较流行的开源项目很是必要.利用这些项目,有时能够让你达到事半功倍的效果.为此,本文整理GitHub上最火的前端开源项目列表,这里按分类的方式列出前九个. 对于开发者而言 ...

  8. 每个JavaScript开发人员应该知道的33个概念

    每个JavaScript开发人员应该知道的33个概念 介绍 创建此存储库的目的是帮助开发人员在JavaScript中掌握他们的概念.这不是一项要求,而是未来研究的指南.它基于Stephen Curti ...

  9. SpingMVC 核心技术帮助文档

    声明:本篇文档主要是用于参考帮助文档,没有实例,但几乎包含了SpringMVC 4.2版本的所有核心技术,当前最新版本是4.3,4.2的版本已经经是很新的了,所以非常值得大家一读,对于读完这篇文档感觉 ...

随机推荐

  1. Angular系列------AngularJS快速开始(转载)

    Hello World! 开始学习AngularJS的一个好方法是创建经典应用程序“Hello World!”: 使用您喜爱的文本编辑器,创建一个HTML文件,例如:helloworld.html. ...

  2. [Solution] AOP原理解析及Castle、Autofac、Unity框架使用

    本节目录: AOP介绍 AOP基本原理 AOP框架 Castle Core Castle Windsor Autofac Unity AOP介绍 面向切面编程(Aspect Oriented Prog ...

  3. 【UWP】对 Thickness 类型属性进行动画

    好几个月没写 blog 了,一个是在忙新版的碧影壁纸,另一方面是等(观望)周年更新的 api(不过现在还是比较失望,仍然没法支持矩形以外的 Clip).闲话少说,进入主题. 在 UWP 中,出于性能考 ...

  4. JavaScript对象字面量

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  5. 组合数学 - 母函数的运用 + 模板 --- hdu : 2082

    找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. sql联合查询去除重复计算总和

    1.首先来个联合查询 SELECT 字段1, 字段2, 字段3, 字段4 FROM 表1 INNER JOIN 表2 ON 表1.字段x = 表2.字段x x:代表随意的一个,只要在联合查询的两张表都 ...

  7. 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域

    [源码下载] 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 动态分配内存 链 ...

  8. C#根据CPU+磁盘标号来注册软件

    很多私人软件都需要自己的作品出售给别人只能一台电脑使用,不可以随便一个电脑都可以运行自己的软件,所以就有了软件注册限制的控制,收集了一个注册软件的帮助类,分享记录一下. 功能介绍:    根据CPU+ ...

  9. 字符串中Emoji表情处理

    吃了经验的亏,因为Emoji表情引起的项目bug被撸主遇到两次了,总有一些调皮的小朋友爱用表情来搞点事.第一次把当时那个表改为utf8mb4解决了,第二次说啥都不好使.网上找了半天,发现好多人不去实验 ...

  10. IBATIS动态SQL(转)

    直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的IF-ELSE条件语句和一连串的字符串连接.对于这个问题,I ...