日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

prototype 属性重写对象方法和新定义对象方法

發布時間:2023/12/29 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 prototype 属性重写对象方法和新定义对象方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

<script>
/**
* @From: http://www.happyshow.org/article.asp?id=135
* @From: http://www.blueidea.com/tech/web/2003/1402.asp
* @From: http://www.cnscn.org
*/

/**
*? 具有prototype屬性的類型
*
* 數組變量(Array)
* 邏輯變量(Boolean)
* 日期變量(Date)
* 結構變量(Function)
* 數值變量(Number)
* 對象變量(Object)
* 字符串變量(String)
*/


/**
* 去掉字符串首尾的空格
*/
String.prototype.cnsTrim = function(){
??? var reg=/(^\s*)|(\s*$)/g;
??? return this.replace(reg, "");
}

/**
* 判斷是否合法email
*/
String.prototype.cnsValidEmail = function() {
?? var reg=/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
?? return reg.test(this);
}

/**
* 判斷是否數字字符串
*/
String.prototype.cnsIsDigit = function() {
?? var reg=/^([+-])?\d*\.?\d*$/;
?? return reg.test(this);
}

/**
* 判斷是否數字
*/
Number.prototype.cnsIsDigit = function() {
?? var reg=/^([+-])?\d*\.?\d*$/;
?? return reg.test(this);
}

?

/**
* 兼容漢字取字符串字節長
*/
String.prototype.cnsLength = function(){
?? var arr=this.match(/[^\x00-\xff]/ig);
?? return this.length+(arr==null?0:arr.length);
}

/**
* 截取字符串的左側子串
*/
String.prototype.left = function(num,mode){
??????? if(!/\d+/.test(num))return(this);
??????? var str = this.substr(0,num);
??????? if(!mode) return str;
??????? var n = str.cnsLength() - str.length;
??????? num = num - parseInt(n/2);
??????? return this.substr(0,num);
??? }


/**
* 取%或千分比等
*/
Number.prototype.toFixed = function(n) {
??????? with(Math) return round(Number(this)*pow(10,n))/pow(10,n)
??? }

/**
* 取出數組元素
*/
Array.prototype.pop = function() {
??????? var lastElement = this[this.length-1];
??????? this.length = Math.max(this.length-1,0);
??????? return lastElement;
??? }

/**
* 把元素加入數組
*/
Array.prototype.push = function(new_element){
?? this[this.length]=new_element;
?? return this.length;
}

/**
* 把多個元素都加入數組
*/
Array.prototype.push = function() {
??????? for(var i=0;i<arguments.length;i++) {
??????????? this[this.length]=arguments;
??????? }
??????? return this.length;
??? }


/**
* 刪除一個元素
*/
Array.prototype.shift = function() {
??????? var firstElement = this[0];
??????? this.reverse();
??????? this.pop();
??????? this.reverse();
??????? return firstElement;
}

Array.prototype.unshift = function() {
??????? var arr = new Array();
??????? for (var i=0;i<arguments.length;arr=arguments[i++]);
??????? arr = arr.concat(this);
??????? this.length = 0;
??????? for (i=0;i<arr.length;this=arr[i++]);
}


/**
* 對數組進行切片
*/
Array.prototype.splice = function() {
??????? var start = arguments[0];
??????? var deleteCount = start+arguments[1];
??????? var deleteItem = this.slice(start,deleteCount);
??????? var beforeItem = this.slice(0,start);
??????? var afterItem = this.slice(deleteCount);
??????? this.length=beforeItem.length;
??????? var i;
??????? for (i=2;i<arguments.length;this[this.length]=arguments[i++]);
??????? for (i=0;i<afterItem.length;this[this.length]=afterItem[i++]);
??????? return deleteItem;
}


/**
* 過濾字符串里面的html標記,類似于 innerText
*/
String.prototype.stripTags = function() {
??? return this.replace(/<\/?[^>]+>/gi, '');
}

/**
*? 過濾掉字符串里面的js
*/
String.prototype.stripScripts = function() {
??? return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
}

/**
* 與 stripScript 相反,取出script標簽里面的代碼,不包括 <script>標記本身
*/
String.prototype.extractScripts = function() {
??? var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
??? var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
??? return (this.match(matchAll) || []).map(function(scriptTag) {
????? return (scriptTag.match(matchOne) || ['', ''])[1];
??? });
}

/**
* 運行腳本
*/
String.prototype.evalScripts = function() {
??? return this.extractScripts().map(eval);
? }

/**
* 類似 escape,轉換 < > 為 < >
*/
String.prototype.escapeHTML = function() {
??? var div = document.createElement('div');
??? var text = document.createTextNode(this);
??? div.appendChild(text);
??? return div.innerHTML;
}

/**
* 和上面相反
*/
String.prototype.unescapeHTML = function() {
??? var div = document.createElement('div');
??? div.innerHTML = this.stripTags();
??? return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
}


/**
* var stringA = "?id=2&type=product";?
* stringA.toQueryParams() 將返回 {id="2"; type="product"}
* 這是個object,具有id與type屬性。
*/
String.prototype.toQueryParams = function() {
??? var pairs = this.match(/^\??(.*)$/)[1].split('&');
??? return pairs.inject({}, function(params, pairString) {
????? var pair = pairString.split('=');
????? params[pair[0]] = pair[1];
????? return params;
??? });
}

/**
* 把字符串轉換為數組,每個字符占一個數組元素
*/
String.prototype.toArray = function(arg) {
??? return this.split(arg?arg:' ');
}


/**
* 把用減號連接的組合單詞如:background-color連接成 backgroundColor
* 調用方法:var ss ="background_color"; ss.camelize();
*/
String.prototype.camelize = function() {
??? var oStringList = this.split('-');
??? if (oStringList.length == 1) return oStringList[0];

??? var camelizedString = this.indexOf('-') == 0
????? ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1)
????? : oStringList[0];

??? for (var i = 1, len = oStringList.length; i < len; i++) {
????? var s = oStringList;
????? camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
??? }

??? return camelizedString;
}


/**
* 轉義單引號與斜杠
*/
String.prototype.inspect = function() {
??? return "'" + this.replace('\\', '\\\\').replace("'", '\\\'') + "'";
}


//測試
//1)
var str="???? xxxxxxxxx?? ";
alert("'"+str.cnsTrim()+"'");


//2)
var str="cnscn@163.com";
alert(str.cnsValidEmail());

//3)
var str="-456"; //調用String.prototype.cnsIsDigit()
alert(str.cnsIsDigit());

var str=-456; //調用Number.prototype.cnsIsDigit()
alert(str.cnsIsDigit());


var arr = new Array();
arr.push(1);
arr.push(2);
arr.push(3);
alert(arr);


var str=" 兼\"容";
//alert(str.cnsLength());

//alert(str.left(2, true));
alert(str.toArray("'"));
</script>

總結

以上是生活随笔為你收集整理的prototype 属性重写对象方法和新定义对象方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。