window.jQuery =function(nodeOrSelector){
letNode ={}// The unified return type is a pseudo-array objectif(typeof nodeOrSelector==="string"){// Pass in the stringletTemp = document. QuerySelectorAll (nodeOrSelector) / / find the specified elementfor(leti=0; i<temp.length; I ++){// Change nodeList to a pseudo-array object called Nodes [I]=temp[I]} nodes.length=temp.length; }else if(nodeOrSelector instanceof Node) {/ / incoming dom element nodes = {0: nodeOrSelector, length: 1} / / to the length of the pseudo array object} nodes. The addClass =function(classes){// Add the addClass methodfor (let i = 0; i < nodes.length; i++) {
nodes[i].classList.add(classes)
}
}
nodes.setText=function(text) {/ / addedsetText methodfor(leti=0; i<nodes.length; i++){ nodes[i].textContent=text } }returnnodes; } window.$= jQuery//aliasmodelCopy the code
- Code encapsulation as a method, easy to reuse
- Implement non-intrusive call methods by returning new objects
- Alias alias