JSÃæÏò¶ÔÏóµÄ³ÌÐòÉè¼Æ

µ¼Óï ÃæÏò¶ÔÏóµÄÓïÑÔÓÐÒ»¸ö±êÖ¾£¬¼´ÓµÓÐÀàµÄ¸ÅÄ³éÏóʵÀý¶ÔÏóµÄ¹«¹²ÊôÐÔÓë·½·¨£¬»ùÓÚÀà¿ÉÒÔ´´½¨ÈÎÒâ¶à¸öʵÀý¶ÔÏó£¬Ò»°ã¾ßÓзâ×°¡¢¼Ì³Ð¡¢¶à̬µÄÌØÐÔ£¡µ«JSÖжÔÏóÓë´¿ÃæÏò¶ÔÏóÓïÑÔÖеĶÔÏóÊDz»Í¬µÄ£¬ECMA±ê×¼¶¨ÒåJS

         ÃæÏò¶ÔÏóµÄÓïÑÔÓÐÒ»¸ö±êÖ¾£¬¼´ÓµÓÐÀàµÄ¸ÅÄ³éÏóʵÀý¶ÔÏóµÄ¹«¹²ÊôÐÔÓë·½·¨£¬»ùÓÚÀà¿ÉÒÔ´´½¨ÈÎÒâ¶à¸öʵÀý¶ÔÏó£¬Ò»°ã¾ßÓзâ×°¡¢¼Ì³Ð¡¢¶à̬µÄÌØÐÔ£¡µ«JSÖжÔÏóÓë´¿ÃæÏò¶ÔÏóÓïÑÔÖеĶÔÏóÊDz»Í¬µÄ£¬ECMA±ê×¼¶¨ÒåJSÖжÔÏó£ºÎÞÐòÊôÐԵļ¯ºÏ£¬ÆäÊôÐÔ¿ÉÒÔ°üº¬»ù±¾Öµ¡¢¶ÔÏó»òÕߺ¯Êý¡£¿ÉÒÔ¼òµ¥Àí½âΪJSµÄ¶ÔÏóÊÇÒ»×éÎÞÐòµÄÖµ£¬ÆäÖеÄÊôÐÔ»ò·½·¨¶¼ÓÐÒ»¸öÃû×Ö£¬¸ù¾ÝÕâ¸öÃû×Ö¿ÉÒÔ·ÃÎÊÏàÓ³ÉäµÄÖµ£¨Öµ¿ÉÒÔÊÇ»ù±¾Öµ/¶ÔÏó/·½·¨£©¡£

  Ò»¡¢Àí½â¶ÔÏó:

         µÚÒ»ÖÖ£º»ùÓÚObject¶ÔÏó

var person = new Object();
person.name = 'My Name';
person.age = 18;
person.getName = function(){
    return this.name;
}

        µÚ¶þÖÖ£º¶ÔÏó×ÖÃæÁ¿·½Ê½£¨±È½ÏÇå³þµÄ²éÕÒ¶ÔÏó°üº¬µÄÊôÐÔ¼°·½·¨£©

var person = {
    name : 'My name',
    age : 18,
    getName : function(){
        return this.name;
    }
}

      JSµÄ¶ÔÏó¿ÉÒÔʹÓÑ.’²Ù×÷·û¶¯Ì¬µÄÀ©Õ¹ÆäÊôÐÔ£¬¿ÉÒÔʹÓÃ’delete’²Ù×÷·û»ò½«ÊôÐÔÖµÉèÖÃΪ’undefined’À´É¾³ýÊôÐÔ¡£ÈçÏ£º

person.newAtt=’new Attr’;//Ìí¼ÓÊôÐÔ
alert(person.newAtt);//new Attr
delete person.age;
alert(person.age);//undefined(ɾ³ýÊôÐÔºóֵΪundefined);

