Read "JS is design mode" first chapter Experience

Source: Internet
Author: User

1, understand their

Understand why the code you write is difficult to understand and bloated, inconvenient to read and inefficient. The main reason is why the whole process can only be written by me, because such code does not have the team force, the coordination ability is poor. The understanding of JS but thorough.

2. The difference between the real learning object and the class

Class: A description of a collection of objects with the same characteristics;
Object: An individual of a real existence;

Human beings, for example, refer to a range; an object: A person, for example, refers to a specific object in this range.

When a function in JavaScript is a constructor, it is a class that, with the new operator, can return an object.
Of course, to generate an object, you can also use the literal form, such as var obj = {x:1, y:function () {}};
A class can be understood as a template, and an object is a concrete instance created from this template.

3. Adding a unified method to the function's prototype

In general, function functions are not encapsulated in prototypes because they pollute the whole world and cause unnecessary overhead. So I'm going to abstract a unified method of adding functionality.

For example:

Function.prototype.addmethod=function (NAME,FN) {

THIS[NAME]=FN;

}

If you want to add a mailbox verification and name verification method, you can do this

var methods=function () {};

Or

var methods = new Function ();

Methods.addmethod (' CheckName ', function () {

Verify Name

});

Methods.addmethod (' Checkemail ', function () {

Verify Mailbox

});

Methods.checkname ();

Methods.checkemail ();

4, Function chain-type method of adding

Adding a function in the prototype adds a return this statement so that the methods function is returned each time a method is added.

Function.prototype. Addmethod= function (NAME,FN) {

THIS[NAME]=FN;

return this;

}

You can also return this for each method that you add.

var methods = function () {};

Methods.addmethod (' CheckName ', function () {

Verify Name

return this;

}). Addmethod (' Checkemail ', function () {

Verify Mailbox

return this;

});

Methods.checkname (). Checkemail ();

This is called in a functional way.

5, Class call method (Chain-add)

Function.prototype.addMethod = function (NAME,FN) {

THIS.PROTOTYPE[NAME]=FN;

return this;

}

var Methods = function () {};

Methods.addmethod (' CheckName ', function () {

Verify Name

  return This

}). Addmethod (' Checkemail ', function () {

Verify Mailbox

return    This

});

But we can't use it right now, and we're going to create a new object by using the keyword new.

var m = new Methods ();

M.checkemail (). Checkemail ()

Two doubts: 1, function method of adding methods The statement why is This[name], why is this written?

2. How does a functional method statement for a class-add method be This.prototype[name]? Does this mean that the methods are not all added to the prototype? Does this not pollute the whole world?

Read "JS is design mode" first chapter Experience

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.