Javascript Ö®prototypeÊôÐÔ
ÔÚjsÖУ¬Ã¿¸ö¶ÔÏó¶¼ÓÐÒ»¸öprototypeÊôÐÔ£º·µ»Ø¶ÔÏóÀàÐÍÔÐ͵ÄÒýÓ᣺ÜÞÖ¿Ú£¡Ï°Óï“ÒÀºù«»Æ°”£¬ÕâÀïµÄºù«¾ÍÊÇÔÐÍ£¬ÄÇô“ư.prototype” ·µ»ØµÄ¾ÍÊǺù«£¬»òÕߓư.prototype= new ºù«()”¡£
prototypeµÄÓÃ;£º
¼Ì³Ð
ÓÐÒ»¸ö¶ÔÏó££×ÓÀࣺ
function ×ÓÀà() {
this.lastname = "Samuel";
}
ÓÐÒ»¸ö¶ÔÏ󣣸¸Àࣺ
function ¸¸Àà() {
this.firstname = "Shen";
}
ÏÖÔÚ×ÓÀàÊÇÓÐÃûÎÞÐÕ£¬¸¸ÀàÊÇÓÐÐÕÎÞÃû£¬Èç¹û×ÓÀàÒªÓÐÃûÓÐÐյϰ£¬Ö»ÒªËµÃ÷££×ÓÀàµÄÔÐÍÊǸ¸Àࣣ¾Í¿ÉÒÔÁË£¬¼´×ÓÀà¼Ì³Ð×Ô¸¸Àࣺ
×ÓÀà.prototype = new ¸¸Àà();
ÖÁ´Ë£¬×ÓÀà¾Í²»ÔÙÊÇÓÐÃûÎÞÐÕÁË¡£
alert(×ÓÀà.firstname + " " + ×ÓÀà.lastname); //Samuel Shen
Ç£Ò»·¢¶ø¶¯È«Éí
¼ÈÈ»prototype·µ»ØµÄÊÇÔÐ͵ÄÒýÓã¬ÄÇôÈç¹û¸Ä±äÔÐ͵ϰ£¬ËùÓм̳Ð×Ô¸ÃÔÐ͵ĶÔÏó¶¼½«Êܵ½Ó°Ïì¡£
function Point(x,y) {
this.x = x;
this.y = y;
}
var p1 = new Point(1,2);
var p2 = new Point(3,4);
Point.prototype.z = 0; //¶¯Ì¬ÎªPointµÄÔÐÍÌí¼ÓÁËÊôÐÔ
alert(p1.z); //0
alert(p2.z); //0
/***************************************************************************/
function Person(name,sex) { //PersonÀàµÄ¹¹Ô캯Êý
this.name = name;
this.sex = sex;
}
Person.prototype.age = 12; //ΪPersonÀàµÄprototypeÊôÐÔ¶ÔÓ¦µÄprototype¶ÔÏóµÄÊôÐÔ¸³Öµ,
//Ï൱ÓÚΪPersonÀàµÄ¸¸ÀàÌí¼ÓÊôÐÔ
Person.prototype.print = function() { //ΪPersonÀàµÄ¸¸ÀàÌí¼Ó·½·¨
alert(this.name+"_"+this.sex+"_"+this.age);
};
var p1 = new Person("name1","male"); //p1µÄageÊôÐԼ̳Ð×ÓPersonÀàµÄ¸¸Àà(¼´prototype¶ÔÏó)
var p2 = new Person("name2","male");
p1.print(); //name1_male_12
p2.print(); //name2_male_12
p1.age = 34; //¸Ä±äp1ʵÀýµÄageÊôÐÔ p1ÊôÐÔÔÚ²»Ö¸ÏòPersonµÄprototypeÊôÐÔ
p1.print(); //name1_male_34
p2.print(); //name2_male_12
Person.prototype.age = 22; //¸Ä±äPersonÀàµÄ³¬ÀàµÄageÊôÐÔ
p1.print(); //name1_male_34(p1µÄageÊôÐÔ²¢
Ïà¹ØÎĵµ£º
¶þʮһ¡¢ÉùÒô
1¡¢¿ÉÒÔ²¥·ÅÉùÒôµÄä¯ÀÀÆ÷£¨Audio-enabled Browsers£©
Q£ºÊ²Ã´¿ÉÒÔä¯ÀÀÆ÷¿ÉÒÔÖ´Ðв¥·ÅÉùÒôµÄ½Å±¾£¿
A£ºÈç¹ûÔÚÉùÒô²¥·ÅÃèÊöµÄ¼¼Êõ¿ÉÒÔÔÚMiscrosoft Internet Explorer 4.0¼°Æä¸üа汾Öй¤×÷£¬ÄÇôͬÑù¿ÉÒÔÔÚNetscape Navigator 3.0¼°Æä¸üа汾Öй¤×÷¡£ÒªÔÚInternet ExplorerÖв¥·ÅÉùÒô£¬ÔÚ4.0ÒÔ¼°ºóÀ´°æ±¾Ö ......
±íµ¥µÄ¿Í»§¶ËjavascriptÑéÖ¤Óи÷ÖÖ¸÷ÑùµÄд·¨£¬µÇ¼ΪformµÄonsubmitʼþ»òsubmit°´Å¤Ð´Ò»¸öº¯Êý¡£¶ÔÓÚС±íµ¥£¨Ö»ÓÐÒ»Á½¸ö±íµ¥ÓòµÄ±íµ¥£©¾Í²»±ØÔÙרÃÅÔÙÓÃjavascriptдһ¸öÑéÖ¤º¯ÊýÁË£¬Ö»ÐèÒªÔÚformµÄonsubmitʼþÀï¼ÓÉÏ£º
onsubmit=”return domainname.value==”?(alert(’ÇëÊäÈëËÑË÷ÄÚÈÝ’),false ......
var obj = new Object();
obj.name = "hello";
obj['name'] = "world";
alert(obj.name);
±»ÈËÎʵ½ÕâÑùÒ»¶Î´úÂ룬alertµÄ½á¹ûÓ¦¸ÃÊÇÊ²Ã´ÄØ£¿
Îһشð£ºhello #½á¹û´ó´íÌØ´í£¡
Ó¦¸ÃÊÇ“world”£¡£¡£¡
ÒòΪjsÊÇÒ»ÖÖËÉÉ¢ÀàÐ͵ÄÓïÑÔ£¬objËäÈ»±»ÉùÃ÷ÊÇÒ»¸ö¶ÔÏ󣬵«ÊǰÑËüµ±³ÉÊý×éÀ´·ÃÎÊҲûʲô²»¿ÉÒÔ¡£
ËäÈ» ......
var myDate = new Date();
myDate.getYear(); //»ñÈ¡µ±Ç°Äê·Ý(2λ)
myDate.getFullYear(); //»ñÈ¡ÍêÕûµÄÄê·Ý(4λ,1970-????)
myDate.getMonth(); //»ñÈ¡µ±Ç°Ô·Ý(0-11,0´ú±í1ÔÂ)
myDate.getDate();   ......
JavascriptË¢ÐÂÒ³ÃæµÄ¼¸ÖÖ·½·¨£º
1 history.go(0)
2 location.reload()
3 location=location
4 location.assign(location)
5 document.execCommand('Refresh')
6 window.navigate(location)
7& ......