2016.8.07 This, new, schema factory, creating a new constructor

Source: Internet
Author: User

Written in front: Today I will publish the functional basis, object Foundation, this, new, encapsulation, encapsulation examples of these personal understanding of the article. It is best to read it in one breath and write it yourself, believing that it will help friends who want to understand functions and objects.

Table of Contents: Schema factory, this, new, creating a fresh constructor

Pattern Factory: Put objects into functions, bulk create objects

Code:

        functionStudent (name,age,sex) {varo=NewObject ();//define an object first,O.name=name; O.age=Age ; O.sex=sex; O.sayhi=function() {Console.log ("My Name" +o.name); }            returnO//returns an object with Name,age,sex,sayhi in it.        }        //using constructors to define LS        varls=NewStudent ("John Doe", 25, "male"); varLs2=NewStudent ("Harry", 22, "female"); Ls.sayhi ();

This: Who calls, refers to who, does not gave.
new:new keyword to complete
1. Create an empty object
2. Point the keyword this to this empty object
3, execute the code inside the constructor, give the current null object this set properties and methods
4. Return this current object

Create a new constructor (that is, encapsulation) code:

        functionStudent (name,age,sex) {varo=NewObject ();//define an object first,O.name=name; O.age=Age ; O.sex=sex; O.sayhi=function() {Console.log ("My Name" +o.name); }            returnO//returns an object with Name,age,sex,sayhi in it.        }        //using constructors to define LS        varls=NewStudent ("John Doe", 25, "male"); varLs2=NewStudent ("Harry", 22, "female"); Ls.sayhi ();

2016.8.07 This, new, schema factory, creating a new constructor

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.