javascript Ö® Closure
http://javascript.crockford.com/private.html
function a() {
var i = 0;
function b() {
alert(++i);
}
return b;
}
var c = a();
c();
µ±ÔÚº¯ÊýbÖзÃÎÊÒ»¸ö±äÁ¿µÄʱºò£¬ËÑË÷˳ÐòÊÇ£º
ÏÈËÑË÷×ÔÉíµÄ»î¶¯¶ÔÏó£¬Èç¹û´æÔÚÔò·µ»Ø£¬Èç¹û²»´æÔÚ½«¼ÌÐøËÑË÷º¯ÊýaµÄ»î¶¯¶ÔÏó£¬ÒÀ´Î²éÕÒ£¬Ö±µ½ÕÒµ½ÎªÖ¹¡£
Èç¹ûº¯Êýb´æÔÚprototypeÔÐͶÔÏó£¬ÔòÔÚ²éÕÒÍê×ÔÉíµÄ»î¶¯¶ÔÏóºóÏȲéÕÒ×ÔÉíµÄÔÐͶÔÏó£¬ÔÙ¼ÌÐø²éÕÒ¡£Õâ¾ÍÊÇJavascriptÖеıäÁ¿²éÕÒ»úÖÆ¡£
Èç¹ûÕû¸ö×÷ÓÃÓòÁ´É϶¼ÎÞ·¨ÕÒµ½£¬Ôò·µ»Øundefined¡£
function Container(param) {
this.member = param;
}
var myContainer = new Container('abc'); //member='abc'
Container.prototype.stamp = function (string) {
return this.member + string;
}
myContainer.stamp('def') //member='abcdef'
function Container(param) {
//private method
function dec() {
if (secret > 0) {
secret -= 1;
return true;
} else {
return false;
}
}
this.member = param;
var secret = 3;
var that = this;
//privileged method
this.service = function () {
if (dec()) {
return that.member;
} else {
return null;
}
};
}
myContainer.service() //abc(first) null(second.....)
Ïà¹ØÎĵµ£º
Ò³ÃæÄÚǶ²»Í¬µÄÍøÒ³ÄÚÈÝʱ£¬µ¼ÖÂiframeµÄ¸ß¶ÈÎÞ·¨ÓëʵÏÖÄÚÈÝ»òä¯ÀÀÆ÷¸ß¶ÈÏàÆ¥Åä¡£´°¿Ú´óС±ä»»Ê±£¬²»ÄÜiframe²»ÄܽøÐÐ×Ô¶¯µ÷Õû¡£ÕâÀÎÒÃÇÀûÓÃJAVASCRIPTÈÃIFRAME¿ò¼ÜµÄ¸ß¶È×ÔÊÊÓ¦¡£
Ê×ÏÈ£¬Ð´³öÈçϵÄJS·½·¨¡£
/**
* ÄÚǶҳÃæµÄ¸ß¶ÈÉèÖÃ
*/
function handleFrameLoad() {
var hDoc = getBodyHeight(document);
var ......
±Õ°ü£¨closure£©ÊÇJavascriptÓïÑÔµÄÒ»¸öÄѵ㣬ҲÊÇËüµÄÌØÉ«£¬ºÜ¶à¸ß¼¶Ó¦Óö¼ÒªÒÀ¿¿±Õ°üʵÏÖ¡£
ÏÂÃæ¾ÍÊÇÎÒµÄѧϰ±Ê¼Ç£¬¶ÔÓÚJavascript³õѧÕßÓ¦¸ÃÊǺÜÓÐÓõġ£
Ò»¡¢±äÁ¿µÄ×÷ÓÃÓò
ÒªÀí½â±Õ°ü£¬Ê×ÏȱØÐëÀí½âJavascriptÌØÊâµÄ±äÁ¿×÷ÓÃÓò¡£
±äÁ¿µÄ×÷ÓÃÓòÎ޷ǾÍÊÇÁ½ÖÖ£ºÈ«¾Ö±äÁ¿ºÍ¾Ö²¿±äÁ¿¡£
JavascriptÓïÑÔµÄÌØÊâÖ®´¦£¬¾ÍÔÚÓ ......
¡¾×ª¡¿http://www.cnblogs.com/chenxizhang/archive/2010/01/01/1637309.html
Èç¹ûÎÒÃÇÐèÒªÔÚjavascript·ÃÎʵ½Ò³ÃæÖж¨ÒåµÄ±äÁ¿£¬´óÖÂÊÇÈçϵÄÓï·¨
//ÕâÊÇ2010Äê1ÔÂ1ÈÕÔö¼ÓµÄ´úÂë
var ismanager = "<%= DepartManager %>";
if (ismanager == "true") {
$(" ......
JavaScriptΪº¯Êý¶ÔÏó¶¨ÒåÁËÁ½¸ö·½·¨£ºapplyºÍcall£¬ËüÃǵÄ×÷Óö¼Êǽ«º¯Êý°ó¶¨µ½ÁíÍâÒ»¸ö¶ÔÏóÉÏÈ¥ÔËÐУ¬Á½Õß½öÔÚ¶¨Òå²ÎÊýµÄ·½Ê½ÓÐËùÇø±ð£º
Function.prototype.apply(thisArg,argArray);
Function.prototype.call(thisArg[,arg1[,arg2…]]);
´Óº¯ÊýÔÐÍ¿ÉÒÔ¿´µ½£¬µÚÒ»¸ö²ÎÊý¶¼±»È¡ÃûΪthisArg£¬¼´ËùÓк¯ÊýÄÚ²¿µ ......