<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<title></title></head><body><script> ???//为内置对象添加原型方法 ???//实例中的方法如果没有,就去创建该实例对象的构造函数的原型对象中找 ???//在字符串中添加一个倒序字符串的方法 ???String.prototype.myReverse = function () { ???????for (var i = this.length - 1; i >= 0; i--) { ???????????console.log(this[i]); ???????} ???}; ???var str = "abcd"; ???str.myReverse(); ???//为Array内置对象的原型对象中添加方法 ???Array.prototype.mySort = function () { ???????for (var i = 1; i < this.length; i++) { ???????????for (var j = 0; j < this.length - i; j++) { ???????????????if (this[j] > this[j + 1]) { ???????????????????var temp = this[j]; ???????????????????this[j] = this[j + 1]; ???????????????????this[j + 1] = temp; ???????????????} ???????????} ???????} ???}; ???var arr = [100, 10, 200, 30, 20, 1]; ???arr.mySort(); ???console.log(arr);</script></body></html>
JS高级----------------->内置对象添加原型的方法
原文地址:https://www.cnblogs.com/cuilichao/p/9535266.html