JS Extension Method Instance Analysis

Source: Internet
Author: User
Tags string to number

JS Extension Method Instance Analysis

The JS extension method is very similar to the C # extension method and can also be called in a chain. It is also implemented through the extension of a class. This is very useful. If you put the pre-written method into a js file for reference, it will be very interesting to write JavaScript later.

The following is an example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<Head>

<Title> test JS Extension Method </title>

<Script type = "text/javascript">

// Merge multiple blanks into one blank

String. prototype. ResetBlank = function () {// String extension

Var regEx =/\ s +/g;

Return this. replace (regEx ,'');

};

Window. onload = function ()

{

Var str = "are you sure you want to be okay? ";

Alert (str );

Str = str. ResetBlank (); // you can call it. It is similar to C!

Alert (str );

}

</Script>

</Head>

It seems that I just told myself that there was such a thing;

The following shows a very good js extension:

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

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

// Clear spaces on both sides

String. prototype. trim = function (){

Return this. replace (/(^ \ s *) | (\ s * $)/g ,'');

};

// Merge multiple blanks into one blank

String. prototype. ResetBlank = function (){

Var regEx =/\ s +/g;

Return this. replace (regEx ,'');

};

 

// Retain numbers

String. prototype. GetNum = function (){

Var regEx =/[^ \ d]/g;

Return this. replace (regEx ,'');

};

 

// Retain Chinese Characters

String. prototype. GetCN = function (){

Var regEx =/[^ \ u4e00-\ u9fa5 \ uf900-\ ufa2d]/g;

Return this. replace (regEx ,'');

};

 

// Convert String to Number

String. prototype. ToInt = function (){

Return isNaN (parseInt (this ))? This. toString (): parseInt (this );

};

 

// Get the byte length

String. prototype. GetLen = function (){

Var regEx =/^ [\ u4e00-\ u9fa5 \ uf900-\ ufa2d] + $ /;

If (regEx. test (this )){

Return this. length * 2;

} Else {

Var oMatches = this. match (/[\ x00-\ xff]/g );

Var oLength = this. length * 2-oMatches. length;

Return oLength;

}

};

 

// Obtain the full name of the object

String. prototype. GetFileName = function (){

Var regEx =/^. * \/([^ \/\?] *). * $ /;

Return this. replace (regEx, '$1 ');

};

 

// Obtain the file extension

String. prototype. GetExtensionName = function (){

Var regEx =/^. * \/[^ \/] * (\. [^ \. \?] *). * $ /;

Return this. replace (regEx, '$1 ');

};

 

// Replace all

String. prototype. replaceAll = function (reallyDo, replaceWith, ignoreCase)