¶þ¡¢¶ÔÏóÊôÐÔÀàÐÍ

      ECMA-262µÚ5°æ¶¨ÒåÁËJS¶ÔÏóÊôÐÔÖÐÌØÕ÷£¨ÓÃÓÚJSÒýÇ棬ÍⲿÎÞ·¨Ö±½Ó·ÃÎÊ£©¡£ECMAScriptÖÐÓÐÁ½ÖÖÊôÐÔ£ºÊý¾ÝÊôÐԺͷÃÎÊÆ÷ÊôÐÔ

      1¡¢Êý¾ÝÊôÐÔ£º

      Êý¾ÝÊôÐÔÖ¸°üº¬Ò»¸öÊý¾ÝÖµµÄλÖ㬿ÉÔÚ¸ÃλÖöÁÈ¡»òдÈëÖµ£¬¸ÃÊôÐÔÓÐ4¸ö¹©ÊöÆäÐÐΪµÄÌØÐÔ£º

      [[configurable]]:±íʾÄÜ·ñʹÓÃdelete²Ù×÷·ûɾ³ý´Ó¶øÖØж¨Ò壬»òÄÜ·ñÐÞ¸ÄΪ·ÃÎÊÆ÷ÊôÐÔ¡£Ä¬ÈÏΪtrue;

      [[Enumberable]]:±íʾÊÇ·ñ¿Éͨ¹ýfor-inÑ­»··µ»ØÊôÐÔ¡£Ä¬ÈÏtrue;

      [[Writable]]:±íʾÊÇ·ñ¿ÉÐÞ¸ÄÊôÐÔµÄÖµ¡£Ä¬ÈÏtrue;

      [[Value]]:°üº¬¸ÃÊôÐÔµÄÊý¾ÝÖµ¡£¶ÁÈ¡/дÈ붼ÊǸÃÖµ¡£Ä¬ÈÏΪundefined;ÈçÉÏÃæʵÀý¶ÔÏópersonÖж¨ÒåÁËnameÊôÐÔ£¬ÆäֵΪ’My name’,¶Ô¸ÃÖµµÄÐ޸Ķ¼·´ÕýÔÚÕâ¸öλÖÃ

      ÒªÐ޸ĶÔÏóÊôÐÔµÄĬÈÏÌØÕ÷£¨Ä¬È϶¼Îªtrue)£¬¿Éµ÷ÓÃObject.defineProperty()·½·¨£¬Ëü½ÓÊÕÈý¸ö²ÎÊý£ºÊôÐÔËùÔÚ¶ÔÏó£¬ÊôÐÔÃûºÍÒ»¸öÃèÊö·û¶ÔÏ󣨱ØÐëÊÇ£ºconfigurable¡¢enumberable¡¢writableºÍvalue£¬¿ÉÉèÖÃÒ»¸ö»ò¶à¸öÖµ£©¡£

      ÈçÏ£º£¨ä¯ÀÀÆ÷Ö§³Ö£ºIE9+¡¢Firefox 4+¡¢Chrome¡¢Safari5+£©

