Write yourself a jquery

Source: Internet
Author: User

By reading the jquery original code, we can simulate writing a simple jquery

Like the usual

JQuery ("div"). CSS ("Color", "red");
JQuery ("#span1"). CSS ("Color", "green");

1. JQuery (), because it is a chained call, so the return is an object.

        JQuery = function (selector) {            return new JQuery.prototype.init (selector);        }        Jquery.prototype = {            constructor:jquery,            init:function (selector) {                this.elements = Document.queryselectorall (selector);            },            css:function (key, value) {            ...            }        }

At this point, this.elements is all the conditional HTML elements

2. CSS (), set the style for all the appropriate conditions of element.

            Css:function (key, value) {                This.elements.forEach (element = = {                    Element.style[key] = value;                });            

3. At this point, CSS is not accessible to this.elements. Because this.elements is defined in init, and Init is a constructor. So this.elements is the attribute inside the init instance.

        Jquery.prototype.init.prototype=jquery.prototype;

This way the CSS () will be able to access the this.elements.

Full code

        JQuery = function (selector) {            return new JQuery.prototype.init (selector);        }        Jquery.prototype = {            constructor:jquery,            init:function (selector) {                this.elements = Document.queryselectorall (selector);            },            css:function (key, value) {                This.elements.forEach (element = > {                    Element.style[key] = value;                });            }        }        Jquery.prototype.init.prototype=jquery.prototype;

The last name of the alias

Jquery.fn=jquery.prototype

        JQuery = function (selector) {            return new JQuery.fn.init (selector);        }        Jquery.fn = Jquery.prototype = {            constructor:jquery,            init:function (selector) {                this.elements = Document.queryselectorall (selector);            },            css:function (key, value) {                This.elements.forEach (element = > {                    Element.style[key] = value;                });            }        }        Jquery.fn.init.prototype=jquery.fn;

Test it for a moment.

Write yourself a jquery

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.