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ÒÔ¼°ºóÀ´°æ±¾Ö ......
1. CSS
<style>
.dstyle1 {
font-size:13px;
font-weight: bold;
color: #ff6600;
}
.dstyle2 {
font-size:13px;
font-weight: bold;
color: #ffffff;
}
</style>
2. JavaScript
<script type="text/javascript">
m();
function m()
{
document.getElementById("span_new").classNa ......
<html>
<head>
<title>Javascript</title>
<script language="Javascript" type="text/javascript">
function callMethod()
{
/*http://localhost/waa/WebService.asmxΪServic ......
1¡¢¶ÔÏóµÄ¼Ì³Ð£¬Ò»°ãµÄ×ö·¨ÊǸ´ÖÆ£ºObject.extend
prototype.jsµÄʵÏÖ·½Ê½ÊÇ£º
Object.extend = function(destination, source){
for (property in source) {
destination[property] = source[property];
}
return destination;
......