{

If (! RegExp. prototype. isPrototypeOf (reallyDo )){

Return this. replace (new RegExp (reallyDo, (ignoreCase? "Gi": "g"), replaceWith );

} Else {

Return this. replace (reallyDo, replaceWith );

}

};

// Format the string add By Liu Jingning

String. Format = function (){

If (arguments. length = 0 ){

Return '';

}

 

If (arguments. length = 1 ){

Return arguments [0];

}

 

Var reg =/{(\ d + )?} /G;

Var args = arguments;

Var result = arguments [0]. replace (reg, function ($0, $1 ){

Return args [parseInt ($1) + 1];

});

Return result;

};

 

// Add zero to the number

Number. prototype. LenWithZero = function (oCount ){

Var strText = this. toString ();

While (strText. length <oCount ){

StrText = '0' + strText;

}

Return strText;

};

 

// Unicode Restoration

Number. prototype. ChrW = function (){

Return String. fromCharCode (this );

};

 

// The number array is sorted in ascending order.

Array. prototype. Min2Max = function (){

Var oValue;

For (var I = 0; I <this. length; I ++ ){

For (var j = 0; j <= I; j ++ ){

If (this [I] <this [j]) {

OValue = this [I];

This [I] = this [j];

This [j] = oValue;

}

}

}

Return this;

};

 

// The number array is sorted in ascending order.

Array. prototype. Max2Min = function (){

Var oValue;

For (var I = 0; I <this. length; I ++ ){

For (var j = 0; j <= I; j ++ ){

If (this [I]> this [j]) {

OValue = this [I];

This [I] = this [j];

This [j] = oValue;

}

}

}

Return this;

};

 

// Obtain the largest item in the number Array

Array. prototype. GetMax = function (){

Var oValue = 0;

For (var I = 0; I <this. length; I ++ ){

If (this [I]> oValue ){

OValue = this [I];

}

}

Return oValue;

};

 

// Obtain the smallest item in the number Array

Array. prototype. GetMin = function (){

Var oValue = 0;

For (var I = 0; I <this. length; I ++ ){

If (this [I] <oValue ){

OValue = this [I];

}

}

Return oValue;

};

 

// Obtain the Chinese form of the current time

Date. prototype. GetCNDate = function (){

Var oDateText = '';

ODateText + = this. getFullYear (). LenWithZero (4) + new Number (24180). ChrW ();

ODateText + = this. getMonth (). LenWithZero (2) + new Number (26376). ChrW ();

ODateText + = this. getDate (). LenWithZero (2) + new Number (26085). ChrW ();

ODateText + = this. getHours (). LenWithZero (2) + new Number (26102). ChrW ();

ODateText + = this. getMinutes (). LenWithZero (2) + new Number (20998). ChrW ();

ODateText + = this. getSeconds (). LenWithZero (2) + new Number (31186). ChrW ();

ODateText + = new Number (32 ). chrW () + new Number (32 ). chrW () + new Number (26143 ). chrW () + new Number (26399 ). chrW () + new String ('20140901 '). substr (this. getDay () * 5, 5 ). toInt (). chrW ();

Return oDateText;

};

// Extended Date formatting

Date. prototype. Format = function (format ){

Var o = {

"M +": this. getMonth () + 1, // month

"D +": this. getDate (), // day

"H +": this. getHours () % 12 = 0? 12: this. getHours () % 12, // hour

"H +": this. getHours (), // hour

"M +": this. getMinutes (), // minute

"S +": this. getSeconds (), // second

"Q +": Math. floor (this. getMonth () + 3)/3), // quarter

"S": this. getMilliseconds () // millisecond

};

Var week = {

"0": "\ u65e5 ",

"1": "\ u4e00 ",

"2": "\ u4e8c ",

"3": "\ u4e09 ",

"4": "\ u56db ",

"5": "\ u4e94 ",

"6": "\ u516d"

};

If (/(y +)/. test (format )){

Format = format. replace (RegExp. $1, (this. getFullYear () + ""). substr (4-RegExp. $1. length ));

}

If (/(E +)/. test (format )){

Format = format. replace (RegExp. $1, (RegExp. $1. length> 1 )? (RegExp. $1. length> 2? "\ U661f \ u671f": "\ u5468"): "") + week [this. getDay () + ""]);

}

For (var k in o ){

If (new RegExp ("(" + k + ")"). test (format )){

Format = format. replace (RegExp. $1, (RegExp. $1. length = 1 )? (O [k]): ("00" + o [k]). substr ("" + o [k]). length )));

}

}

Return format;

}

Date. prototype. Diff = function (interval, objDate ){

// If the parameter is insufficient or the objDate is not of the date type, the undefined will be returned.

If (arguments. length <2 | objDate. constructor! = Date) {return undefined ;}

Switch (interval ){

// Calculate the second difference

Case's ': return parseInt (objDate-this)/1000 );

// Calculate the Score Difference

Case 'N': return parseInt (objDate-this)/60000 );

// Computing time difference

Case 'H': return parseInt (objDate-this)/3600000 );

// Calculate the daily difference

Case 'D': return parseInt (objDate-this)/86400000 );

// Calculate the week difference

Case 'W': return parseInt (objDate-this)/(86400000*7 ));

// Calculate the monthly difference

Case 'M': return (objDate. getMonth () + 1) + (objDate. getFullYear ()-this. getFullYear () * 12)-(this. getMonth () + 1 );

// Calculate the year difference

Case 'y': return objDate. getFullYear ()-this. getFullYear ();

// Input error

Default: return undefined;

}

};

 

// Check whether it is empty

Object. prototype. IsNullOrEmpty = function (){

Var obj = this;

Var flag = false;

If (obj = null | obj = undefined | typeof (obj) = 'undefined' | obj = ''){

Flag = true;

} Else if (typeof (obj) = 'string '){

Obj = obj. trim ();

If (obj = '') {// null

Flag = true;

} Else {// not empty

Obj = obj. toUpperCase ();

If (obj = 'null' | obj = 'undefined' | obj = '{}'){

Flag = true;

}

}

}

Else {

Flag = false;

}

Return flag;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.