JavaScriptJScript core language object expansion function

zhaozj2021-02-16  53

JavaScript / JScript core language object expansion function

First, the expansion and code of the object function

The objects of the expansion function include Array, String, Date, Number, Boolean.

Array object expansion function

Function name

parameter

Description

Indexof

_Value

Returns an array medium value equal to the element index position of the parameter _value, if not returned - 1

LastIndexof

_Value

Returns an array medium value equal to the element of the parameter _Value reverse index position, if not returned -1

Contains

_Value

Returns whether there is a value in the array equal to the parameter _Value, true is existence, FALSE does not exist

Copy

Copy array to a new array

insertat

_Value, i

Insert parameter value _Value to parameter i specifies the location of the index

INSERTBEFORE

_Value, _Invalue

Insert parameter value _Value to array is equal to the element of the parameter _INValue

REMOVEAT

i

Delete an element of an index position is i

Remove

_Value

Elements that delete the value _value

String object expansion function

Function name

parameter

Description

TRIM

Returns the string of the front and rear blank characters

Ltrim

Returns the string of blank characters left on the left

RTRIM

Returns the string of blank characters on the right side

LTRIMZERO

Returns the string of "0" character on the left

Left

_leftnum

Return to the left _Leftnum characters

Right

_Rightnum

Return to the right _Rightnum characters

Hasalpha

