//我但願字符串中有一個倒序字符串的方法 String.prototype.myReverse = function () { for (var i = this.length - 1; i >= 0; i--) { console.log(this[i]); } }; var str = "abcdefg"; str.myReverse();
//爲Array內置對象的原型對象中添加方法 Array.prototype.mySort = function () { for (var i = 0; i < this.length - 1; i++) { for (var j = 0; j < this.length - 1 - i; j++) { if (this[j] < this[j + 1]) { var temp = this[j]; this[j] = this[j + 1]; this[j + 1] = temp; }//end if } //end for }//end for }; var arr = [30, 19, 304, 83, 349, 98, 90]; arr.mySort(); console.log(arr);
//爲字符串內置對象的原型對象中添加了sayHi的方法 String.prototype.sayHi = function () { console.log(this + "你好"); }; //字符串就有了打招呼的方法 var str2 = "Node.js開發指南"; str2.sayHi();