Javascript ÃæÏò¶ÔÏó±à³Ì£¨Ò»£©£º·â×°
ѧϰJavascript£¬×îÄѵĵط½ÊÇʲô£¿
ÎÒ¾õµÃ£¬Object£¨¶ÔÏó£©×îÄÑ¡£ÒòΪJavascriptµÄObjectÄ£ÐͺܶÀÌØ£¬ºÍÆäËûÓïÑÔ¶¼²»Ò»Ñù£¬³õѧÕß²»ÈÝÒ×ÕÆÎÕ¡£
ÏÂÃæ¾ÍÊÇÎÒµÄѧϰ±Ê¼Ç£¬Ï£Íû¶Ô´ó¼ÒѧϰÕâ¸ö²¿·ÖÓÐËù°ïÖú¡£ÎÒÖ÷Òª²Î¿¼ÁËObject-Oriented JavaScriptºÍProfessional JavaScript for Web Developers (2nd Edition)ÕâÁ½±¾Êé¡£ËüÃǶ¼ÊǷdz£ÓÅÐãµÄJavascript¶ÁÎÍƼöÔĶÁ¡£
±Ê¼Ç·Ö³ÉÁ½²¿·Ö¡£½ñÌìµÄµÚÒ»²¿·ÖÊÇÌÖÂÛ"·â×°"£¨Encapsulation£©£¬ÏÂÒ»´ÎµÄµÚ¶þ²¿·ÖÌÖÂÛ"¼Ì³Ð"£¨Inheritance£©¡£
============================
Javascript ÃæÏò¶ÔÏó±à³Ì£¨Ò»£©£º·â×°
×÷ÕߣºÈîÒ»·å
JavascriptÊÇÒ»ÖÖ»ùÓÚ¶ÔÏó£¨object-based£©µÄÓïÑÔ£¬ÄãÓöµ½µÄËùÓж«Î÷¼¸ºõ¶¼ÊǶÔÏó¡£µ«ÊÇ£¬ËüÓÖ²»ÊÇÒ»ÖÖÕæÕýµÄÃæÏò¶ÔÏó±à³Ì£¨OOP£©ÓïÑÔ£¬ÒòΪËüµÄÓï·¨ÖÐûÓÐclass£¨Àࣩ¡£
ÄÇô£¬Èç¹ûÎÒÃÇÒª°Ñ"ÊôÐÔ"£¨property£©ºÍ"·½·¨"£¨method£©£¬·â×°³ÉÒ»¸ö¶ÔÏó£¬ÉõÖÁÒª´ÓÔÐͶÔÏóÉú³ÉÒ»¸öʵÀý¶ÔÏó£¬ÎÒÃÇÓ¦¸ÃÔõô×öÄØ£¿
1. Éú³É¶ÔÏóµÄÔʼģʽ
¼Ù¶¨ÎÒÃÇ°Ñè¿´³ÉÒ»¸ö¶ÔÏó£¬ËüÓÐ"Ãû×Ö"ºÍ"ÑÕÉ«"Á½¸öÊôÐÔ¡£
¡¡¡¡var Cat = {
¡¡¡¡¡¡¡¡name : '',
¡¡¡¡¡¡¡¡color : ''
¡¡¡¡}
ÏÖÔÚ£¬ÎÒÃÇÐèÒª¸ù¾ÝÕâ¸öÔÐͶÔÏó£¬Éú³ÉÁ½¸öʵÀý¶ÔÏó¡£
¡¡¡¡var cat1 = {};
¡¡¡¡¡¡¡¡cat1.name = "´óë";
¡¡¡¡¡¡¡¡cat1.color = "»ÆÉ«";
¡¡¡¡var cat2 = {};
¡¡¡¡¡¡¡¡cat2.name = "¶þë";
¡¡¡¡¡¡¡¡cat2.color = "ºÚÉ«";
ºÃÁË£¬Õâ¾ÍÊÇ×î¼òµ¥µÄ·â×°ÁË¡£µ«ÊÇ£¬ÕâÑùµÄд·¨ÓÐÁ½¸öȱµã£¬Ò»ÊÇÈç¹û¶àÉú³É¼¸¸öʵÀý£¬Ð´ÆðÀ´¾Í·Ç³£Âé·³£»¶þÊÇʵÀýÓëÔÐÍÖ®¼ä£¬Ã»ÓÐÈκΰ취£¬¿ÉÒÔ¿´³öÓÐʲôÁªÏµ¡£
2. ÔʼģʽµÄ¸Ä½ø
ÎÒÃÇ¿ÉÒÔдһ¸öº¯Êý£¬½â¾ö´úÂëÖظ´µÄÎÊÌâ¡£
¡¡¡¡function Cat(name,color){
¡¡¡¡¡¡¡¡return {
¡¡¡¡¡¡¡¡¡¡¡¡name:name,
¡¡¡¡¡¡¡¡¡¡¡¡color:color
¡¡¡¡¡¡¡¡}
¡¡¡¡}
È»ºóÉú³ÉʵÀý¶ÔÏ󣬾͵ÈÓÚÊÇÔÚµ÷Óú¯Êý£º
¡¡¡¡var cat1 = Cat("´óë","»ÆÉ«");
¡¡¡¡var cat2 = Cat("¶þë","ºÚÉ«");
ÕâÖÖ·½·¨µÄÎÊÌâÒÀÈ»ÊÇ£¬cat1ºÍcat2Ö®¼äûÓÐÄÚÔÚµÄÁªÏµ£¬²»ÄÜ·´Ó³³öËüÃÇÊÇͬһ¸öÔÐͶÔÏóµÄʵÀý¡£
3. ¹¹Ô캯Êýģʽ
ΪÁ˽â¾ö´ÓÔÐͶÔÏóÉú³ÉʵÀýµÄÎÊÌ⣬JavascriptÌṩÁËÒ»¸ö¹¹Ô캯Êý£¨Constructor£©Ä£Ê½¡£
Ëùν"¹¹Ô캯Êý"£¬Æäʵ¾ÍÊÇÒ»¸öÆÕͨº¯Êý£¬µ«ÊÇÄÚ²¿Ê¹ÓÃÁËthis±äÁ¿¡£¶Ô¹¹Ô캯ÊýʹÓÃnewÔËËã·û£¬¾ÍÄÜÉú³ÉʵÀý£¬²¢ÇÒthis±äÁ¿»á°ó¶¨ÔÚʵÀý¶ÔÏóÉÏ¡£
±ÈÈ磬èµÄÔÐͶÔÏóÏÖÔÚ¿ÉÒÔÕâÑùд£¬
¡¡¡¡function Cat
Ïà¹ØÎĵµ£º
<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
......
Primitive types
1. undefined, null, boolean, number, string; undefined is derived from null.
e.g. var tmp; typeof tmp == undefined.
e.g. void(javascript:aler(¡®x¡¯)) == undefined.
e.g. undefined==null
2. NaN!=NaN isNaN(¡°123¡±)==false isNaN(¡°blue¡±)==true ......
1.ÔÚHTMLÖÐʹÓÃ<script>ÔªËØÒýÈëJavaScript¡£
¸ÃÔªËØÓÐÁ½¸öÊôÐÔ£¬languageÉùÃ÷ҪʹÓõĽű¾ÓïÑÔ£¬srcÊôÐÔÊÇ¿ÉÑ¡µÄ£¬ÓÃÓÚÒýÓÃÍⲿJavaScriptÎļþ¡£
NB
£ºÏÖÔÚ´ó¶àʹÓÃtypeÊôÐÔ£¨type=“text/javascript”£©Ìæ´úlanguageÊôÐÔ£¬ÒÔ±ã¸üºÃµØÖ§³ÖXHTML£¨¿ÉÀ©Õ¹HTML£©¡£
2.Ò»°ãÈÏΪ ......
×òÌ죬ÎÒ±¾À´´òË㣬°ÑÕâ¸ö»°Ìâ½áÊøÁË¡£
µ«ÊÇ£¬Ð´µ½Ò»°ëµÄʱºò£¬ÎÒͻȻÒâʶµ½£¬JavascriptµÄ¼Ì³ÐÓÐÁ½ÖÖ¡£Ò»ÖÖÊÇ»ùÓÚ"º¯Êý¶ÔÏó"µÄ¼Ì³Ð£¬Ò²¾ÍÊÇÒ»¸öfunctionÈ¥¼Ì³ÐÁíÍâÒ»¸öfunction£¬Õâ¸öÎÒÒѾÔÚ×òÌì½éÉܹýÁË¡£
ÁíÒ»ÖÖÔòÊÇ»ùÓÚ"·Çº¯Êý¶ÔÏó"µÄ¼Ì³Ð£¬²»Éæ¼°function¡£ËüµÄ×ö·¨ÓëÇ°Ò»ÖÖÇé¿öÍêÈ«²»Í¬¡£
Ò»¡¢Ê²Ã´ÊÇ"·Çº¯Êý¶ÔÏ ......