Judging whether the string has an alpha character (@ # $, other non-digital alphabetic characters)

Isalpha

Determine if the string is completely alpha character

Isletter

Judging whether the string is completely alphabetic characters

Isfigure

Judging whether the string is completely numeric characters

IsDomainName

Judging whether the string is a legitimate domain name string

Isemail

Judging whether the string is an email address

ISHEX

Judging whether the string is a hexadecimal character

ISGUID

Judging whether the string is a GUID value

Isinteger

_bittype

Determine whether the string can be the integer number of _bittype type ("64bit" || "32bit" || "16bit")

Isint8

Judging whether the string can be an integer of INT8

ISINT16

Judging whether the string can be integer for INT16

ISINT32

Determine whether the string can be integer for INT32

ISINT64

Determine whether the string can be integer for INT64

Todate

Converting the string to time type values, which can be converted into a legal Date character, such as yyyy-mm-dd or yyyy / mm / dd

Tonumber

Converting strings to numeric values

Date object expansion function

Function name

parameter

Description

Getquarter

Return time quarter value

Dateadd

_TimeInterval, _Number

Returns the time of the interval _Number, _TimerInterval is the interval type, with the year (YYY), month (mm), day (DD), time (h), points (m), second (s), Week (WW)

FormatorTDime

_NameFormate

Returns the format string of time, _nameformate is the format type, this function can be referred to the VBS sympathy function

Isnan

Whether the time variable is legal, TRUE is an illegal variable, FALSE is legal.

Number object expansion function

Function name

parameter

Description

ABS

Return value absolute value

ACOS

The return value is in the absence of radians.

asin

Return value is in an arc-oriented noctone

Atan

Return value cut in an arc unit

Atan2

_Value

Return value and _Value's unsuitable

CEIL

Returns the next integer greater than equals

COS

Return the cosine

EXP

Return value of European constant

Floor

Returns an integer that is less than the value equal to the value

log

The return value is natural logarithm for e a bottom

Round

Return to the number of rounds

Sin

The return value is sinusoidal in an arc.

SQRT

Return value square root

Isnan

Judging whether it is a legal value

Tan

The return value is in an arc.

The objects in the browser extends two messages prompt functions Alert and Cofirm, which can pop up the message box directly using the object reference function.

Code section:

-------------------------------------------------- -------------------------------------------------- -------------------

Array.prototype.indexOf = function (_value) {

For (var i = 0; i

Return -1;

}

Array.Prototype.lastIndexOf = Function (_Value) {

For (var i = this.length-1; i> = 0; i -) IF (this [i] == _ value) Return i;

Return -1;

}

Array.prototype.contains = function (_value) {Return this.indexof (_Value)! = -1;

Array.Prototype.copy = function () {Return this.concat ();

Array.Prototype.Insertat = function (_value, i) {this.splice (i, 0, _value);

Array.Prototype.Insertbefore = function (_Value, _invalue) {

Var i = this.indexof (_INVALUE);

IF (i == -1) this.push (_value);

Else this.splice (i, 0, _value);

}

Array.Prototype.removeat = function (i) {this.splice (i, 1);

Array.prototype.remove = function (_value) {

Var i = this.indexof (_Value);

IF (i! = -1) this.SPLICE (i, 1);

}

String.Prototype.trim = function () {Return this.Replace (/ (^ / s ) | / s $ / g, "");

String.prototype.ltrim = function () {Return this.Replace (/ (^ / s ) / g, "")};

String.prototype.rtrim = function () {Return this.Replace (/ (/ s $) / g, "")}

String.prototype.ltrimzero = function () {return this.Replace (/ (^ 0 ) / g, "")};

String.Prototype.Left = function (_leftnum) {return this.substr (0, _leftnum)};

String.Prototype.right = function (_rightnum) {return this.substr (this.length - _rightnum)}

String.Prototype.hasalpha = function () {var _checkalpha = /[//*/ /?/|/ (/ )/ }/ {】/-~` !@#$%^^&_=: "" '<>,.] /;

Return (_Checkalpha.test (this));

}

String.prototype.isalpha = function () {

Var _Checkalpha = /[xtile (/ )/ }/[/]/- ~` !@#%^&_=:;"'<> ,.]/ ;

Return (! _ checkalpha.test (this));

}

String.prototype.isletter = function () {Return ((// w / .test (this));};

String.Prototype.isfigure = function () {return (! // d / .test (this));

String.Prototype.isDomainName = function () {return (! / [^ / W -_ /.] | ^ /. | /. /. Test (this));

String.prototype.isemail = function () {

Var_emaillist = this.split ("@");

IF (_emaillist.length! = 2) Return False;

Return ((! / [^ / w -_] /. Test (_emaillist [0])) && _ emailist [1] .isdomainname ());

}

String.prototype.ishex = function () {return (! / [^ / Dabcdefabcdef] /. Test (this));

String.prototype.isguid = function () {

IF (this.Left (1)! = "{" || this.right (1)! = "}") Return False;

Var _HexNumberlist = this.Replace (/ (^ / {) | (/} $) / g, ""). Split ("-");

IF (_HexNumberList.Length! = 5) Return False;

IF (_HexNumberlist [0] .length! = 8 ||! _hexnumberlist [0] .ishex ()) Return False;

IF (_HexNumberlist [1] .length! = 4 ||! _hexnumberlist [1] .ishex ()) Return False;

IF (_HexNumberlist [2] .length! = 4 ||! _hexnumberlist [2] .ishex ()) Return False;

IF (_HexNumberlist [3] .length! = 4 ||! _hexnumberlist [3] .ishex ()) Return False;

IF (_HexNumberlist [4] .length! = 12 ||! _hexnumberlist [4] .isHex ()) Return False;

Return True;

}

String.Prototype.isinteger = function (_bittype) {

Var _LimitValue = [];

_LimitValue ["Upper"] = [];

_LimitValue ["Lower"] = []; _ limitValue ["Upper"] ["64bit"] = "9223372036854775807";

_LimitValue ["UPPER"] ["32bit"] = "2147483647";

_LimitValue ["UPPER"] ["16bit"] = "32767";

_LimitValue ["Lower"] ["64bit"] = "9223372036854775808";

_LIMITVALUE ["Lower"] ["32bit"] = "2147483648";

_LimitValue ["Lower"] ["16bit"] = "32768";

Var _plus = "Upper";

Var _thevalue = new string (this);

IF (_thevalue.indexof ("-") == 0) {

_thevalue = _thevalue.substr (1, _thevalue.length-1);

_Plus = "lower";

}

IF (! _ thevalue.isfigure ()) Return False;

IF (_limitvalue [_plus] [_ bitType] .length <_thevalue.length) Return False;

IF (_LimitValue [_plus] [_ bitType] .length == _thevalue.length) {

For (var i = 0; i <_limitvalue [_plus] [_ bittype] .length; i ) {

IF (_thevalue.Charat (i) <_limitvalue [_plus] [_ bittype] .Charat (i)) Return True;

}

IF (_LimitValue [_plus]! = _thevalue) Return False;

}

Return True;

}

String.prototype.isint8 = function () {

Var _thevalue = this.tonumber ();

IF (_thevalue.isnan ()) Return False;

IF (_thevalue <0 || _thevalue> 255) Return False;

IF (_thevalue.toString ()! = this) Return False;

Return True;

}

String.prototype.isint16 = function () {Return this.isinteger ("16bit");

String.prototype.isint32 = function () {Return this.isinteger ("32bit");

String.prototype.isint64 = function () {Return this.isinteger ("64bit");

String.prototype.todate = function () {

Var _dateStr = this.trim (). split (// s /) [0];

Var _TimeStr = (this.trim (). Split (// s /) [1]? this.trim (). Split (// s /) [1]: "1: 1: 1"); var _datesplitsymbol = /[///-,year]/;

Var _timesplitsymbol = / [:, time] /;

IF (! _ datesplitsymbol.test (_datestr)) Return New Date ("X");

Var _splitsymbol = _datesplitsymbol.exec (_datestr);

Var _Datelist = [];

IF (_SPLITSYMBOL == "year") {

IF (! ")> _dateStr.Indexof (" Month ") &&_DateStr.indexof (" Month ")> _DateStr.indexof (" Year ") &&_dateStr.Indexof (" Year)> 1 )) Return New Date ("x");

_datelist = _dateStr.Split (/ [Year of the Year] /);

} else _datelist = _dateStr.split (_SPLITSYMBOL);

IF (_Datelist.Length <2) Return New Date ("x");

Var _timelist = [1,1,1];

IF (_TimeSplitsymbol.test (_timeStr)) {

_SPLITSYMBOL = _TimesPlitsymbol.exec (_timeStr);

IF (_SPLITSYMBOL == "") {

IF (! ")> _TimeStr.Indexof (" 分 ") & _timestr.indexof (" 分 ")> _timeStr.Indexof (" Time "&& _timeStr.Indexof (" Time)> 1 )) Return New Date ("X");

_timelist = _timeStr.Split (/ [time division] /);

} else _timelist = _timestr.split (_splitsymbol);

}

Return New Date (_Datelist [0], _ Datelist [1], _ Datelist [2], _ Timelist [0], _ Timelist [1], _ Timelist [2]);

}

String.Prototype.tonumber = function () {return new number (this)}

Date.prototype.getquarter = function () {Return ((this.getmonth () 1) / 3) .ceil ();

Date.prototype.dateAdd = function (_timeInterval, _number) {

Switch (_TimeInterVal.touppercase ()) {

Case "YYYY":

Return new date (this.getfullyear () _Number, this.getMonth () (), this.getdate () (), this.getroup (), this.getseconds ());

Break;

Case "mm": return new date (this.getFullyEar (), this.getmonth () _Number, this.getdate (), this.gethouse (), this.gets (), this.getSeconds ());

Break;

Case "DD":

Return new date (this.getFullyEar (), this.getdate () _Number, this.gethai (), this.getminutes (), this.getseconds ());

Break;

Case "H":

Return New Date (this.getFullyEar (), this.getdate (), this.getdate (), this.gets () _Number, this.getminutes (), this.getSeconds ());

Break;

Case "M":

Return New Date (this.getFullyEar (), this.getMonth (), this.getdate (), this.getHours (), this.getminutes () _Number, this.getSeconds ());

Break;

Case "S":

Return new date (this.getFullyEar (), this.getMonth (), this.getdate (), this.getroup (), this.gets (), this.getSeconds () _Number;

Break;

Case "WW":

Return New Date (this.getFullyEar (), this.getMonth (), this.getdate () _Number * 7, this.gethai (), this.getminutes (), this.getSeconds ());

Break;

DEFAULT: RETURN THIS;

}

}

Date.prototype.formatdatetime = function (_nameformate) {

Switch (_NameFormate) {

Case 0:

RTURN this.Getfullyear (). TOSTRING (). Right (2) "/" (this.getMont () 1) .tostring () "/" this.getdate (). Tostring () "" this.GetHours (). toString () ":" this.getminutes (). TOSTRING () ":" this.getSeconds (). Tostring ();

Break;

Case 1:

Return this.Getfullyear (). toString () "year" (this.getmonth () 1) .tostring () "Month" this.Getdate (). Tostring () "Day";

Break;

Case 2:

RTURN this.GetFullyear (). TOSTRING (). Right (2) "/" (this.getmonth () 1) .tostring () "/" this.getdate (). Tostring (); break;

Case 3:

Return this.GetHours (). toString () ":" this.getminutes (). TOSTRING () ":" this.getSeconds (). Tostring ();

Break;

Case 4:

Return this.GetHours (). Tostring () ":" this.getminutes (). Tostring ();

Break;

DEFAULT:

RTURN this.Getfullyear (). TOSTRING (). Right (2) "/" (this.getMont () 1) .tostring () "/" this.getdate (). Tostring () "" this.GetHours (). toString () ":" this.getminutes (). TOSTRING () ":" this.getSeconds (). Tostring ();

}

}

Date.prototype.isnan = function () {Return Isn (this);

Number.prototype.abs = function () {return math.abs (this)}

Number.Prototype.acos = function () {return math.acos (this)}

Number.Prototype.asin = function () {return math.asin (this)}

Number.prototype.atan = function () {return math.atan (this)}

Number.prototype.atan2 = function () {return math.atan2 (this)}

Number.prototype.ceil = function () {return math.ceil (this)}

Number.prototype.cos = function () {return math.cos (this)}

Number.Prototype.exp = function () {return math.exp (this)}

Number.prototype.floor = function () {return math.floor (this)}

Number.prototype.log = function () {return math.log (this)}

Number.prototype.round = function () {return math.round (this)}

Number.prototype.sin = function () {return math.sin (this)};

Number.prototype.sqrt = function () {return math.sqrt (this)}

Number.prototype.isnan = function () {Return Isn (this)}

Number.prototype.tan = function () {return math.tan (this)}}}; if (window) {

Array.prototype.alert = function () {WINDOW.Alart (this);

Array.Prototype.confirm = function () {Return window.confirm (this);}

String.prototype.alert = function () {WINDOW.Alart (this)};

String.prototype.confirm = function () {return window.confirm (this)}

Date.prototype.alert = function () {WINDOW.Alart (this.tolocaledatestring ());};

Date.prototype.confirm = function () {Return window.confirm (this.tolocaledatestring ());

Number.prototype.alert = function () {WINDOW.Alart (THIS)};

Number.prototype.confirm = function () {return window.confirm (this)}

Boolean.Prototype.alert = function () {WINDOW.Alart (this);

Boolean.prototype.confirm = function () {return window.confirm (this);

}

-------------------------------------------------- -------------------------------------------------- -------------------

Second, about prototype properties

In JavaScript1.1, several basic objects String, Regexp, Array, Date, Number, Boolean, Function add a prototype attribute that allows programmers to add new properties and functions to existing object prototypes. The usage is as follows:

Function Obj () {

THIS.ID = "";

}

Obj.prototype.set_id = function (_value) {

THIS.ID = _Value;

}

VAR OBJ1 = New obj ();

Obj1.set_id ("test");

Give the OBJ class via the prototype (actually not class, but a function, here is just to facilitate expression) created a function (assigning a function to the SET_ID as a function).

Prototype is not very significant for custom pseudo, because custom pseudo classes can be created in constructor.

However, it has a more important meaning for the function of the existing JavaScript object type, which can expand new functions to the JS basic object. For example, there is no TRIM function similar to the VBS in JS, and if the TRIM function and other useful functions are written separately, the language simple characteristics of the JS script is lost. The most ideal situation is to expand it into the existing object prototype.

Take a look at some interesting examples:

("1234") .trim (). Alert (); // Pop up a dialog prompt to 1234

IF (Text1.Value.todate (). isnan ()) Alert ("Date Input Error!"); // Enter the time in a text box, the format does not prompt the error! Among the expansion functions are String functions, TRIM, LEFT, RIGHT, ISXX, and other functions are more commonly used in the verification of the form.

2004/5

转载请注明原文地址:https://www.9cbs.com/read-18420.html

New Post(0)