首頁 > web前端 > js教程 > 詳解JS中Array物件擴充與String物件擴充的方法

詳解JS中Array物件擴充與String物件擴充的方法

怪我咯
發布: 2017-07-04 15:18:48
原創
1324 人瀏覽過

這篇文章主要介紹了詳解JS中Array物件擴充與String物件擴充的相關資料,需要的朋友可以參考下

廢話不多說了,直接給大家上array物件擴充程式碼了,具體程式碼如下所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

/**

* Created by laixiangran on 2016/01/07.

* Array扩展

*/

(function() {

// 遍历数组

if (typeof Array.prototype.forEach != "function") {

Array.prototype.forEach = function (fn, context) {

for (var i = 0; i < this.length; i++) {

if (typeof fn === "function" && Object.prototype.hasOwnProperty.call(this, i)) {

fn.call(context, this[i], i, this);

}

}

};

}

// 让数组中的每一个元素调用给定的函数,然后把得到的结果放到新数组中返回

if (typeof Array.prototype.map != "function") {

Array.prototype.map = function (fn, context) {

var arr = [];

if (typeof fn === "function") {

for (var k = 0, length = this.length; k < length; k++) {

arr.push(fn.call(context, this[k], k, this));

}

}

return arr;

};

}

// 把符合条件的元素放到一个新数组中返回

if (typeof Array.prototype.filter != "function") {

Array.prototype.filter = function (fn, context) {

var arr = [];

if (typeof fn === "function") {

for (var k = 0, length = this.length; k < length; k++) {

fn.call(context, this[k], k, this) && arr.push(this[k]);

}

}

return arr;

};

}

// 如果数组中的每个元素都能通过给定的函数的测试,则返回true,反之false

if (typeof Array.prototype.every != "function") {

Array.prototype.every = function (fn, context) {

var passed = true;

if (typeof fn === "function") {

for (var k = 0, length = this.length; k < length; k++) {

if (passed === false) break;

passed = !!fn.call(context, this[k], k, this);

}

}

return passed;

};

}

// 类似every函数,但只要有一个通过给定函数的测试就返回true

if (typeof Array.prototype.some != "function") {

Array.prototype.some = function (fn, context) {

var passed = false;

if (typeof fn === "function") {

for (var k = 0, length = this.length; k < length; k++) {

if (passed === true) break;

passed = !!fn.call(context, this[k], k, this);

}

}

return passed;

};

}

// 返回元素在数组的索引,没有则返回-1,从左到右

if (typeof Array.prototype.indexOf != "function") {

Array.prototype.indexOf = function (item, index) {

var n = this.length,

i = index == null ? 0 : index < 0 ? Math.max(0, n + index) : index;

for (; i < n; i++) {

if (i in this && this[i] === item) {

return i

}

}

return -1

};

}

// 返回元素在数组的索引,没有则返回-1,从右到左

if (typeof Array.prototype.lastIndexOf != "function") {

Array.prototype.lastIndexOf = function (item, index) {

var n = this.length,

i = index == null ? n-1 : index < 0 ? Math.max(0, n + index) : index;

for (; i >= 0; i--) {

if (i in this && this[i] === item) {

return i;

}

}

return -1;

};

}

// 让数组元素依次调用给定函数,最后返回一个值(从左到右)

if (typeof Array.prototype.reduce != "function") {

Array.prototype.reduce = function (callback, initialValue) {

var previous = initialValue, k = 0, length = this.length;

if (typeof initialValue === "undefined") {

previous = this[0];

k = 1;

}

if (typeof callback === "function") {

for (k; k < length; k++) {

this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this));

}

}

return previous;

};

}

// 让数组元素依次调用给定函数,最后返回一个值(从右到左)

if (typeof Array.prototype.reduceRight != "function") {

Array.prototype.reduceRight = function (callback, initialValue) {

var length = this.length, k = length - 1, previous = initialValue;

if (typeof initialValue === "undefined") {

previous = this[length - 1];

k--;

}

if (typeof callback === "function") {

for (k; k > -1; k-=1) {

this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this));

}

}

return previous;

};

}

// 去掉重复项(唯一性),返回新数组

if (typeof Array.prototype.uniq != "function") {

Array.prototype.uniq = function() {

var arr = [];

arr[0] = this[0];

for (var i = 1; i < this.length; i++) {

if (arr.indexOf(this[i]) == -1) {

arr.push(this[i]);

}

}

return arr;

};

}

// 指定删除数组中某值

if (typeof Array.prototype.remove != "function") {

Array.prototype.remove = function(item) {

for (var i = this.length; i >= 0; i--) {

if (item === this[i]) {

this.splice(i, 1);

}

}

return this;

};

}

// 打乱数组顺序

if (typeof Array.prototype.shuffle != "function") {

Array.prototype.shuffle = function() {

var i = this.length;

while (i) {

var j = Math.floor(Math.random()*i);

var t = this[--i];

this[i] = this[j];

this[j] = t;

}

return this;

};

}

// 求数组的最大值

if (typeof Array.prototype.max != "function") {

Array.prototype.max = function() {

return Math.max.apply({}, this)

};

}

// 求数组的最小值

if (typeof Array.prototype.max != "function") {

Array.prototype.min = function() {

return Math.min.apply({}, this)

};

}

 

// 判断是否为数组

if (typeof Array.prototype.isArray != "function") {

Array.prototype.isArray = function() {

return Object.prototype.toString.apply(this) === "[object Array]";

};

}

}());

登入後複製

下面是string物件擴充程式碼如下所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

/**

* Created by laixiangran on 2015/12/12.

* String扩展

*/

(function() {

// 十六进制颜色值的正则表达式

var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;

// RGB颜色转换为16进制

if (typeof String.prototype.rgbToHex != "function") {

String.prototype.rgbToHex = function() {

var that = this;

if (/^(rgb|RGB)/.test(that)) {

var aColor = that.replace(/(?:\(|\)|rgb|RGB)*/g,"").split(",");

var strHex = "#";

for (var i=0; i<aColor.length; i++) {

var hex = Number(aColor[i]).toString(16);

if (hex === "0") {

hex += hex;

}

strHex += hex;

}

if (strHex.length !== 7) {

strHex = that;

}

return strHex;

}else if (reg.test(that)) {

var aNum = that.replace(/#/,"").split("");

if (aNum.length === 6){

return that;

}else if (aNum.length === 3) {

var numHex = "#";

for (var j=0; j<aNum.length; j++) {

numHex += (aNum[j]+aNum[j]);

}

return numHex;

}

}else{

return that;

}

};

}

// 16进制颜色转为RGB格式

if (typeof String.prototype.hexToRgb != "function") {

String.prototype.hexToRgb = function() {

var sColor = this.toLowerCase();

if (sColor && reg.test(sColor)) {

if (sColor.length === 4) {

var sColorNew = "#";

for (var i = 1; i < 4; i++) {

sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));

}

sColor = sColorNew;

}

// 处理六位的颜色值

var sColorChange = [];

for (var j=1; j<7; j+=2) {

sColorChange.push(parseInt("0x"+sColor.slice(j,j+2)));

}

return "RGB(" + sColorChange.join(",") + ")";

}else{

return sColor;

}

};

}

// 移除字符串首尾空白

if (typeof String.prototype.trim != "function") {

String.prototype.trim = function() {

return this.replace(/^\s+|\s+$/g, "");

};

}

}());

登入後複製

以上是詳解JS中Array物件擴充與String物件擴充的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板