Because it is an auxiliary class library, in order to be compatible with all other frameworks and class libraries, a wrapper is used to extend the object. The main content of the D class library is the extension of commonly used built-in objects in js, such as: String, Number, Array, Date, etc. These extensions focus on specific business logic, such as the trim method extended to String and toStr extended to Date. Methods, etc. are extensions to some commonly used functions that are not supported by the object itself and are not supported or incompletely supported by the framework class library. At the same time, through the packaging of the corresponding wrapper, we can operate the object through the chain method. Finally, each wrapper provides an unboxing (that is, restoring to the native object) method. Therefore, the essence provided by the packager is a process of packing, operating, and unpacking.
namespace:
var D = {};
Some functions are as follows:
. String wrapper
(function(){
//Packaging String
D.str = function(s){
if(! (this instanceof y.str ))return new y.str(s);
this.val = (s!==undefined) ? s.toString() : "";
};
D.str.prototype = {
//Remove the blanks on both sides of the string
trim : function(type){
var types = {0:"(^\s )|(\s $)",1:"^\s " ,2:"\s $"};
type = type || 0;
this.val = this.val.replace(new RegExp(types[type],"g"),"");
return this;
},
//Repeat string
repeat: function(n){
this.val = Array(n 1).join(this.val);
return this;
},
//Padding on both sides of the string
padding: function(len,dire,str){
if(this.val.length>=len)return this;
dire = dire || 0; //[0 represents the left, 1 represents the right]
str = str || " "; //The default is a blank character
var adder = [];
for(var i=0,l = len - this.val.length; i
}
adder = adder.join("" );
this.val = dire ? (this.val adder) : (adder this.val);
return this;
},
reverse : function(){
this. val = this.val.split("").reverse().join("");
return this;
},
byteLen : function(){
return this.val. replace(/[^x00-xff]/g,"--").length;
},
unBox : function(){
return this.val;
}
} ;
//alert(D.str(" 123 ").trim().repeat(2).padding(10,0,"x").reverse().unBox());
} )();
.Array wrapper
(function(){
//Packaging Array
D.arr = function(arr){
if(!(this instanceof D.arr))return new D.arr(arr);
this.val = arr || [];
};
D.arr.prototype = {
each : function(fn){
for (var i=0,len=this.val.length;i
return this;
}
}
return this;
},
map : function(fn){
var copy = [];
for(var i=0,len = this .val.length;i
}
this.val = copy;
return this ;
},
filter : function(fn){
var copy = [];
for(var i=0,len=this.val.length;i
}
this.val = copy;
return this;
} ,
remove : function(obj,fn){
fn = fn || function(m,n){
return m===n;
};
for(var i =0,len = this.val.length;i
this.val.splice (i,1);
}
}
return this;
},
unique : function(){
var o = {}, arr = [];
for(var i=0,len = this.val.length;i
(!o[itm] || (o[ itm]!==itm) )&& (arr.push(itm),o[itm] = itm);
}
this.val = arr;
return this;
},
indexOf : function(obj,start){
var len = this.val.length,start = ~~start;
start for(;start< ;len;start ){
if(this.val[start]===obj)return start;
}
return -1;
},
lastIndexOf : function(obj, start){
var len = this.val.length,start = arguments.length === 2 ? ~~start : len-1;
start = start for(;start>-1;start--){
if(this.val[start] === obj)return start;
}
return -1;
},
unBox : function(){
return this.val;
}
};
//alert( D.arr (["123",123]).unique().unBox());
//alert(D.arr([1,2,3]).map(function(i){return i;} ).filter(function(i){return i>2;}).remove(3).unBox());
})();
.Number wrapper
(function(){
//Pack Number
D.num = function(num){
if(!(this instanceof D.num))return new D.num(num);
this.val = Number (num) || 0;
};
D.num.prototype = {
padZore : function(len){
var val = this.val.toString();
if (val.length>=len)return this;
for(var i=0,l = len-val.length;i
}
return val;
},
floatRound : function(n){
n = n || 0;
var temp = Math.pow(10,n);
this .val = Math.round(this.val * temp)/temp;
return this;
},
unBox : function(){
return this.val;
}
};
//alert(D.num(3.1235888).floatRound(3).unBox());
})();
.Date packaging Device
(function(){
//Packaging Date
D.date = function(date){
if(!(this instanceof D.date))return new D.date(date);
if(!(date instanceof Date) ){
var d = new Date(date);
this.val = (d == "Invalid Date" || d == "NaN") ? new Date() : new Date(date);
}else{
this.val = date;
}
};
D.date.prototype = {
toStr : function(tpl){
var date = this.val,tpl = tpl || "yyyy-MM-dd hh:mm:ss";
var v = [date.getFullYear(),date.getMilliseconds(),date.getMonth() 1,date. getDate(),date.getHours(),date.getMinutes(),date.getSeconds()];
var k = "MM,M,dd,d,hh,h,mm,m,ss,s" .split(",");
var kv = {"yyyy":v[0],"yy":v[0].toString().substring(2),"mmss":("000" v[1]).slice(-4),"ms":v[1]};
for(var i=2;i
kv[k[(i-2)*2 1]] = v[i];
}
for(var k in kv){
tpl = tpl.replace(new RegExp( k,"g"),kv[k]);
}
return tpl;
},
unBox : function(){
return this.val;
}
};
//alert(D.date("2017-123-12").toStr( "yyyy-MM-dd hh:mm:ss ms-mmss"));
// alert(D.date("2017").unBox());
})();
. Finally, in order for D to be able to undertake the task of dom operation without being separated from other framework libraries, a Dom wrapper was implemented, as follows:
(function(){
2 //包装Dom
3 D.dom = function(node){
4 if(!(D.dom의 이 인스턴스))return new D .dom(노드);
5 if(typeof 노드 === "정의되지 않음"){
6 node = document.body
7 }else if(typeof 노드 == "string"){
8 node = document.getElementById(node);
9 !node && (node = document.body)
}else{
!node.getElementById && (node = document.body); 🎜>}
this.val = node;
};
D.dom.prototype = {
inner : function(value){
this.val.innerHTML ? || "",this.val.innerHTML = 값) : (값 = 값 || 0,this.val.value = 값)
return this
},
attr : function(k ,v){
if(typeof k == "객체"){
for(var m in k){
this.val[m] = k[m]}
}else{
this.val[k] = v;
}
return this
},
css : function(k,v){
var style = this; .val.style;
if(typeof k == "object"){
for(var m in k){
style[m] = k[m]
}else{
style[k] = v;
}
return this;
},
addClass : function(cls){
var clsName = " " this.val. 클래스명 " ";
(clsName.indexOf(" " cls " ")==-1) && (clsName = (clsName cls).replace(/^s /,""));
this.val.className = clsName;
이것을 돌려주세요;
},
removeClass : function(cls){
var clsName = " " this.val.className " ";
this.val.className = clsName.replace(new RegExp(" " cls " ","g"),"").replace(/(^s )|(s $)/,"");
이것을 돌려주세요;
},
addEvent : function(evType,fn){
var that = this, typeEvent = this.val["on" evType];
if(!typeEvent){
(this.val["on" evType] = function(){
var fnQueue = 인수.callee.funcs;
for(var i=0;i< ;fnQueue.length;i ){
fnQueue[i].call(that.val)
}
}).funcs =[fn];
}else{
typeEvent.funcs.push(fn);
}
이것을 반환하세요.
},
delEvent : function(evType,fn){
if(fn===undefine){
this.val["on" evType] = null;
}else{
var fnQueue = this.val["on" evType].funcs;
for(var i=fnQueue.length-1;i>-1;i--){
if(fnQueue[i] === fn){
fnQueue.splice(i,1) ;
휴식;
}
}
fnQueue.length==0 && (this.val["on" evType] = null);
}
이것을 반환하세요.
},
unBox : function(){
return this.val;
}
};
//静态방법
var __ = D.dom;
__.$ = function(id){
반환 유형 id == "string" ? document.getElementById(id) : id;
};
__.$$ = function(tag,box){
return (box===undefine?document:box).getElementsByTagName(tag);
};
__.$cls = function(cls,tag,node){
node = node === 정의되지 않음 ? 문서: 노드;
cls = cls.replace(/(.)|(^s )|(s $)/g,"");
if(node.getElementsByClassName)return node.getElementsByClassName(cls);
태그 = 태그 === 정의되지 않음 ? "*" : 태그;
var filter = [], 노드 = (tag==="*" && node.all) ? node.all : node.getElementsByTagName(태그);
for(var i=0,j=nodes.length;i
}
반환 필터;
};
//静态方法结束
alert(D.dom.$cls(".abc").length);
})();
Dom包装器的实例对象负责当前dom节点的自身操작품 🎜>
以上就是D类库的初级版本, 其中的要要part——对内置对象的扩展目前只有较少象的物話,比如对Numberally 扩件中,到当多数字操작품,其中有一些是常可以将其添加入Number包装器,好处也是显而易见的。
最后如果你看到了这篇文章,有足够的想法,我希望你能尽你所能来给于包装더 많은 방법을 확장하면, 이 부분이 주요 의미로 사용됩니다.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor
