function Animal() {}
Animal.prototype = {
constructor: Animal,
name: "animal",
age: 1,
hobbies: ["dance", "sing", "play"],
sayName: function() {
alert(this.name);
}
};
var cat = new Animal();
var dog = new Animal();
cat.hobbies.push("sleep");
alert(cat.hobbies); // "dance", "sing", "play", "sleep"
alert(dog.hobbies); // "dance", "sing", "play", "sleep"
alert(cat.hobbies === dog.hobbies); // true
ËÄ. ×éºÏʹÓù¹Ô캯ÊýģʽºÍÔÐÍģʽ
function Animal(name, age) {
this.name = "animal";
this.age = 1;
this.hobbies = ["dance", "sing", "play"];
}
Animal.prototype = {
constructor: Animal,
sayName: function() {
alert(this.name);
}
};
var cat = new Animal("cat", 2);
var dog = new Animal("dog", 3);
cat.hobbies.push("sleep");
alert(cat.hobbies); // "dance", "sing", "play", "sleep"
alert(dog.hobbies); // "dance", "sing", "play"
alert(cat.hobbies === dog.hobbies); // false
alert(cat.sayName === dog.sayName); // true
Îå. ¶¯Ì¬ÔÐÍģʽ
function Animal(name, age) {
this.name = name;
this.age = age;
if(typeof this.sayName != "function") {
Animal.prototype.sayName = function() {
alert(this.name);
}
}
}
var cat = new Animal("cat", 12);
cat.sayName(); // "cat"
ʹÓö¯Ì¬ÔÐÍģʽʱ£¬²»ÄÜʹÓöÔÏó×ÖÃæÁ¿ÖØдÔÐÍ¡£Èç¹ûÔÚÒѾ´´½¨ÁËʵÀýµÄÇé¿öÏÂÖØдÔÐÍ£¬ÄÇô¾Í»áÇжÏÏÖÓÐʵÀýÓëÐÂÔÐÍÖ®¼äµÄÁªÏµ¡£
Áù. ¼ÄÉú¹¹Ô캯Êýģʽ
¼ÄÉú¹¹Ô캯ÊýģʽµÄ»ù±¾Ë¼ÏëÊÇ´´½¨Ò»¸öº¯Êý£¬¸Ãº¯ÊýµÄ×÷Óýö½öÊÇ·â×°´´½¨¶ÔÏóµÄ´úÂ룬ȻºóÔÙ·µ»Øд´½¨µÄ¶ÔÏó¡£
´Ó±íÃæÉÏ¿´£¬Õâ¸öº¯ÊýºÜÏñµäÐ͵Ĺ¹Ô캯Êý¡£³ýÁËʹÓÃnew²Ù×÷·ûÖ®Í⣬Õâ¸öģʽ¸ú¹¤³§Ä£Ê½³¤µÃһģһÑù¡£¹¹Ô캯ÊýÔÚ²»·µ»ØÖµµÄÇé¿öÏ£¬Ä¬Èϻ᷵»ØжÔÏóʵÀý¡£¶øͨ¹ýÔÚ¹¹Ô캯ÊýµÄĩβÌí¼ÓÒ»¸öreturnÓï¾ä£¬¿ÉÒÔÖØдµ÷Óù¹Ô캯Êýʱ·µ»ØµÄÖµ¡£
function Animal(name, age) {
var o = new Object();
o.name = name;
o.age = age;
o.sayName = function() {
alert(this.name);
}
return o;
}
var cat = new Animal("cat", 12);
cat.sayName(); // "cat"
ÓÉÓÚ·µ»ØµÄ¶ÔÏóÓë¹¹Ô캯Êý»ò¹¹Ô캯ÊýµÄÔÐÍÖ®¼äûÓÐʲô¹Øϵ£¬Òò´Ë²»ÄÜÒÀÀµinstanceof²Ù×÷·ûÀ´È·¶¨¶ÔÏóÀàÐÍ¡£
½¨ÒéÔÚ¿ÉÒÔʹÓÃÆäËûģʽµÄÇé¿öÏ£¬²»ÄÜʹÓÃÕâÖÖģʽ¡£
Æß. ÎÈÍ×¹¹Ô캯Êýģʽ
ÎÈÍ×¹¹Ô캯ÊýģʽÓë¼ÄÉú¹¹Ô캯ÊýģʽÀàËÆ£¬µ«ÓÐÁ½µã²»Í¬£º
Ò»ÊÇд´½¨¶ÔÏóµÄʵÀý·½·¨²»ÒýÓÃthis£»
¶þÊDz»ÊÊÓÃnew²Ù×÷·ûµ÷Óù¹Ô캯Êý¡£
function Animal(name, age) {
var o = new Object();
o.name = name;
o.age = age;
var msg = "Hello, I'm ";
o.sayName = function() {
alert(msg + this.name);
}
return o;
}
var cat = new Animal("cat", 12);
cat.sayName(); // "Hello, I'm cat"
ÎÈÍ×¹¹Ô캯ÊýģʽÊʺÏÔÚijЩ°²È«Ö´Ðл·¾³¡£
ûÓÐÁË
JavaScriptÖеļ̳ÐÔÚÎÒÃÇÓ¦ÓÃÖÐÓõ½·Ç³£µÄ¶àÁËÔÚÕâÀïÎÒÃÇÒ»ÆðÀ´Á˽âÒ»ÏÂJavaScriptÖеļ̳еÄÈëÃÅ֪ʶ°É£¬¾ßÌåµÄ²½ÖèÈçÏ¡£
Ðí¶àOOPÓïÑÔ¶¼Ö§³ÖÁ½Öּ̳з½Ê½£º½Ó¿Ú¼Ì³ÐºÍʵÏּ̳С£½Ó¿Ú¼Ì³ÐÖ»¼Ì³Ð·½·¨Ç©Ãû£¬¶øʵÏּ̳ÐÔò¼Ì³Ðʵ¼ÊµÄ·½·¨¡£ÓÉÓÚº¯ÊýûÓÐÇ©Ãû£¬ÔÚECMAScriptÖÐÎÞ·¨ÊµÏֽӿڼ̳С£ECMAScriptÖ»Ö§³ÖʵÏּ̳У¬¶øÇÒÆäʵÏּ̳ÐÖ÷ÒªÊÇÒÀ¿¿ÔÐÍÁ´À´ÊµÏֵġ£
ʵÏÖ¶ÔÏ󼯳ɼ¯Öз½·¨ÈçÏ£º
Ò». ÔÐÍÁ´¼Ì³Ð
ʹÓÃÔÐÍÁ´¼¯³ÉµÄ»ù±¾Ë¼Ï룬ÊÇÀûÓÃÔÐÍÈÃÒ»¸öÒýÓÃÀàÐͼ¯³ÉÁíÒ»¸öÒýÓÃÀàÐ͵ÄÊôÐԺͷ½·¨¡£
±¾ÖʾÍÊÇÖØдÔÐͶÔÏ󣬴úÖ®ÒÔÒ»¸öÐÂÀàÐ͵ÄʵÀý¡£
function SuperType() {
this.property = true;
}
SuperType.prototype.getSuperValue = function() {
return this.property;
}
function SubType() {
this.subProperty = false;
}
// ¼Ì³ÐÁËSuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function() {
return this.subProperty;
}
¡¡