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