JavaScript implements object-oriented

Source: Internet
Author: User

1         /*2 JS implementation of Object-oriented method3             */4             5             //1 Factory model not recommended6             functionPerson (name, sex, age) {7obj = {};8Obj.name =name;9Obj.sex =sex;TenObj.age =Age ; OneObj.say =function(){ AAlert This. Name + "" + This. sex+ "" + This. age); -                     }; -                     returnobj; the                 } -                  -                 varP1 = Person ("Z3", "female", 18); - P1.say (); +              -              +              A              at             //2 recommended constructor Methods -             //the first letter to capitalize when creating a class this is a specification -             functionPerson (name, sex, age) { -                  This. Name =name; -                  This. Sex =sex; -                  This. Age =Age ; in                  This. Say =function(){ -Alert This. Name + "" + This. sex+ "" + This. age); to                 }; +             } -             varP2 =NewPerson ("Z3", "male", 18); the             //P2.say (); *             varP3 =NewPerson ("Z4", "female", 20); $             //P3.say ();Panax Notoginseng              -              the             //Obj.constructor is the constructor of obj +Alert (p2.constructor== person);//true A              the             //verifies that an instance is not an object of an instance instanceof +Alert (P2instanceofperson);//true -Alert (P2instanceofObject);//true JS in which everything is Object $              $              -              -              the             //3 Deprecated, binding methods use call or Apply -             functionPerson (name, sex, age) {Wuyi                  This. Name =name; the                  This. Sex =sex; -                  This. Age =Age ; Wu                  This. Say =function(){ -Alert This. Name + "" + This. sex+ "" + This. age); About                 }; $             } -             varn \NewObject (); -             //use call to bind an object to O to bind a Person object instance, which is the argument after O -Person.call (O, "Z3", "male", 19); A O.say (); +             

JavaScript implements object-oriented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.