var person = {};
Object.defineProperty(person, 'name', {
    configurable: false,
    writable: false,
    value: 'Jack'
});
alert(person.name);//Jack
delete person.name;
person.name = 'lily';
alert(person.name);//Jack

      ¿ÉÒÔ¿´³ö£¬delete¼°ÖØÖÃperson.nameµÄÖµ¶¼Ã»ÓÐÉúЧ£¬Õâ¾ÍÊÇÒòΪµ÷ÓÃdefinePropertyº¯ÊýÐÞ¸ÄÁ˶ÔÏóÊôÐÔµÄÌØÕ÷£»ÖµµÃ×¢ÒâµÄÊÇÒ»µ©½«configurableÉèÖÃΪfalse£¬ÔòÎÞ·¨ÔÙʹÓÃdefineProperty½«ÆäÐÞ¸ÄΪtrue£¨Ö´Ðлᱨ´í£ºcan't redefine non-configurable property£©;

      2¡¢·ÃÎÊÆ÷ÊôÐÔ£º

      ËüÖ÷Òª°üÀ¨Ò»¶ÔgetterºÍsetterº¯Êý£¬ÔÚ¶ÁÈ¡·ÃÎÊÆ÷ÊôÐÔʱ£¬»áµ÷ÓÃgetter·µ»ØÓÐЧֵ£»Ð´Èë·ÃÎÊÆ÷ÊôÐÔʱ£¬µ÷ÓÃsetter£¬Ð´ÈëÐÂÖµ£»¸ÃÊôÐÔÓÐÒÔÏÂ4¸öÌØÕ÷£º

      [[Configurable]]:ÊÇ·ñ¿Éͨ¹ýdelete²Ù×÷·ûɾ³ýÖØж¨ÒåÊôÐÔ£»

      [[Numberable]]:ÊÇ·ñ¿Éͨ¹ýfor-inÑ­»·²éÕÒ¸ÃÊôÐÔ£»

      [[Get]]:¶ÁÈ¡ÊôÐÔʱµ÷Óã¬Ä¬ÈÏ£ºundefined;

      [[Set]]:дÈëÊôÐÔʱµ÷Óã¬Ä¬ÈÏ£ºundefined;

      ·ÃÎÊÆ÷ÊôÐÔ²»ÄÜÖ±½Ó¶¨Ò壬±ØÐëʹÓÃdefineProperty()À´¶¨Ò壬ÈçÏ£º

var person = {
    _age: 18
};
Object.defineProperty(person, 'isAdult', {
    get: function () {
        if (this._age >= 18) {
            return true;
        } else {
            return false;
        }
    }
});
alert(person.isAdult?'³ÉÄê':'δ³ÉÄê');//³ÉÄê

      ´ÓÉÏÃæ¿ÉÖª£¬¶¨Òå·ÃÎÊÆ÷ÊôÐÔʱgetterÓësetterº¯Êý²»ÊDZØÐëµÄ,²¢ÇÒ£¬ÔÚ¶¨ÒågetterÓësetterʱ²»ÄÜÖ¸¶¨ÊôÐÔµÄconfigurable¼°writableÌØÐÔ£»

      ´ËÍ⣬ECMA-262£¨5£©»¹ÌṩÁËÒ»¸öObject.defineProperties()·½·¨£¬¿ÉÒÔÓÃÀ´Ò»´ÎÐÔ¶¨Òå¶à¸öÊôÐÔµÄÌØÐÔ£º

var person = {};
Object.defineProperties(person,{
    _age:{
        value:19
    },
    isAdult:{
        get: function () {
            if (this._age >= 18) {
                return true;
            } else {
                return false;
            }
        }
    }
});
alert(person.isAdult?'³ÉÄê':'δ³ÉÄê');//³ÉÄê

 

      ÉÏÊö´úÂëʹÓÃObject.defineProperties()·½·¨Í¬Ê±¶¨ÒåÁË_age¼°isAudltÁ½¸öÊôÐÔµÄÌØÐÔ

      ´ËÍ⣬ʹÓÃObject.getOwnPropertyDescriptor()·½·¨¿ÉÒÔÈ¡µÃ¸ø¶¨ÊôÐÔµÄÌØÐÔ£º

var descriptor = Object.getOwnPropertyDescriptor(person,'_age');
alert(descriptor.value);//19

   ¶ÔÓÚÊý¾ÝÊôÐÔ£¬¿ÉÒÔÈ¡µÃ£ºconfigurable,enumberable,writableºÍvalue£»

   ¶ÔÓÚ·ÃÎÊÆ÷ÊôÐÔ£¬¿ÉÒÔÈ¡µÃ£ºconfigurable,enumberable,getºÍset

Èý¡¢´´½¨¶ÔÏó

