JavaScript Creation Object--constructor mode

Source: Internet
Author: User

1 functionPerson (name, age) {2      This. Name =name;3      This. Age =Age ;4      This. Eat =function() {5Alert ("Eating ...");6     }7 }8 varP1 =NewPerson ("cai10", 21);9 varP2 =NewPerson ("cai20", 27);Tenalert (;//cai10 Onealert (p1.age);// + ();//Eating ... -Alerttypeofp1);//Object -Alert (P1instanceofObject);//true theAlert (P1instanceofperson);//true -alert (p1.constructor);//function person (name, age) {...} -Alert ( = =;//false

Creates and returns a new object by using the New keyword.
In person (), no created objects are displayed,
Instead, the properties and methods are assigned directly to the this object, and there is no return statement.
You create an instance by using the constructor pattern, which identifies the instance as a specific type,
In the example, the instanceof operator or the constructor property can be used for validation.
However, there is still a problem with multiple function instances.

JavaScript Creation Object--constructor mode

Related Article

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: 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.