Self-defining functions模式

 
  
/* Title: 自我定义函数
Description: 自身重写,以减少第一次调用以后的代码量
*/
var scareMe = function () {
alert ( "Boo!" );
scareMe = function () {
alert ( "Double boo!" );
};
};
// 1. 添加阴属性
scareMe . property = "properly" ;
var prank = scareMe ;
var spooky = {
boo : scareMe
};
prank (); // "Boo!"
prank (); // "Boo!"
console . log ( prank . property ); // "properly"
spooky . boo (); // "Boo!"
spooky . boo (); // "Boo!"
console . log ( spooky . boo . property ); // "properly"
// 使用自我定义函数
scareMe (); // Double boo!
scareMe (); // Double boo!
console . log ( scareMe . property ); // undefined

你可能感兴趣的:(javascript)