ʹÓÃObject¹¹Ô캯Êý»ò¶ÔÏó×ÖÃæÁ¿¶¼¿ÉÒÔ´´½¨¶ÔÏ󣬵«È±µãÊÇ´´½¨¶à¸ö¶ÔÏóʱ£¬»á²úÉú´óÁ¿µÄÖظ´´úÂ룬Òò´ËÏÂÃæ½éÉܿɽâ¾öÕâ¸öÎÊÌâµÄ´´½¨¶ÔÏóµÄ·½·¨

     1¡¢¹¤³§Ä£Ê½

function createPerson(name, age, job) {
    var o = new Object();
    o.name = name;
    o.age = age;
    o.job = job;
    o.getName = function () {
        return this.name;
    }
    return o;//ʹÓÃreturn·µ»ØÉú³ÉµÄ¶ÔÏóʵÀý
}
var person = createPerson('Jack', 19, 'SoftWare Engineer');

     ´´½¨¶ÔÏ󽻸øÒ»¸ö¹¤³§·½·¨À´ÊµÏÖ£¬¿ÉÒÔ´«µÝ²ÎÊý£¬µ«Ö÷ҪȱµãÊÇÎÞ·¨Ê¶±ð¶ÔÏóÀàÐÍ£¬ÒòΪ´´½¨¶ÔÏó¶¼ÊÇʹÓÃObjectµÄÔ­Éú¹¹Ô캯ÊýÀ´Íê³ÉµÄ¡£

     2¡¢¹¹Ô캯Êýģʽ

function Person(name,age,job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.getName = function () {
        return this.name;
    }
}

	

var person1 = new Person('Jack', 19, 'SoftWare Engineer');

var person2 = new Person('Liye', 23, 'Mechanical Engineer');

 

     ʹÓÃ×Ô¶¨ÒåµÄ¹¹Ô캯Êý£¨ÓëÆÕͨº¯ÊýÒ»Ñù£¬Ö»ÊÇÓÃËüÀ´´´½¨¶ÔÏ󣩣¬¶¨Òå¶ÔÏóÀàÐÍ£¨È磺Person£©µÄÊôÐԺͷ½·¨¡£ËüÓ빤³§·½·¨Çø±ðÔÚÓÚ£º

  • ûÓÐÏÔʽµØ´´½¨¶ÔÏó
  • Ö±½Ó½«ÊôÐԺͷ½·¨¸³Öµ¸øthis¶ÔÏó£»
  • ûÓÐreturnÓï¾ä£»

      ´ËÍ⣬Ҫ´´½¨PersonµÄʵÀý£¬±ØÐëʹÓÃnew¹Ø¼ü×Ö£¬ÒÔPersonº¯ÊýΪ¹¹Ô캯Êý£¬´«µÝ²ÎÊýÍê³É¶ÔÏó´´½¨£»Êµ¼Ê´´½¨¾­¹ýÒÔÏÂ4¸ö¹ý³Ì£º

  1. ´´½¨Ò»¸ö¶ÔÏó
  2. ½«º¯ÊýµÄ×÷ÓÃÓò¸³¸øжÔÏó£¨Òò´ËthisÖ¸ÏòÕâ¸öжÔÏó£¬È磺person1£©
  3. Ö´Ðй¹Ô캯ÊýµÄ´úÂë
  4. ·µ»Ø¸Ã¶ÔÏó

     ÉÏÊöÓÉPerson¹¹Ô캯ÊýÉú³ÉµÄÁ½¸ö¶ÔÏóperson1Óëperson2¶¼ÊÇPersonµÄʵÀý£¬Òò´Ë¿ÉÒÔʹÓÃinstanceofÅжϣ¬²¢ÇÒÒòΪËùÓжÔÏ󶼼̳ÐObject£¬Òò´Ëperson1 instanceof ObjectÒ²·µ»ØÕ棺

