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.

var scareMe = function () {

    alert("Boo!");

    scareMe = function () {

        alert("Double boo!");

    };

};

// using the self-defining function

scareMe(); // Boo!

scareMe(); // Double boo! 

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.

If the function is used with a different name, for example, assigned to a different variable or used as a method of an object, then the redefinition part will never happen and the original function body will be executed.

// 1. adding a new property

scareMe.property = "properly";

// 2. assigning to a different name

var prank = scareMe;

// 3. using as a method

var spooky = {

    boo: scareMe

};

// calling with a new name

prank(); // "Boo!"

console.log(prank.property); // "properly"

// calling as a method

spooky.boo(); // "Boo!"

console.log(spooky.boo.property); // "properly"

// using the self-defined function

scareMe(); // Double boo!

console.log(scareMe.property); // undefined

JavaScript Patterns 4.4 Self-Defining Functions的更多相关文章

  1. JavaScript Patterns 3.2 Custom Constructor Functions

    When you invoke the constructor function with new, the following happens inside the function: • An e ...

  2. 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 ...

  3. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  4. JavaScript Patterns 5.8 Chaining Pattern

    Chaining Pattern - Call methods on an object one after the other without assigning the return values ...

  5. JavaScript Patterns 5.5 Sandbox Pattern

    Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s ...

  6. JavaScript Patterns 5.3 Private Properties and Methods

    All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...

  7. JavaScript Patterns 4.10 Curry

    Function Application apply() takes two parameters: the first one is an object to bind to this inside ...

  8. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  9. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  10. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

随机推荐

  1. elasticsearch配置

    配置文件详解1.0版 配置文件位于es根目录的config目录下面,有elasticsearch.yml和logging.yml两个配置,主配置文件是elasticsearch.yml,日志配置文件是 ...

  2. 订餐App回顾与总结

    MY-HR 成员: 角色分配 学号 博客园 丘惠敏 PM项目经理 201406114203 http://www.cnblogs.com/qiuhuimin/ 郭明茵 用户 201406114204 ...

  3. H5案例学习笔记

    ★基础篇     增加主体结构元素    

  4. 以对象的方式来访问xml数据表(一)

    所有实例代码都是以C#演示—— 在将如何以对象的方式来访问xml数据表之前,我们先来谈谈如何用xml文件作为数据库吧! 平时在开发一些小的应用的时候,需要一个数据库,这个时候虽然可以用SQL serv ...

  5. C#的pictureBox怎样使用多张图片简单切换

    首先,先创建一个新的winform项目ImageTest,选择窗体,起名我ImageForm,在ImageForm拉一个picturebox控件,一个控制器trimer,一个相册imageList,在 ...

  6. 重新想象 Windows 8.1 Store Apps (76) - 新增控件: SearchBox

    [源码下载] 重新想象 Windows 8.1 Store Apps (76) - 新增控件: SearchBox 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之 ...

  7. svn的管理与维护要点—纯手工编写

    由于在公司要维护阿里云的linux服务器,我们的svn服务器就安在阿里云上面.所以经常会涉及到svn的维护操作.离职的时候编写交接文档,刚好有充足的时间写一篇说明介绍,此说明纯原创,不是从网上复制,手 ...

  8. 成熟的RosettaNet解决方案软件介绍

    RosettaNet是一套B2B标准,以标准来优化供应链管理流程,它可以缩短整个供应链各个供货周期.RosettaNet 标准为电子商务标准化提供一个健壮的.非专有的解决方案,它是免费的,通过 Ros ...

  9. 在Hadoop平台跑python脚本

    1.开发IDE,我使用的是PyCharm. 2.运行原理       使用python写MapReduce的“诀窍”是利用Hadoop流的API,通过STDIN(标准输入).STDOUT(标准输出)在 ...

  10. elasticsearch的mapping映射

    Mapping简述 Elasticsearch是一个schema-less的系统,但并不代表no shema,而是会尽量根据JSON源数据的基础类型猜测你想要的字段类型映射.Elasticsearch ...