Home > Web Front-end > JS Tutorial > How to build a jQuery-like skeleton and test it (with code)

How to build a jQuery-like skeleton and test it (with code)

不言
Release: 2018-08-06 11:22:26
Original
1028 people have browsed it

The content of this article is about how to build a jQuery-like skeleton and test it (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Built a jQuery-like skeleton, the jQuery skeleton is almost like this
(function(global, factory) {
    if (typeof global.document === 'undefined') {
        throw new Error('the environment must have a window Object with document !')
    }
    // 若环境存在则执行factory
    factory(global);
})(typeof window !== 'undefined' ? window : this, function (window) {
    var _mJQ = function (selector) {
        return new _mJQ.init(selector);
    }
    // 初始化
    _mJQ.init = function(selector) {
        // 进行selector匹配,比如class,attr,id等...
        if (selector === '#test') {
            const elem = document.getElementById('test')
            this.elem = elem
            return this
        }
        return this
    }
    // 让init的原型对象指向_mJQ的原型
    _mJQ.init.prototype = _mJQ.prototype = {
        // 功能
        each: function() {
            // 循环
        },
        html: function() {},
        css: function (name, value) {
            console.log(this)
            this.elem.style[name] = value
        }
    }
    // 设置contructor指向问题
    Object.defineProperty(_mJQ.prototype, 'constructor', {
        enumerable: false,
        value: _mJQ
    })
    // 挂载到window
    window.$ = window.mJQ = _mJQ;
})
Copy after login

Test demo address

https://github.com/clm960227/...

Test Result

How to build a jQuery-like skeleton and test it (with code)

Recommended related articles: The use of elements and marker attributes in

svg Introduction

A brief introduction to JavaScript design patterns Adapter pattern

Introduction to two methods of Angular form validation

The above is the detailed content of How to build a jQuery-like skeleton and test it (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template