alert(person1 instanceof Person);//true;
alert(person2 instanceof Person);//true;
alert(person1 instanceof Object);//true;
alert(person1.constructor === person2.constructor);//ture;

     ËäÈ»¹¹Ô캯Êý·½Ê½±È½Ï²»´í£¬µ«Ò²´æÔÚȱµã£¬ÄǾÍÊÇÔÚ´´½¨¶ÔÏóʱ£¬ÌرðÕë¶Ô¶ÔÏóµÄÊôÐÔÖ¸Ïòº¯Êýʱ£¬»áÖظ´µÄ´´½¨º¯ÊýʵÀý£¬ÒÔÉÏÊö´úÂëΪ»ù´¡£¬¿ÉÒÔ¸ÄдΪ£º

function Person(name,age,job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.getName = new Function () {//¸ÄдºóЧ¹ûÓëÔ­´úÂëÏàͬ£¬²»¹ýÊÇΪÁË·½±ãÀí½â
        return this.name;
    }
}

ÉÏÊö´úÂ룬´´½¨¶à¸öʵÀýʱ£¬»áÖظ´µ÷ÓÃnew Function();´´½¨¶à¸öº¯ÊýʵÀý£¬ÕâЩº¯ÊýʵÀý»¹²»ÊÇÒ»¸ö×÷ÓÃÓòÖУ¬µ±È»ÕâÒ»°ã²»»áÓÐ´í£¬µ«Õâ»áÔì³ÉÄÚ´æÀË·Ñ¡£µ±È»£¬¿ÉÒÔÔÚº¯ÊýÖж¨ÒåÒ»¸ögetName = getNameµÄÒýÓ㬶øgetNameº¯ÊýÔÚPersonÍⶨÒ壬ÕâÑù¿ÉÒÔ½â¾öÖظ´´´½¨º¯ÊýʵÀýÎÊÌ⣬µ«ÔÚЧ¹ûÉϲ¢Ã»ÓÐÆðµ½·â×°µÄЧ¹û£¬ÈçÏÂËùʾ£º

function Person(name,age,job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.getName = getName;
}
function getName() {//µ½´¦ÊÇ´úÂ룬¿´×ÅÂÒ£¡£¡
        return this.name;
}

     3¡¢Ô­ÐÍģʽ

     JSÿ¸öº¯Êý¶¼ÓÐÒ»¸öprototype(Ô­ÐÍ)ÊôÐÔ£¬Õâ¸öÊôÐÔÊÇÒ»¸öÖ¸Õ룬ָÏòÒ»¸ö¶ÔÏó£¬ËüÊÇËùÓÐͨ¹ýnew²Ù×÷·ûʹÓú¯Êý´´½¨µÄʵÀýµÄÔ­ÐͶÔÏó¡£Ô­ÐͶÔÏó×î´óÌصãÊÇ£¬ËùÓжÔÏóʵÀý¹²ÏíËüËù°üº¬µÄÊôÐԺͷ½·¨£¬Ò²¾ÍÊÇ˵£¬ËùÓÐÔÚÔ­ÐͶÔÏóÖд´½¨µÄÊôÐÔ»ò·½·¨¶¼Ö±½Ó±»ËùÓжÔÏóʵÀý¹²Ïí¡£

