´ó¸ÅµÄ˼·£¬ ´ó¼Ò¿´Ò»Ï£¬È»ºóÔÙ¿´Ò»¿´ÉÏÃæµÄ´úÂë
1. Ê×ÏÈÏÈ·Ö³öÁ½¸ö´°¿Ú£¬Ò»¸öÓÃÀ´·ÅÖµÔͼ£¬Ò»¸öÓÃÀ´·ÅÖ÷ŴóµÄͼƬ¡£ Ò»¶¨Òª°Ñ´°¿ÚµÄ¿í¸ßÉèÖúÃ(ÖØÒª£¡)
2. Ҫ׼±¸Á½ÕÅͼƬ£¬ÔͼºÍ·Å´óµÄͼƬ¡£
3.ͼƬ·ÅÖµºÃºó£¬¸øÔͼ´°¿Ú¼ÓÊó±êÒƶ¯Ê¼þ
4.»ñµÃʼþ¶ÔÏ󣬼ÆËãÉ«¿éµÄλÖã¬ÅжÏÊÇ·ñ³¬³ö£¬³¬³öÈÃСɫ¿é¹Ì¶¨
5.ÈÃÉ«¿é¸ú×ÅÊó±êÒƶ¯Òƶ¯
6.¼ÆËãÓÒ²àͼƬλÖã¬ÈÃÓÒ²àÒƶ¯
7.¸ø×ó²à¼ÓÊó±êÒÆÈëʼþ£¬ÈÃ×ó²àÉ«¿éºÍÓÒ²àÏÔʾ
8.¸ø×ó²à¼ÓÊó±êÒƳöʼþ£¬ÈÃ×ó²àÉ«¿éºÍÓÒ²àÒþ²Ø
Ò»¸ö´ó¿Ó£¬ÕâʱºòÄã»áÒƶ¯É«¿éʱ·¢ÏÖ£¬¿¨¶Ù£¬É«¿éÂÒÅÜ
ÒÔÉϾÍÊÇ JSʵÏÖµçÉÌ´¥Ãþ·Å´óͼЧ¹û µÄÔ´ÂëºÍ´ó¸Å˼·£¬ÓÐÐËȤµÄͬѧÇë¹Ø×¢
ʲôÊDZհü£¿
±Õ°üÊÇʲô?±Õ°üÊÇClosure£¬ÕâÊǾ²Ì¬ÓïÑÔËù²»¾ßÓеÄÒ»¸öÐÂÌØÐÔ¡£µ«ÊDZհüÒ²²»ÊÇʲô¸´ÔÓµ½²»¿ÉÀí½âµÄ¶«Î÷£¬¼ò¶øÑÔÖ®£¬±Õ°ü¾ÍÊÇ£º±Õ°ü¾ÍÊǺ¯ÊýµÄ¾Ö²¿±äÁ¿¼¯ºÏ£¬Ö»ÊÇÕâЩ¾Ö²¿±äÁ¿ÔÚº¯Êý·µ»Øºó»á¼ÌÐø´æÔÚ¡£±Õ°ü¾ÍÊǾÍÊǺ¯ÊýµÄ¡°¶ÑÕ»¡±ÔÚº¯Êý·µ»Øºó²¢²»ÊÍ·Å£¬ÎÒÃÇÒ²¿ÉÒÔÀí½âΪÕâЩº¯Êý¶ÑÕ»²¢²»ÔÚÕ»ÉÏ·ÖÅä¶øÊÇÔÚ¶ÑÉÏ·ÖÅ䣬µ±ÔÚÒ»¸öº¯ÊýÄÚ¶¨ÒåÁíÍâÒ»¸öº¯Êý¾Í»á²úÉú±Õ°ü¡£
±Õ°ü = º¯ÊýÄÚ²¿´´½¨µÄº¯Êý£¨»òÕß¼ò³ÆÄÚ²¿º¯Êý£© + ¸Ãº¯Êý´´½¨Ê±Ëù´¦»·¾³ÐÅÏ¢
ËùÒÔ±Õ°ü²¢²»µÈÓÚÄäÃûº¯Êý£¬ËäȻҲÓÐÈ˳ÆÕâЩÔÚº¯ÊýÄÚ²¿´´½¨µÄº¯ÊýΪ±Õ°üº¯Êý£¬µ«ÊÇÎÒ¾õµÃÆäʵ²¢²»×¼È·¡£
ÎÒÃÇ¿´Ò»ÏÂÏÂÃæÕâ¶Î´úÂ룺
function init() { var name = "Zilongshanren"; // name ÊÇÔÚ init º¯ÊýÀïÃæ´´½¨µÄ±äÁ¿ // displayName() ÊÇÒ»¸öÄÚ²¿º¯Êý£¬¼´Ò»¸ö±Õ°ü¡£×¢Ò⣬Ëü²»ÊÇÄäÃûµÄ¡£ function displayName() { console.log(name); } //µ± displayName º¯Êý·µ»Øºó£¬Õâ¸öº¯Êý»¹ÄÜ·ÃÎÊ init º¯ÊýÀïÃ涨ÒåµÄ±äÁ¿¡£ return displayName; } var closure = init(); closure(); Zilongshanren undefineddisplayName ÊÇÒ»¸öÔÚ init º¯ÊýÄÚ²¿´´½¨µÄº¯Êý£¬ËüЯ´øÁË init º¯ÊýÄÚ²¿×÷ÓÃÓòµÄËùÓÐÐÅÏ¢£¬±ÈÈçÕâÀïµÄ name ±äÁ¿¡£µ± displayName º¯Êý·µ»ØµÄʱºò£¬Ëü±¾ÉíЯ´øÁ˵±Ê±´´½¨Ê±µÄ»·¾³ÐÅÏ¢£¬¼´ init º¯ÊýÀïÃæµÄ name ±äÁ¿¡£
±Õ°üÓÐʲô×÷Óã¿
ÔÚÀí½âʲôÊDZհüÖ®ºó£¬½ÓÏÂÀ´Äã¿ÉÄÜ»áÎÊ£ºÕⶫÎ÷ÕâôÄÑÀí½â£¬Ëüµ½µ×ÓÐʲôÓ𡣿
ÒòΪÔÚ Js ÀïÃæÊÇûÓа취´´½¨Ë½Óз½·¨µÄ£¬Ëü²»Ïñ java »òÕß C++ÓÐʲô private ¹Ø¼ü×Ö¿ÉÒÔ¶¨Òå˽ÓеÄÊôÐԺͷ½·¨¡£ Js ÀïÃæÖ»Óк¯Êý¿ÉÒÔ´´½¨³öÊôÓÚ×ÔÉíµÄ×÷ÓÃÓòµÄ¶ÔÏó£¬Js ²¢Ã»Óпé×÷ÓÃÓò£¡Õâ¸öÎÒºóÃæ»áÔÙдһƪÎÄÕÂÏêϸ½éÉÜ¡£
±à³ÌÀÏÄñ¶¼ÖªµÀ£¬³ÌÐòдµÃºÃ£¬·â×°ºÍ³éÏóÒªÔËÓõú㡲»Äܶ¨Òå˽ÓеÄÊôÐԺͷ½·¨£¬Òâζ×Å·â×°ºÍ³éÏó¸ù±¾Ã»·¨Óᣡ£¡£
²»Äܶ¨Òå˽ÓеĶ«Î÷£¬ËùÓбäÁ¿ºÍº¯Êý¶¼ public ÏÔÈ»ÓÐÎÊÌ⣬ Global is Evil£¡
±Õ°üÊÇÎÒÃǵľÈÐÇ£¡
ÎÒÃÇ¿´Ò»ÏÂÏÂÃæÕâ¶Î´úÂ룺
var makeCounter = function() { var privateCounter = 0; function changeBy(val) { privateCounter += val; } return { increment: function() { changeBy(1); }, decrement: function() { changeBy(-1); }, value: function() { return privateCounter; } } }; var counter1 = makeCounter(); var counter2 = makeCounter(); console.log(counter1.value()); /* Alerts 0 */ counter1.increment(); counter1.increment(); console.log(counter1.value()); /* Alerts 2 */ counter1.decrement(); console.log(counter1.value()); /* Alerts 1 */ console.log(counter2.value()); /* Alerts 0 */ 0 2 1 0 undefinedÕâÀïÃæµÄ privateCounter ±äÁ¿ºÍ changeBy ¶¼ÊÇ˽Óеģ¬¶ÔÓÚ makeCounter º¯ÊýÍⲿÊÇÍêÈ«²»¿É¼ûµÄ¡£ÕâÑùÎÒÃÇͨ¹ý makeCounter Éú³ÉµÄ¶ÔÏó¾Í°Ñ×Ô¼ºµÄ˽ÓÐÊý¾ÝºÍ˽Óз½·¨È«²¿Òþ²ØÆðÀ´ÁË¡£
ÕâÀïÓÐûÓÐÈÃÄãÏëµ½µãʲô£¿
¹þ¹þ£¬Õâ²»¾ÍÊÇ OO ô£¿·â×°Êý¾ÝºÍ²Ù×÷Êý¾ÝµÄ·½·¨£¬È»ºóͨ¹ý¹«¹²µÄ½Ó¿Úµ÷ÓÃÀ´Íê³ÉÊý¾Ý´¦Àí¡£
µ±È»£¬ÄãÒ²Ðí»á˵£¬ÎÒÓÃÔÐͼ̳ÐÒ²¿ÉÒÔʵÏÖ OO ѽ¡£Ã»´í£¬ÏÖÔڴ󲿷ÖÈËÒ²ÕýÊÇÕâô¸ÉµÄ£¬°üÀ¨ÎÒÃÇ×Ô¼º¡£²»¹ý¼Ì³ÐÕâ¸ö¶«Î÷£¬ÔÚÀí½âÆðÀ´×ÜÊǷdz£À§Äѵģ¬ÒòΪҪÀí½âÒ»¶Î´úÂ룬Äã±ØÐëÒªÀí½âËüµÄËùÓм̳ÐÁ´¡£Èç¹ûÒ»µ©´úÂë³ö bug ÁË£¬Õ⽫ÊǷdz£Äѵ÷ÊԵġ£
³¶Ô¶ÁË£¬½ÓÏÂÀ´£¬ÈÃÎÒÃÇ¿´¿´ÈçºÎÕýÈ·µØʹÓñհü¡£
ÈçºÎÕýÈ·µØʹÓñհü£¿
±Õ°ü»áÕ¼ÓÃÄڴ棬Ҳ»áÓ°Ïì js ÒýÇæµÄÖ´ÐÐЧÂÊ£¬ËùÒÔ£¬Èç¹ûÒ»¶Î´úÂ뱻Ƶ·±Ö´ÐУ¬ÄÇôҪ½÷É÷¿¼ÂÇÔÚÕâ¶Î´úÂëÀïÃæʹÓñհü¡£
ÈÃÎÒÃÇÀ´¿´Ò»¸ö´´½¨¶ÔÏóµÄº¯Êý£º
function MyObject(name, message) { this.name = name.toString(); this.message = message.toString(); this.getName = function() { return this.name; }; this.getMessage = function() { return this.message; };} var myobj = new MyObject(); var myobj = new MyObject();ÿһ´Î±»µ÷ÓÃÉú³ÉÒ»¸öжÔÏóµÄʱºò£¬¶¼»áÉú³ÉÁ½¸ö±Õ°ü¡£Èç¹ûÄãµÄ³ÌÐòÀïÃæÓгÉǧÉÏÍò¸öÕâÑùµÄ MyObject ¶ÔÏó£¬ÄÇô»á¶îÍâ¶à³öºÜ¶àÄÚ´æÕ¼Óá£
¡¡