¿åÁƨ¢ÉC¹Ä¢¿áÁ½ÌÅUIȶÍÉÈÁÄÜ·BB
·²í©èâ·¢ðàL𸢽ÌÅA±¿çàKÇÅ·B
new ðsÉæÈßéAdÆ JavaScript ɨ¯éNXÌp³\¢Ìb - vivid memo
(ÇLF2011/5/15)
OÌLunewðóµÄAJavaScriptÅIuWFNgwü·évªv¢ÌÙ©½¿ªå«©Á½ÌÅA²qÉæÁÄâ«ð¢ÄÝÜ·æB
uRÖÌÔM
u³pÈ¡G»É©¦éBvu´´IuWFNgwüÈCª·éBvÆ¢Á½²Ó©à¢½¾¢½ñÅ·ªAʽÁ½POsÌÖÅIIIuWFNgwüÈñÄìêȢŷæcBJavaScriptÌà@\ðÙÆñÇ»ÌÜÜgÁÄ¢é©ç±»A±ÌsÅÏÞÌÅ·B
»êÉA±Ìvg^CvIp³Ìl¦ûðDouglas CrockfordªÒÝoµ½ÌÍAà¤TNàOÌbÅ·BECMAScript5©çObject.createÆ¢¤¼OÅö®ÉÌp³ê½±Æ©çàí©éÊèAnewðg¤ÃTIÈX^CæèAvg^CvIp³ðg¤p^[Ìûª_Å èå¬ÉÈè èÜ·BiobjectÖͽdp³pÉg£µ¿áÁĢܷªcj
¿åÁƾ¢óߢĵܢܵ½BOñÌLÅÍAªv¤u±¤·é̪êÔí©èâ·¢vÆ¢¤«ûðÜÆßܵ½BÅÍtÉAWIÈ«ûÉ]ÁÄnewðg¤æ¤É¢½êǤÈé©Av·éÉnewð³ñ´ñdisë¤Æ¢¤ÌªA¡ñÌï|Å·BµåϪ¬¶ÁÄ¢é©àµêܹñªA²¹³¾³¢Ü¹B
newLCPFnewðt¯ÈÄàRXgN^ª®ìµÄµÜ¤
á¦ÎA·û`ÌÊÏðvZ·éNXðnewðgÁÄìÁÄÝܵå¤B
function Rectangle(x, y) { this.x = x; this.y = y; alert(x + " x " + y + "Ì·û`ðìèܵ½"); } Rectangle.prototype = { getArea: function(){ return this.x * this.y } }; //g¢ûÌá var rec1 = new Rectangle(3, 4); alert(rec1.getArea()); //12
newðg¤±Æðzè·éNXÌ«ûðͤÜྷé©Mª³¢ÌÅAڵܹͫñªA²²VvÈNXÈÌÅÇßéÆv¢Ü·B
©Äí©éÊèARectangleÍNXêp̶@Å¢Ä éí¯ÅÍÈA²ÊÌÖÅ·BÈÌÅnewðt¯¸ÉÄԱƪūܷB»µÄnewð¯¸ÉÄñŵÜÁ½êÌ®ªAà¤ñíÉC¿«¢±ÆÉÈèÜ·B
Rectangle(3,4); //new¯í·ê print(x); //3 print(y); //4
newðt¯¸ÉÄñ¾êAÖàÌthisÍO[oIuWFNgðwµÜ·B¨©°ÅO[oÏÉxÆyªÇÁ³êĵܢܵ½BO[oóÔÉùÉxÆyª¶Ý·éêA»êçÌÏÍjó³êĵܢܷB
êAÅßÌnÅÍstrict[hű̮ð}§·é±ÆªÅ«éæ¤ÉÈÁĢܷB
nÌo[WÉ˶µÈ¢Ås¦éÎû@ÍAȺÌæ¤ÈK[hßð¢Ä¨±ÆÅ·B
function Rectangle(x, y) { if (!(this instanceof Rectangle)) return new Rectangle(x, y); this.x = x; this.y = y; alert(x + " x " + y); //®ìmF̽ßÌalert } Rectangle.prototype = { getArea: function(){ return this.x * this.y } }
ÅàNXðìé½ÑɱñÈè^¶ð©È¢Æ¢¯È¢ÈñÄAAzçµ¢Æv¢Ü¹ñ©H
newLCQFp³ðl¶·éKvª é
JavaScriptÅÍCX^X»ÆPêp³ª¯`Å·B»Ì½ßACX^X»·éÆ«¾¯ÅÈAp³³¹éÆ«àl¶µÄRXgN^ð©È¢Æ¢¯Ü¹ñB
æÙÇÌ·û`NXðp³µÄA³û`NXðìé±Æðl¦ÄÝÜ·Bf¼ÉƱ¤ÈèÜ·B
function Square(x) { Rectangle.call(this, x, x); } Square.prototype = new Rectangle;
µ©µ±¤¢½êAnew Rectangleµ½Æ«ÉRectangleÌRXgN^ªèÉ®¢ÄµÜ¤½ßAalert()ªÄÎêĵܢܷBÉp³¾¯³¹æ¤Æv¤ÆAHvªKvÉÈèÜ·B
âèû͢© èÜ·B
// p³ðl¶µ½RXgN^ //1. argumentsÌÅ©é function Rectangle(x, y) { if (arguments.length == 0) //p³³¹æ¤ÆµÄ¢éÌÅA½àµÈ¢ return this; ... } //2. arguments[0]Ì^ð©é function Rectangle(x, y) { if (typeof arguments[0] == "string" && arguments[0] == "inherit") return this; ... } ... Square.prototype = new Rectangle("inherit"); //p³³¹éÆ«ÍÁèÌL[[hðn· //3. »à»àRXgN^ðÊ\bhɪ¯Ä¨ function Rectangle(){} Rectangle.prototype = { init: function(x, y) { ... return this; } ... }; var rec1 = new Rectangle().init(3,4); ... Square.prototype = new Rectangle;
ÇñÈRXgN^ð¢Ä¢æ¤ÆÎÅ«éæ¤ÉA±ñÈ«ûð·é±Æà èÜ·B
function Square(x) { Rectangle.call(this, x, x); } Square.prototype = (function(){ function f(){} f.prototype = Rectangle.prototype; return new f; })();
±Ì«ûÍAobjectÖðCCWJµ½ÌƯ`Å·cB»êÈç¢Á»Ì±ÆAíÉobjectÖðg¤æ¤Éµ½ûªy¾Æv¢Ü¹ñ©H
newLCRF½ðâÁÄ¢éÌ©í©èÉ¢
JavaScript©ÌÍvg^Cvx[XÅìçêÄ¢é¹ÉAnewðg¤ÆNXx[Xݽ¢È«ûð·éKvª èÜ·BÅâÁÄ¢é±ÆÆAO¤©ç©¦éC^[tF[Xªá¢·¬éÌÅ·B¨©°ÅtÉí©èÉÈÁĢܷB
newLCSFÇÝÃç¢
newð¶Åg¤êAprototypeÆ¢¤·Á½çµ¢L[[hð½p·é±ÆÉÈèÜ·B±Ì¹¢ÅAnewðOñÉ¢½NXÍA¶ÄÇÝÃçÈ調ŷB
µÉâÖðêØgí¸ÉAPenguinNXðè`µÄÝܵå¤B
function Animal(){} Animal.prototype = { name: "®¨" , breathe: function(){alert("·[Í[")} , sayName: function(){alert(this.name)} }; function Wing(){} Wing.prototype = { fly: function(){alert("ϽϽ")} }; function Bird(){} Bird.prototype = new Animal; Bird.prototype.name = "¹"; Bird.prototype.fly = Wing.prototype.fly; function Penguin(){} Penguin.prototype = new Bird; Penguin.prototype.name = "Øñ¬ñ"; Penguin.prototype.fly = function(){ Bird.prototype.fly.apply(this, arguments); alert("Øñ¬ñÍò×È¢c"); };
±ÌöxÌPÈàÌÅàAprototypeð10ñà©È¯êÎÈèܹñB
êûÅAobjectÖðgÁįα¤ÈèÜ·B
var Animal = { name: "®¨" , breathe: function(){alert("·[Í[")} , sayName: function(){alert(this.name)} }; var Wing = { fly: function(){alert("ϽϽ")} }; var Bird = object(Animal, Wing, { name: "¹" }); var Penguin = object(Bird, { fly: function(){ Bird.fly.apply(this, arguments); alert("Øñ¬ñÍò×È¢c"); } });
±¿çÌûªÇÝâ·¢Æv¢Ü¹ñ©H ±êÅànewðgÁ½ûªí©èâ·¢AÆ¢¤lª¢½çA̯ŷBǤ¼D«È¾¯newðgÁľ³¢BB
ÜÆß
ÈÆà´ÍAnewÍC¿«¢Æv¤B
½¾A¿áñÆðµ½ãÅg¤ÌÍà¿ëñâèÈ¢Æv¢Ü·æBnewð¶ÅgÁ½ûªÖðÄÔñð¸ç¹éÌÅAptH[}XªÇÈéÆ¢¤bgà èÜ·B
keyword: javascript