function Person(){
}
Person.prototype.name = 'Jack';//ʹÓÃÔ­ÐÍÀ´Ìí¼ÓÊôÐÔ
Person.prototype.age = 29;
Person.prototype.getName = function(){
    return this.name;
}
var person1 = new Person();
alert(person1.getName());//Jack
var person2 = new Person();
alert(person1.getName === person2.getName);//true;¹²ÏíÒ»¸öÔ­ÐͶÔÏóµÄ·½·¨

      Ô­ÐÍÊÇÖ¸ÏòÔ­ÐͶÔÏóµÄ£¬Õâ¸öÔ­ÐͶÔÏóÓë¹¹Ô캯ÊýûÓÐÌ«´ó¹Øϵ£¬Î¨Ò»µÄ¹ØϵÊǺ¯ÊýµÄprototypeÊÇÖ¸ÏòÕâ¸öÔ­ÐͶÔÏ󣡶ø»ùÓÚ¹¹Ô캯Êý´´½¨µÄ¶ÔÏóʵÀýÒ²°üº¬Ò»¸öÄÚ²¿Ö¸ÕëΪ£º[[prototype]]Ö¸ÏòÔ­ÐͶÔÏó¡£

       ʵÀýÊôÐÔ»ò·½·¨µÄ·ÃÎʹý³ÌÊÇÒ»´ÎËÑË÷¹ý³Ì£º

  • Ê×ÏÈ´Ó¶ÔÏóʵÀý±¾Éí¿ªÊ¼£¬Èç¹ûÕÒµ½ÊôÐÔ¾ÍÖ±½Ó·µ»Ø¸ÃÊôÐÔÖµ£»
  • Èç¹ûʵÀý±¾Éí²»´æÔÚÒª²éÕÒÊôÐÔ£¬¾Í¼ÌÐøËÑË÷Ö¸ÕëÖ¸ÏòµÄÔ­ÐͶÔÏó£¬ÔÚÆäÖвéÕÒ¸ø¶¨Ãû×ÖµÄÊôÐÔ£¬Èç¹ûÓоͷµ»Ø£»

     »ùÓÚÒÔÉÏ·ÖÎö£¬Ô­ÐÍģʽ´´½¨µÄ¶ÔÏóʵÀý£¬ÆäÊôÐÔÊǹ²ÏíÔ­ÐͶÔÏóµÄ£»µ«Ò²¿ÉÒÔ×Ô¼ºÊµÀýÖÐÔÙ½øÐж¨Ò壬ÔÚ²éÕÒʱ£¬¾Í²»´ÓÔ­ÐͶÔÏó»ñÈ¡£¬¶øÊǸù¾ÝËÑË÷Ô­Ôò£¬µÃµ½±¾ÊµÀýµÄ·µ»Ø£»¼òµ¥À´Ëµ£¬¾ÍÊÇʵÀýÖÐÊôÐÔ»áÆÁ±ÎÔ­ÐͶÔÏóÖеÄÊôÐÔ£»

     Ô­ÐÍÓëin²Ù×÷·û

     Ò»¾ä»°£ºÎÞÂÛÔ­ÐÍÖÐÊôÐÔ£¬»¹ÊǶÔÏóʵÀýµÄÊôÐÔ£¬¶¼¿ÉÒÔʹÓÃin²Ù×÷·û·ÃÎʵ½£»ÒªÏëÅжÏÊÇ·ñÊÇʵÀý±¾ÉíµÄÊôÐÔ¿ÉÒÔʹÓÃobject.hasOwnProperty(‘attr’)À´Åжϣ»

     Ô­Éú¶ÔÏóÖÐÔ­ÐÍ

     Ô­Éú¶ÔÏóÖÐÔ­ÐÍÓëÆÕͨ¶ÔÏóµÄÔ­ÐÍÒ»Ñù£¬¿ÉÒÔÌí¼Ó/ÐÞ¸ÄÊôÐÔ»ò·½·¨£¬ÈçÒÔÏ´úÂëΪËùÓÐ×Ö·û´®¶ÔÏóÌí¼ÓÈ¥×óÓÒ¿Õ°×Ô­ÐÍ·½·¨£º

