Method 5 for JavaScript Creation

Source: Internet
Author: User

Common JS creation objects: <br/> var OBJ = new object; </P> <p> obj. name = "Goul"; </P> <p> obj. age = 22; </P> <p> obj. sex = "male"; </P> <p> obj. say = function () {</P> <p> alert ("My name is" + this. name) </P> <p >}; </P> <p> the following uses the <SPAN class = 'wp _ keywordlink '> Program </span> as an example: </P> <p> 1. factory method: </P> <p> function say () {alert ("My name is" + this. name)} // defines the object method outside the factory function, and points to this method through the property </P> <p> function createobj (name, age, sex) {// factory function, three parameters are accepted </P> <p> varobj = new object; </P> <p> obj. name = Name; </P> <p> obj. age = age; </P> <p> obj. sex = sex; </P> <p> obj. say = say; // method pointing out of the factory function </P> <p> return OBJ; // return object </P> <p >}</P> <p> var obj1 = createobj ("Goul", 22, "male "); // create an instance of the class </P> <p> 2. constructor method: </P> <p> function OBJ (name, age, sex) {// constructor </P> <p> This. name = Name; </P> <p> This. age = age; </P> <p> This. sex = sex; </P> <p> This. say = function () {</P> <p> alert ("My name is" + this. name) </P> <p >}</P> <p> var obj1 = new OBJ ("Goul", 22, "male "); // create an instance of the class </P> <p> 3. prototype: </P> <p> function OBJ () {}// define an empty function </P> <p> obj. prototype. name = "Goul"; </P> <p> obj. prototype. age = 22; </P> <p> obj. prototype. sex = "male"; </P> <p> obj. say = function () {</P> <p> alert ("My name is" + this. name) </P> <p >}</P> <p> var obj1 = new OBJ (); // create a class instance </P> <p> 4, constructor: </P> <p> function OBJ (name, age, sex) {// constructor </P> <p> This. name = Name; </P> <p> This. age = age; </P> <p> This. sex = sex; </P> <p >}</P> <p> obj. prototype. say = function () {// method defined in the prototype </P> <p> alert ("My name is" + this. name); </P> <p >}</P> <p> var obj1 = new OBJ ("Goul", 22, "male "); // create an instance of the class </P> <p> 5. Dynamic Prototype Method: </P> <p> function OBJ (name, age, sex) {</P> <p> This. name = Name; </P> <p> This. age = age; </P> <p> This. sex = sex; </P> <p> If (typeof obj. _ initalized = "undefines") {// _ initalize the prefix underline is the private variable that the programmer has achieved </P> <p> obj. prototype. say = function () {</P> <p> alert ("My name is" + this. name) </P> <p >}</P> <p> obj. _ initalized = true; </P> <p >}</P> <p>

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.