Design patterns (a) object-oriented

First look at several functions:

function checkName () {) {} // name verification 
 function checkEmail () {} // verify mailbox 
 function checkPassword () {} // authentication password

This is equivalent to creating three global variables, then there is a problem, do not function variables? Then we solve this problem.

First, the function of another form : see below the three functions.

var checkName = function () {) {} // verify name 
var checkEmail = function () {} // verify mailbox 
var checkPassword = function () {} // authentication password

Compare the top two way, in fact, it is the same. . . . .

But such a view, you would find creating a function saves in three years to implement variable function.

However, the problem again. What's the problem?

First of all, from the functional point, there is no problem.

However, when the code is written in team development can not only consider himself. Also taking into account other people. If others are defined the same way, it will overwrite the original, if you define a lot of ways, then this problem covering each other is not easy to detect.

So, the question again. How to do it?

So is this: incorporate variables object.

You can create a test object, and then put inside the method:

Look at the code below:

var CheckObject = { 
    CheckName: function () {) {} // verify Name 
    The checkEmail: function () {} // verify mail 
    the checkPassword: function () {} // authentication password 
}

So, when the call is CheckObject.checkName () method to call the object inside. That is, the Warriors turned out to be functional in front of one object name. Well, since the method can be used by dot syntax, so it is not able to create it

Yes, sure. Another form of object

var CheckObject = function () {}; 
CheckObject.checkName = function () {} // verify name 
CheckObject.checkEmail = function () {} // verify mailbox 
CheckObject.checkPassword = function () {} // authentication password

Use and front are the same, CheckObject.checkName (), but others want to use when you write object methods, some trouble, because the object can not be a copy of, or the object class in creating a new object with the new keyword , the newly created object are not inherited these methods. Next introduce a genuine objects

 var CheckObject = function () {
     return { 
     CheckName: function () {}, // verify Name 
     The checkEmail: function () {}, // verify mail 
     the checkPassword: function () {} // authentication password   
}       
}

That call: var a = CheckObject (); a.checkEmail (). When someone wants to call this function returns a new object, so the process is CheckObject clear surface on the object, the new object is actually returned.

Although complete the requirements by creating a new object, but he was not really a way to create class and objects created and a CheckObject nothing to do, so little transformation about.

var CheckObject = function () {
  this.checkName = function () {
       
  } ,
  ...     
}

This method calls This is a class, then it is to use the new keyword var a = new CheckObject (); a.checkName ().

Now that we have put all the methods are on the internal functions. So by this definition of each new keyword is defined by the time the new objects are created on the properties of this class of replicate. So these newly created objects will have their own set of methods, but sometimes this is very extravagant. We need to deal with it.

var CheckObject = function () {}; 
CheckObject.prototype.checkName = function () {}; // verify name 

...

The benefits of this creation is the method of objects created are owned by just a, and must rely on a prototype prototypes look, they are bound to the prototype CheckObject object class ,, but we can continue to optimize it

var CheckObject = function () {}; 
CheckObject.prototype = { 
    CheckName: function () {}, // verify name 

    
    ...   
}

Then use the class: var a = new CheckObject (); a.checkName ();

However, the method according to the top of the call CheckObject object. Call every time (a. Method name) is not feeling a lot of trouble.

So another way

var CheckObject = function  () {};
CheckObject.prototype = {
   checkName:function () {return this;}, //验证姓名
checkEmail:function () {return this;}
}

Then the call is var a = new CheckObject ();.. A.checkName () checkEmail () is not very easy.

 

Guess you like

Origin www.cnblogs.com/xueyelone/p/11939145.html