我用这篇文章来理一理如何用js去实现封装jQuery的简略办法。
文末有残缺代码链接,须要代码的敌人间接看文末
本文js实现了上面jquery的几种办法,我将它分为8个小指标
- 实现$(".box1").click( )办法
- 实现$("div").click( )办法
- 思考$( )中参数的三种状况
- 实现jq中的on办法
- 实现链式操作
- 实现jq中的eq办法
- 实现jq中的end办法
- 实现jq中的css办法
有不正确的中央还望大家在评论区指出来,谢谢啦。
1. 实现$(".box1").click( )办法
首先,咱们定第一个小指标,就是如何一步一步去实现下方jQuery代码的性能。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
//同一个文件下操作的话,前面记得删除上面引入的cdn
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<style>
.box1 {
width: 100px;
height: 100px;
background: red;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
<script>
$(".box1").click(()=>{
console.log(456);
})
</script>
</html>
这里说一下哈,不会jQuery的小伙伴也别慌,只有你有一点点js根底,花个10分钟到jquery教程简略理解一下即可。
$(".box1").click(()=>{
console.log(456);
})
复制代码
好了,言归正传,咱们来剖析下面jQuery的代码。
- $(".box1") 就是实现了选择器的性能。
- $(".box1").click 就是选择器 + 调用click办法
- 最初在click外面传入函数。
第一个小指标就是本人封装js来实现下面代码的性能。咱们分三步走策略来实现。
- js实现 $(".box1")
- 实现 $(".box1").click()
- 实现 $(".box1").click( ( ) => { console.log("123") } )
第一步就是先用js实现 $(".box1"), 对吧
// 实现$(".box1")
class jquery {
constructor(arg) {
console.log(document.querySelector(arg));
}
}
function $(arg) {
return new jquery(arg);
}
// 实现$(".box1")
let res = $(".box1");
console.log(res);
复制代码
这样是不是就通过构建()办法并返回jquery实例,实现了()办法并返回_jquer**y_实例,实现了(".box1")呢。
那好,接下来咱们进行第二步就是实现 $(".box1").click()。置信大家也看进去了,就是在jquery类中多了一个click办法。
// 实现$(".box1").click()
class jquery {
constructor(arg) {
console.log(document.querySelector(arg));
}
click() {
console.log("执行了click办法");
}
}
function $(arg) {
return new jquery(arg);
}
// 实现$(".box1").click()
let res = $(".box1").click();
console.log(res);
复制代码
接下来,咱们进行第三步就是实现 $(".box1").click( ( ) => { console.log("123") } )。
// 实现$(".box1").click(() => {console.log("123")})
class jquery {
constructor(arg) {
this.element = document.querySelector(arg);
// console.log(element);
}
click(fn) {
this.element.addEventListener("click", fn);
}
}
function $(arg) {
return new jquery(arg);
}
//实现$(".box1").click(() => {console.log("123")})
$(".box1").click(() => {
console.log("123")
});
复制代码
到此为止,咱们实现了第一个小指标,大家是不是感觉简略呢,ok,接下来咱们持续第二个小指标。
2. 实现$("div").click( )办法
第二个小指标也不难,就是思考有多个div元素须要绑定click事件,咱们用selectSelectorAll来获取元素的话,如何解决,其实也挺简略,就是在click办法中多出一个循环,去获取NodeList中的值。我间接上代码了,大家试一试就晓得啦。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1 {
width: 100px;
height: 100px;
background: red;
}
.box2 {
width: 100px;
height: 100px;
background: blue;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
<script>
// 实现$(".box1").click(() => {console.log("123")})
class jquery {
constructor(arg) {
//上面element存的是NodeList对象,它是一个类数组有length属性
this.element = document.querySelectorAll(arg);
}
click(fn) {
for(let i = 0; i < this.element.length; i++) {
this.element[i].addEventListener("click", fn);
}
}
}
function $(arg) {
return new jquery(arg);
}
//实现$(".box1").click(() => {console.log("123")})
$("div").click(() => {
console.log("123")
});
</script>
</html>
好了,实现两个小指标了,置信你曾经有成就感了。
3. 思考$( )中参数的三种状况
接下来第三个小指标 咱们来考虑一下$( )中参数不同的状况,我先将三种状况列出来。(可能还有其余状况,这里就不说了)
1.状况一:就是$( )参数为字符串
$(".box1")
复制代码
2.状况二:就是$( )参数为函数的状况。
//参数为函数
$(function() {
console.log("123");
})
复制代码
3.状况三:就是$( )参数为NodeList对象或selectSelect取得的节点
// 状况三
$(document.querySelectorAll("div")).click(()=>{
console.log("123");
})
$(document.querySelector("div")).click(()=>{
console.log("456");
})
接下来第三个小指标是手写函数来实现三种状况。 首先咱们减少addEles办法,批改下面的click办法
addEles(eles){
for (let i = 0; i < eles.length; i++) {
this[i] = eles[i];
}
this.length = eles.length;
}
// 实现$(".box1").click(() => {console.log("123")})
click(fn) {
for(let i = 0; i < this.length; i++) {
this[i].addEventListener("click", fn);
}
}
接下来实现三种不同参数的解决办法
constructor(arg) {
//状况一
if(typeof arg === 'string') {
this.addEles(document.querySelectorAll(arg));
}else if(typeof arg === 'function') {
//状况二
document.addEventListener("DOMContentLoaded", arg);
}else {
//状况三
if(typeof arg.length === 'undefined') {
this[0] = arg;
this.length = 1;
}else {
this.addEles(arg);
}
}
}
4. 实现jq中的on办法
接下来实现第四个小指标 实现jq的on办法
// on办法
on(eventName, fn) {
let eventArray = eventName.split(" ");
//思考多个节点
for(let i = 0; i < this.length; i++) {
//思考多个事件
for(let j = 0; j < eventArray.length; j++) {
this[i].addEventListener(eventArray[j], fn);
}
}
}
再测试下ok不
// on办法
$("div").on("mouseover mousedown",function(){
console.log("on办法");
})
5. 实现链式操作
接下来实现第五个小指标 实现jq的链式操作
划重点,在on和click中增加return this即可实现链式
//链式操作
//划重点,在on和click中增加return this即可实现链式
// click办法
click(fn) {
for(let i = 0; i < this.length; i++) {
this[i].addEventListener("click", fn);
}
return this;
// console.log(this);
}
// on办法
on(eventName, fn) {
let eventArray = eventName.split(" ");
//思考多个节点
for(let i = 0; i < this.length; i++) {
//思考多个事件
for(let j = 0; j < eventArray.length; j++) {
this[i].addEventListener(eventArray[j], fn);
}
}
return this;
}
6. 实现jq中的eq办法
接下来实现第六个小指标 实现jq中的eq办法
//eq办法
eq(index) {
return new jquery(this[index]);
}
这里通过new一个jquery实现 new的过程大家应该分明吧,咱们复习一下:
- 执行函数
- 主动创立一个空对象
- 将空对象的原型指向构造函数的prototype属性
- 将空对象和函数外部this绑定
- 如果renturn后跟着对象,返回这个对象。没跟的话就主动返回this对象
7. 实现jq中的end办法
实现第七个小指标 实现jq中的end办法。要实现这个性能,除了新增end( )办法,咱们还得在构造函数上实现,constructor新增参数root,新增属性prevObject,并在eq办法这种新增参数this。
constructor(arg, root) {
if(typeof root === "undefined") {
this.prevObject = [document];
}else {
this.prevObject = root;
}
//eq办法
eq(index) {
return new jquery(this[index], this);
}
//end办法
end() {
return this.prevObject;
}
8. 实现jq中的css办法
在jq中css能够获取款式,设置一个款式或多个款式
// 状况一 :获取款式 (只去获取第一个元素)
let res = $("div").css("background");
console.log(res);
// 状况二 (设置款式)
$("div").css("background","yellow");
// // 状况三 (设置多个款式)
$("div").css({background:"black",width:200,opacity:0.3});
接下来实现css办法
//css办法
css(...args) {
if(args.length === 1) {
//状况一:获取款式
if(typeof args[0] === 'string') {
return this.getStyle(this[0], args[0]);
}else {
//状况三:设置多个款式
for(let i = 0; i < this.length; i++) {
for(let j in args[0]) {
this.setStyle(this[i], j, args0);
}
}
}
}else {
//状况三
for(let i = 0; i < this.length; i++) {
this.setStyle(this[i], args[0], args[1]);
}
}
} //css办法
css(...args) {
if(args.length === 1) {
//状况一:获取款式
if(typeof args[0] === 'string') {
return this.getStyle(this[0], args[0]);
}else {
//状况三:设置多个款式
for(let i = 0; i < this.length; i++) {
for(let j in args[0]) {
this.setStyle(this[i], j, args0);
}
}
}
}else {
//状况三
for(let i = 0; i < this.length; i++) {
this.setStyle(this[i], args[0], args[1]);
}
}
}
减少cssNumber办法来确定不必加px的属性名
//css办法用
$.cssNumber = {
animationIterationCount: true,
columnCount: true,
fillOpacity: true,
flexGrow: true,
flexShrink: true,
fontWeight: true,
gridArea: true,
gridColumn: true,
gridColumnEnd: true,
gridColumnStart: true,
gridRow: true,
gridRowEnd: true,
gridRowStart: true,
lineHeight: true,
opacity: true,
order: true,
orphans: true,
widows: true,
zIndex: true,
zoom: true
}
最初献上残缺代码,如果大哥们觉的不错,给个赞呗
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1 {
width: 100px;
height: 100px;
background: red;
}
.box2 {
width: 100px;
height: 100px;
background: blue;
transform-origin: 0 100% 0;
transition: 0.3s;
}
.box2:hover {
transform: scaleX(2);
width: 200px;
height: 100px;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<button>点击</button>
</body>
<script>
class jquery {
constructor(arg, root) {
if(typeof root === "undefined") {
this.prevObject = [document];
}else {
this.prevObject = root;
}
//状况一
if(typeof arg === 'string') {
this.addEles(document.querySelectorAll(arg));
}else if(typeof arg === 'function') {
//状况二
document.addEventListener("DOMContentLoaded", arg);
}else {
//状况三
if(typeof arg.length === 'undefined') {
this[0] = arg;
this.length = 1;
}else {
this.addEles(arg);
}
}
}
//减少办法
addEles(eles){
for (let i = 0; i < eles.length; i++) {
this[i] = eles[i];
}
this.length = eles.length;
}
//链式操作
//划重点,在on和click中增加return即可实现链式
// click办法
click(fn) {
for(let i = 0; i < this.length; i++) {
this[i].addEventListener("click", fn);
}
return this;
// console.log(this);
}
// on办法
on(eventName, fn) {
let eventArray = eventName.split(" ");
//思考多个节点
for(let i = 0; i < this.length; i++) {
//思考多个事件
for(let j = 0; j < eventArray.length; j++) {
this[i].addEventListener(eventArray[j], fn);
}
}
return this;
}
//eq办法
eq(index) {
return new jquery(this[index], this);
}
//end办法
end() {
return this.prevObject;
}
//css办法
css(...args) {
if(args.length === 1) {
//状况一:获取款式
if(typeof args[0] === 'string') {
return this.getStyle(this[0], args[0]);
}else {
//状况三:设置多个款式
for(let i = 0; i < this.length; i++) {
for(let j in args[0]) {
this.setStyle(this[i], j, args0);
}
}
}
}else {
//状况三
for(let i = 0; i < this.length; i++) {
this.setStyle(this[i], args[0], args[1]);
}
}
}
getStyle(ele, styleName) {
return window.getComputedStyle(ele, null)[styleName];
}
setStyle(ele, styleName, styleValue) {
if(typeof styleValue === "number" && !(styleName in $.cssNumber)) {
styleValue = styleValue + "px";
}
ele.style[styleName] = styleValue;
}
}
function $(arg) {
return new jquery(arg);
}
//css办法用
$.cssNumber = {
animationIterationCount: true,
columnCount: true,
fillOpacity: true,
flexGrow: true,
flexShrink: true,
fontWeight: true,
gridArea: true,
gridColumn: true,
gridColumnEnd: true,
gridColumnStart: true,
gridRow: true,
gridRowEnd: true,
gridRowStart: true,
lineHeight: true,
opacity: true,
order: true,
orphans: true,
widows: true,
zIndex: true,
zoom: true
}
// //实现状况一:$(".box1")
// $("div").click(() => {
// console.log("123")
// });
// //实现状况二:参数为函数
// $(function() {
// console.log('状况2');
// })
// // 状况三
// $(document.querySelectorAll("div")).click(()=>{
// console.log("123");
// })
// $(document.querySelector("div")).click(()=>{
// console.log("456");
// })
// // on办法
// $("div").on("mouseover mousedown",function(){
// console.log("on办法");
// })
//链式操作
// $("div").click(() => {
// console.log("click办法")
// }).on("mouseover", function() {
// console.log('链式on办法');
// })
// $("div").on("mouseover", function() {
// console.log('链式on办法');
// }).click(() => {
// console.log("click办法")
// })
// //eq办法
// $("div").eq(0).click(() => {
// console.log("eq办法")
// })
//endf办法
// let res = $("div").eq(0).eq(0).eq(0).end();
// console.log(res);
//css办法
// 状况一 :获取款式 (只去获取第一个元素)
// let res = $("div").css("background");
// console.log(res);
// 状况二 (设置款式)
// $("div").css("background","yellow");
// // 状况三 (设置多个款式)
// $("div").css({background:"black",width:200,opacity:0.3});
</script>
</html>