String.prototype.trim = function(){
    return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
var str = '   word space   ';
alert('!'+str.trim()+'!');//!word space!

     Ô­ÐÍģʽµÄȱµã£¬ËüÊ¡ÂÔÁËΪ¹¹Ô캯Êý´«µÝ³õʼ»¯²ÎÊý£¬ÕâÔÚÒ»¶¨³ÌÐò´øÀ´²»±ã£»ÁíÍ⣬×îÖ÷ÒªÊǵ±¶ÔÏóµÄÊôÐÔÊÇÒýÓÃÀàÐÍʱ£¬ËüµÄÖµÊDz»±äµÄ£¬×ÜÊÇÒýÓÃͬһ¸öÍⲿ¶ÔÏó£¬ËùÓÐʵÀý¶Ô¸Ã¶ÔÏóµÄ²Ù×÷¶¼»áÆäËüʵÀý£º

function Person() {
}
Person.prototype.name = 'Jack';
Person.prototype.lessons = ['Math','Physics'];
var person1 = new Person();
person1.lessons.push('Biology');
var person2 = new Person();
alert(person2.lessons);//Math,Physics,Biology£¬person1ÐÞ¸ÄÓ°ÏìÁËperson2

     4¡¢×éºÏ¹¹Ô캯Êý¼°Ô­ÐÍģʽ

      Ä¿Ç°×îΪ³£ÓõĶ¨ÒåÀàÐÍ·½Ê½£¬ÊÇ×éºÏ¹¹Ô캯ÊýģʽÓëÔ­ÐÍģʽ¡£¹¹Ô캯ÊýģʽÓÃÓÚ¶¨ÒåʵÀýµÄÊôÐÔ£¬¶øÔ­ÐÍģʽÓÃÓÚ¶¨Òå·½·¨ºÍ¹²ÏíµÄÊôÐÔ¡£½á¹û£¬Ã¿¸öʵÀý¶¼»áÓÐ×Ô¼ºµÄÒ»·ÝʵÀýÊôÐԵĸ±±¾£¬µ«Í¬Ê±ÓÖ¹²Ïí×ŶԷ½·½·¨µÄÒýÓã¬×î´óÏ޶ȵĽÚÔ¼ÄÚ´æ¡£´ËÍ⣬×éºÏģʽ»¹Ö§³ÖÏò¹¹Ô캯Êý´«µÝ²ÎÊý£¬¿ÉνÊǼ¯Á½¼ÒÖ®Ëù³¤¡£

function Person(name, age, job) {
    this.name = name;
    this.age = age;
    this.job = job;
    this.lessons = ['Math', 'Physics'];
}
Person.prototype = {
    constructor: Person,//Ô­ÐÍ×ÖÃæÁ¿·½Ê½»á½«¶ÔÏóµÄconstructor±äΪObject£¬
//´ËÍâÇ¿ÖÆÖ¸»ØPerson
    getName: function () {
        return this.name;
    }
}
var person1 = new Person('Jack', 19, 'SoftWare Engneer');
person1.lessons.push('Biology');
var person2 = new Person('Lily', 39, 'Mechanical Engneer');
alert(person1.lessons);//Math,Physics,Biology
alert(person2.lessons);//Math,Physics
alert(person1.getName === person2.getName);//true,//¹²ÏíÔ­ÐÍÖж¨Òå·½·¨

 

   ÔÚËù½Ó´¥µÄJS¿âÖУ¬jQueryÀàÐ͵ķâ×°¾ÍÊÇʹÓÃ×éºÏģʽÀ´ÊµÀýµÄ£¡£¡£¡

 
    5¡¢¶¯Ì¬Ô­ÐÍģʽ

     ×éºÏģʽÖÐʵÀýÊôÐÔÓë¹²Ïí·½·¨£¨ÓÉÔ­ÐͶ¨Ò壩ÊÇ·ÖÀëµÄ£¬ÕâÓë´¿ÃæÏò¶ÔÏóÓïÑÔ²»Ì«Ò»Ö£»¶¯Ì¬Ô­ÐÍģʽ½«ËùÓй¹ÔìÐÅÏ¢¶¼·â×°ÔÚ¹¹Ô캯ÊýÖУ¬ÓÖ±£³ÖÁË×éºÏµÄÓŵ㡣ÆäÔ­Àí¾ÍÊÇͨ¹ýÅжϹ¹Ô캯ÊýµÄÔ­ÐÍÖÐÊÇ·ñÒѾ­¶¨ÒåÁ˹²ÏíµÄ·½·¨»òÊôÐÔ£¬Èç¹ûûÓÐÔò¶¨Ò壬·ñÔò²»ÔÙÖ´Ðж¨Òå¹ý³Ì¡£¸Ã·½Ê½Ö»Ô­ÐÍÉÏ·½·¨»òÊôÐÔÖ»¶¨ÒåÒ»´Î£¬ÇÒ½«ËùÓй¹Ôì¹ý³Ì¶¼·â×°ÔÚ¹¹Ô캯ÊýÖУ¬¶ÔÔ­ÐÍËù×öµÄÐÞ¸ÄÄÜÁ¢¼´ÌåÏÖËùÓÐʵÀýÖУº

function Person(name, age, job) {
    this.name = name;
    this.age = age;
    this.job = job;
    this.lessons = ['Math', 'Physics'];
    if (typeof this.getName != 'function') {//ͨ¹ýÅжÏʵÀý·â×°
        Person.prototype = {
            constructor: Person,//Ô­ÐÍ×ÖÃæÁ¿·½Ê½»á½«¶ÔÏóµÄconstructor±äΪObject£¬
//´ËÍâÇ¿ÖÆÖ¸»ØPerson
            getName: function () {
                return this.name;
            }
        }
    }
}
 
var person1 = new Person('Jack', 19, 'SoftWare Engneer');
person1.lessons.push('Biology');
var person2 = new Person('Lily', 39, 'Mechanical Engneer');
alert(person1.lessons);//Math,Physics,Biology
alert(person2.lessons);//Math,Physics
alert(person1.getName === person2.getName);//true,//¹²ÏíÔ­ÐÍÖж¨Òå·½·¨


http://www.aseoe.com/ true JSÃæÏò¶ÔÏóµÄ³ÌÐòÉè¼Æ http://www.aseoe.com/show-12-90-1.html report <£¿php echo strlen($content) / 2; ?> ÃæÏò¶ÔÏóµÄÓïÑÔÓÐÒ»¸ö±êÖ¾£¬¼´ÓµÓÐÀàµÄ¸ÅÄ³éÏóʵÀý¶ÔÏóµÄ¹«¹²ÊôÐÔÓë·½·¨£¬»ùÓÚÀà¿ÉÒÔ´´½¨ÈÎÒâ¶à¸öʵÀý¶ÔÏó£¬Ò»°ã¾ßÓзâ×°¡¢¼Ì³Ð¡¢¶à̬µÄÌØÐÔ£¡µ«JSÖжÔÏóÓë´¿ÃæÏò¶ÔÏóÓïÑÔÖеĶÔÏóÊDz»Í¬µÄ£¬ECMA±ê×¼¶¨ÒåJS
TAG:JS ÃæÏò¶ÔÏó
±¾Õ¾»¶Ó­ÈκÎÐÎʽµÄתÔØ£¬µ«ÇëÎñ±Ø×¢Ã÷³ö´¦£¬×ðÖØËûÈËÀͶ¯³É¹û
תÔØÇë×¢Ã÷£º ÎÄÕÂתÔØ×Ô£º°®Ë¼×ÊÔ´Íø http://www.aseoe.com/show-12-90-1.html

[Ç°¶Ë²å¼þÍƼö] Plugin

1 2 3 4
  • jQueryʵÏÖÖð×ÖÖð¾äÏÔʾ²å¼þl-by-l.min.js
  • jQuery´ø·½Ïò¸ÐÖªµÄÊó±ê»¬¹ýͼƬ±ß¿òÌØЧ²å¼þ
  • jQuery HotKeys¼àÌý¼üÅÌ°´ÏÂʼþkeydown²å¼þ
  • ÏìӦʽÎÞÏÞÂÖ²¥jQueryÐýתľÂí²å¼þ
ÏìӦʽÎÞÏÞÂÖ²¥jQueryÐýתľÂí²å¼þ
webÇ°¶Ë¿ª·¢
°®Ë¼×ÊÔ´Íø Copyright 2012-2014 Www.Aseoe.Com All rights reserved.(½úICP±¸13001436ºÅ-1)