RSS
热门关键字:
当前位置 :| 主页>web development>

JavaScript is patulous: Yong of of lie of of of Si Yao

来源: 作者:Stand originally 时间:2008-12-21 Tag: 点击:
Textual address: " Regular Expressions As Functions "

Firefox included a nonstandard JavaScript to expand, make can be being called like function. This is call criterion Exec method was offerred convenient. For example, in Firefox, regex(”string”) is equal at Regex.exec(”string”) . Once ECMAScript 4 suggests to point out this function will increase in ES4 standard, but in the in ES4-discuss Mailing List discussion later, this suggests the likelihood is abolished.

However, you can pass the similar reality in adding Call and Apply method to RegExp.prototype these methods. Conduce to functional design already, can come true to be mixed to function again criterion expression all conceals a type effectively (Duck-typed) code. Accordingly, let us add these methods.

RegExp.prototype.call = Function (context, str) {Return This.exec(str);
};
RegExp.prototype.apply = Function (context, args) {Return This.exec(args[0]);
};

Two methods above the attention are complete oversight Context parameter, you can refer Null to perhaps hold the post of the object that he regards how as Context, and you will get similarly criterion the return of value of Exec method. Use the method above, no matter be below what circumstance, make we are used normally criterion expression and function become easy much. A few apparent example, these are very for instance useful in the array iteration of JavaScript 1.6. The Filter below, every, some, with Map methodological execution can cross a browser.
If (! Array.prototype.filter) {
/ / return an array, if offer filter function returns True, return the element in the array of existence.
Array.prototype.filter = Function (func, context) {Var Results = [];For (var I = 0; I<This.length; I ) {If (i In This &&Func.call(context, this[i] , i, this) )Results.push(this[i]);
}Return Results;
};
}
If (! Array.prototype.every) {
/ / return True, the test function that if every element in array is contented,provides.
Array.prototype.every = Function (func, context) {For (var I = 0; I<This.length; I ) {If (i In This &&! Func.call(context, this[i] , i, this) )Return False;
}Return True;
};
}
If (! Array.prototype.some) {
/ / return True, if there is an element at least in array,satisfy offerred test function.
Array.prototype.some = Function (func, context) {For (var I = 0; I<This.length; I ) {If (i In This &&Func.call(context, this[i] , i, this) )Return True;
}Return False;
};
}
If (! Array.prototype.map) {
/ / return an array, every element in existing array calls the return of value of offerred function.
Array.prototype.map = Function (func, context) {Var Results = [];For (var I = 0; I<This.length; I ) {If (i In This)Results[i] = Func.call(context, this[i] , i, this);
}Return Results;
};
}

Because Exec method returns array or Null value, meet appropriate type changeover is True and False, the code above allows us to resemble such use: [”a” , ”b” , ”ab” , ”ba”].filter(/^a/) , return all values that begin with “a” : [”a” , ”ab”] .
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册