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. CSS程序思想

    CSS的设计思想,比如:CSS预处理器.CSS对像(OOCSS).SMACSS.Atomic设计和OrganicCSS等             一.CSS预处理器最重要的功能:      1.连接: ...

  2. 一种感觉不太好的设置radioButton的方法

    从后台传到前台,让前台的Radiobutton被选中. jquery代码: if(b_type == '') { return false; } else if($('input[name = &qu ...

  3. .NET ORM 哪家强

    ORM到底哪家强? 很多人都想知道这个问题,自已也没测试过,只能道听途说. 闲的无聊就将几个ORM拿出来比一比,假如怀疑测试代码有问题可以将它下载下来慢慢研究. 参赛ORM 1.SqlSugar:是一 ...

  4. UWP开发入门(十一)——Attached Property的简单应用

    UWP中的Attached Property即附加属性,在实际开发中是很常见的,比如Grid.Row: <Grid Background="{ThemeResource Applica ...

  5. IOS开发UI基础UIPikerView的属性

    UIPikerView的属性 1. numberOfComponents:返回UIPickerView当前的列数NSInteger num = _pickerView.numberOfComponen ...

  6. 重构第23天 引用参数对象(Introduce Parameter Object)

    理解:有时候我们的一个方法,需要很多个参数,太多参数,不易阅读和理解,我们就可以把多个参数封装成一个对象. 详解: 重构前代码: public class Registration { public ...

  7. meta元素常用属性整理

    感谢菜鸟教程 参考资料:http://www.runoob.com/w3cnote/meta.html

  8. SqlServer定时备份数据库和定时杀死数据库死锁解决

    上周五组长对我说了一句要杀死数据库的死锁进程,有时候同一时刻不停写入数据库会造成这种情况的发生,因为自己对数据库不是很熟悉,突然组长说了我也就决定一定要倒腾一下,不然自己怎么提高呢?现在不研究,说不定 ...

  9. Docker on CentOS for beginners

    Introduction The article will introduce Docker on CentOS. Key concepts Docker Docker is the world's ...

  10. Angularjs,WebAPI 搭建一个简易权限管理系统 —— 基本功能演示(二)

    目录 前言 Angularjs名词与概念 Angularjs 基本功能演示 系统业务与实现 WebAPI项目主体结构 Angularjs 前端主体结构 基本功能演示(二) 非常抱歉这个月实在太忙,一直 ...