javascript prototype
ÏÂÃ涼ÊǸöÈËÀí½âÒÔ¼°²éÕÒµÄÍøÉϵÄ×ÊÁÏ,ÈçÓв»¶ÔµÄµØ·½ÇëÖ¸Õý
prototype
prototypeÔÚÕâÀïÊÇÔÐ͵ÄÒâ˼,²»ÊÇÖ¸ÄǸö¿ò¼Ü...
ÿ¸öº¯Êý¾ÍÊÇÒ»¸ö¶ÔÏó£¨Function£©£¬µ±º¯Êý±»½âÎöºó»áÌí¼ÓÒ»¸öprototypeµÄ¶ÔÏó,È»ºóprototype»áÌí¼ÓÒ»¸öconstructorµÄÊôÐÔ
ËüÖ¸ÏòÄǸöº¯ÊýµÄ
±ÈÈ綨ÒåÒ»¸öfunction test(){}
Ëü»á×Ô¶¯Ìí¼ÓÒ»¸öprototype ; ¼´test.prototype={}
È»ºó prototype»á×Ô¶¯Ìí¼ÓÒ»¸ötest.prototype.constructor =test ËüÊÇÖ¸Ï뺯Êý±¾ÉíµÄ
¿ÉÒÔ²âÊÔһϠÀý×ÓÈçÏÂ
function test(){
alert('a');
}
alert(test.prototype); //¿ÉÒÔ¿´³öÊÇobj
test.prototype.constructor(); //¿ÉÒÔ¿´³öÊÇÖ´ÐÐÁËtest
Ò»¸öÀà,ÈçºÎʵÀý»¯³ÉÒ»¸ö¶ÔÏóÁË,ÀàÀïÃæµÄprototypeÀïµÄÊôÐÔÓÖÊÇÔõô³ÉΪʵÀýÀïÃæµÄÊôÐÔµÄÁË
ÕâÀï²»µÃ²»ËµÒ»ÏÂnew µÄ¹ý³Ì
´ó¸Å¿ÉÒÔ·Ö³É3²½
Õâ¸öÀý×Ó¿ÉÕæºÃѽ ¹þ¹þ
<1> var p={}; Ò²¾ÍÊÇ˵£¬³õʼ»¯Ò»¸ö¶ÔÏóp¡£
<2> p.__proto__=Person.prototype;
<3> Person.call(p);Ò²¾ÍÊÇ˵¹¹Ôìp£¬Ò²¿ÉÒÔ³Æ֮Ϊ³õʼ»¯p¡£
±ðÈ˸øµÄÒ»¸önewµÄ¹ý³Ì
/*
new²Ù×÷ÔÀí£¨spiderMonkeyÒýÇæÏ£©
*/
var a = function(sA,sH){
var x = "x";
this.a = sA;
this.h = sH;
this.say = function(){alert(this.a+','+x)}
}
a.prototype.hi = function(){alert(this.h)}
var createInstance = function(source){
var p = {}
var args = Array.prototype.slice.call(arguments,1);
source.apply(p,args);
p.__proto__ = source.prototype;
return p;
}
var A = createInstance(a,"A","hi A");
A.say();
A.hi();
ÈçÒ»¸öÀà ,new Ö®ºóµÄ¶ÔÏóÈçÏÂ
function test(){
this.name="taozi";
}
test.prototype={
sex : "nan"
}
var ss = new test();
/*
ss¾ÍÏ൱ÓÚ
{
name : "nan",
__proto__ : {sex : "nan"}
}
*/
//¿ÉÒÔ²âÊÔÒ»ÏÂ
alert('-----------ÎÒÊÇ»ªÀöµÄ·Ö¸îÏß');
alert(ss.__proto__ === test.prototype); //¼ÇµÃÔÚfirefoxϲâÊÔ ieÏÂÎÞ·¨·ÃÎʵ½__proto__ ¿ÉÒÔ¿´µ½µ¯³öÀ´µÄÊÇtrue
__proto__Õâ¸öÒ²µÃ½éÉÜÒ»ÏÂ,ËüÊǸöºÃ¶«Î÷.....
ÿ¸ö¶ÔÏóÔÚ³õʼ»¯µÄʱºò¶¼»áÓÐÕâôһ¸öÊôÐÔ;
Èç: (ÇëÔÚfirefoxϲâÊÔ)
var test ={};
alert(test.__proto__); //¿ÉÒÔ¿´µ«µ¯³öµÄÊÇobj
µ±ÓÐÒ»¸ö¶ÔÏó ¼ÙÈçÊÇtest ;²éÕÒËûµÄaÊô
Ïà¹ØÎĵµ£º
Ò»¡¢¸ÅÊö
Marquee£ºHtmlÖйöÆÁ¿Ø¼þ£¬¿ÉÒÔʹ°üº¬Ôڴ˱ê¼ÇÖеÄÄÚÈݽøÐи÷¸ö·½Ïò£¬¸÷ÖÖËٶȵĹö¶¯£¬Òò´ËÔÚ²»¿¼ÂÇʹÓõÚÈý·½¿Ø¼þµÄÇ°ÌáÏ£¬¿¼ÂÇʹÓÃMarquee+xmlhttpÀ´ÊµÏÖ¶¨´Î¸üÐÂÒ³ÃæÖеĹö¶¯ÄÚÈÝ£¬´ËʵÏÖÔÚIE6.0¼°ÒÔÉÏ°æ±¾²âÊÔͨ¹ý£¬ÆäËûä¯ÀÀÆ÷ÔÝʱδ²âÊÔ¹ý¡£
MarqueeÖеÄonfinishʼþ£ºÔÚ¸øMarqueeÊôÐÔloop¸ ......
1¡¢function vv()
{
if(event.button==2)
{
alert('·ÀÖ¹·þÎñÆ÷¸ºµ£¹ý´ó£¬ÓÒ¼ü¹¦Äܱ»ÆÁ±Î£¡')
......
ÐèÇó£ºÄ£Äâ±íµ¥Ìá½»²¢¿ÉÉèÖÃÌá½»¼ä¸ô
»¹ºÃÒ³ÃæÊÇajaxÌá½»µÄ£¬Ö»ÒªÐ´¸öjs¶¨Ê±Ìá½»¾ÍokÁË¡£ ÓÚÊÇÕÒÁ˼¸¸öдµÄÀàËÆÓÚsleepµÄ¹¦ÄÜ£¬ÄùýÀ´ÓÃÓ÷¢ÏÖÒ²²»ÊǺܺÃÓá£
×ÔÒÑдÁ˸ö
<script language="javascript">
var base = {};
......
base.auto_submit = function(){
var total = 10 ......
ÃæÏò¶ÔÏóÒѲ»ÔÙÊÇÖ¸·â×°¡¢¼Ì³Ð¡¢³éÏóµÈÉè¼Æ·½·¨£¬Ä¿Ç°ÒѾÉæ¼°µ½ÃæÏò¶ÔÏóµÄ·ÖÎö£¨OOA£¬Object Oriented Analysis£©£¬ÃæÏò¶ÔÏóµÄÉè¼Æ£¨OOD£¬Object Oriented Design£©¼°ÃæÏò¶ÔÏóµÄ±à³ÌʵÏÖ£¨OOP£¬Object Oriented Programming£©¡£±¾Æ¬ÎÄÕÂÖ÷ÒªÌÖÂÛµÄÊÇÈçºÎʵÏÖJavascriptµÄÃæÏò¶ÔÏóÉè¼Æ¡£
JavascriptÊǽű¾ÓïÑÔ£¬ÊÇ»ùÓÚ¶ÔÏó ......
ÔÎÄ£ºJavaScript - The this keyword
ÔÚ JavaScript ÖÐ this ÊÇ×îÇ¿µÄ¹Ø¼ü×ÖÖ®Ò»¡£ÕâƪÌùÎľÍÊÇÒª¸æËßÄãÈçºÎÓúà this¡£
ÏÂÃæÏȽ²ÈçºÎÔÚevent handling
(ʼþ´¦Àí)ÖÐÓÃËü£¬ÔÙ½Ó×ÅÊǽ² this µÄÆäËûÓ÷¨¡£
ËùÓÐÕß(Owner)
ÏÈÀ´¿´¿´º¯Êý doSomething() ÀïµÄ this ¾¿¾¹Ö¸Ïò(refer to)ÁËʲô£¿
function doSomething() { ......