AJax¼¼Êõ

javaScriptÖÐBlob¶ÔÏóµÄѧϰ±Ê¼Ç(2)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2016-10-06 18:00 ÎÒÒªÆÀÂÛ( )

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 A

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;
}

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • AJAXϵÁн̳Ì

    AJAXϵÁн̳Ì

    2016-09-30 12:00

  • javascript ѧϰ

    javascript ѧϰ

    2016-09-12 15:17

  • ѧϰjavascript:Å£È˵Ľ²×ùÊÓƵºÍPPT

    ѧϰjavascript:Å£È˵Ľ²×ùÊÓƵºÍPPT

    2016-08-22 13:00

  • ×îÈ«ÃæµÄJavaScriptµ÷ÊÔ¼¼ÇÉ×ܽá

    ×îÈ«ÃæµÄJavaScriptµ÷ÊÔ¼¼ÇÉ×ܽá

    2016-08-21 15:01

ÍøÓѵãÆÀ
á