[JavaScript]ÀàÖ®Èý
Ïê½âjavascriptÀà¼Ì³Ð»úÖƵÄÔÀí
Ä¿Ç° javascriptµÄʵÏּ̳з½Ê½²¢²»ÊÇͨ¹ý“extend”¹Ø¼ü×ÖÀ´ÊµÏֵģ¬¶øÊÇͨ¹ýconstructor functionºÍprototypeÊôÐÔÀ´ÊµÏּ̳С£Ê×ÏÈÎÒÃÇ´´½¨Ò»¸öanimalÀà
js ´úÂë
var animal = function(){ //Õâ¾ÍÊÇconstructor function ÁË
this.name = 'pipi';
this.age = 10;
this.height = 0;
}
//½¨Á¢Ò»¸ö¶¯ÎïµÄʵÀý
var a1 = new animal ();
¹¹Ô캯ÊýÓëÆäËûÆÕͨº¯ÊýÇø±ðÔÚÓÚ£¬1.¹¹Ô캯ÊýÀïÓÐ this¹Ø¼ü×Ö£¬2.µ÷Óù¹Ô캯ÊýÊÇʹÓõÄnew¹Ø¼ü×Ö¡£Í¨¹ýnewÔËËã·ûµ÷Óù¹Ô캯Êýanimal ºó£¬ÏµÍ³¾Í»á·µ»ØÒ»¸ö¶ÔÏó£¬Õâ¸ö¶ÔÏó¾ÍÏ൱ÓÚ
µÈͬÕâÑùµÄ·½Ê½À´²úÉújs¶ÔÏó¡£
µ½ÕâÀïÎÒÃÇÖªµÀÈçºÎÔÚjsÖж¨ÒåÒ»¸öÀàÁË£¬½ÓÏÂÀ´ÎÒÃÇչʾÈçºÎдһ¸öcat
js ´úÂë
var a1 = { name:'pipi' ,age:10,height:0 }
//»òÕß
var a1 = new Object();
a1.name='pipi';
a1.age = 10;
a1.height = 0;
js ´úÂë
var cat = function(){
this.play = function(){
alert('cat play')
}
}
cat .prototype = new animal ();
//prototype ÊôÐÔÖ¸ÏòÒ»¸ö¶ÔÏó
var c1 = new cat();
µ½ÕâÀcat¾Í¼Ì³ÐÁËanimal ¶ÔÏó£¬ÀàcatµÄÒ»¸öʵÀý¶ÔÏóc1ÓµÓÐÊôÐÔname,age,height,ºÍ·½·¨playÁË¡£
ÄÇôprototypeÆðµ½ÁËÒ»¸öʲôÑùµÄ×÷ÓÃÄØ£¿
prototype¾ÍºÃ±ÈÒ»¸öÖ¸Õ룬ËüÖ¸ÏòÒ»¸öobject£¬Õâ¸öobject¾Í³ÆΪ×ÓÀà¶ÔÏóµÄÔÐÍ¡£µ±catµÄ¶ÔÏó±»´´½¨µÄʱºò£¬ÓÉÓÚcatµÄ¹¹Ô캯ÊýÓµÓÐprototypeÊôÐÔ£¬ÄÇôcatµÄʵÀý¾Í»á¼ä½ÓÖ¸ÏòÕâ¸öÔÐͶÔ
Ïà¹ØÎĵµ£º
1¡¢¶ÔÏóµÄ¼Ì³Ð£¬Ò»°ãµÄ×ö·¨ÊǸ´ÖÆ£ºObject.extend
prototype.jsµÄʵÏÖ·½Ê½ÊÇ£º
Object.extend = function(destination, source){
for (property in source) {
destination[property] = source[property];
}
return destination;
......
ÔÚjsÖУ¬Ã¿¸ö¶ÔÏó¶¼ÓÐÒ»¸öprototypeÊôÐÔ£º·µ»Ø¶ÔÏóÀàÐÍÔÐ͵ÄÒýÓ᣺ÜÞÖ¿Ú£¡Ï°Óï“ÒÀºù«»Æ°”£¬ÕâÀïµÄºù«¾ÍÊÇÔÐÍ£¬ÄÇô“Æ°.prototype” ·µ»ØµÄ¾ÍÊǺù«£¬»òÕß“Æ°.prototype= new ºù«()”¡£
prototypeµÄÓÃ;£º
¼Ì³Ð
ÓÐÒ»¸ö¶ÔÏó££×ÓÀࣺ
function ×ÓÀà() {
this.lastname = ......
·µ»Ø JavaScript È«¾Ö¶ÔÏó²Î¿¼ÊÖ²á
¶¨ÒåºÍÓ÷¨
isNaN() º¯ÊýÓÃÓÚ¼ì²éÆä²ÎÊýÊÇ·ñÊÇ·ÇÊý×ÖÖµ¡£
Óï·¨
isNaN(x)
²ÎÊýÃèÊö
x
±ØÐè¡£Òª¼ì²âµÄÖµ¡£
·µ»ØÖµ
Èç¹û x ÊÇÌØÊâµÄ·ÇÊý×ÖÖµ NaN£¨»òÕßÄܱ»×ª»»ÎªÕâÑùµÄÖµ£©£¬·µ»ØµÄÖµ¾ÍÊÇ true¡£Èç¹û x ÊÇÆäËûÖµ,Ôò·µ»Ø false¡£
˵Ã÷
isNaN() º¯Êý¿ÉÓÃÓÚÅжÏÆä²ÎÊýÊÇ·ñÊÇ NaN£ ......
//°ÑÊý¾ÝдÈëÊý¾Ý¿â
function
res(){
//»ñÈ¡ÊäÈëÖµ(mynameºÍmymailÊÇÁ½¸öÎı¾¿òµÄid)
var
uname = document.getElementById("myname"
).value;
var
umail = document.getElementById("mymail"
).value;
......
javascript ÀඨÒå4ÖÖ·½·¨
Java´úÂë
/*
¹¤³§·½Ê½--- ´´½¨²¢·µ»ØÌض¨ÀàÐ͵ĶÔÏóµÄ ¹¤³§º¯Êý £¨ factory function £©
*/
function createCar(color,doors,mpg){
......