对象扩展方法,JavaScript原生对象及扩展

JavaScript 原生对象及扩充

对象扩展方法,JavaScript原生对象及扩展。2016/09/26 · JavaScript
· 对象

原稿出处: trigkit4   

Object类型:

https://segmentfault.com/a/1190000002634958

/** 在字符串末尾追加字符串 **/
String.prototype.append = function (str) {
    return this.concat(str);
}
/** 删除指定索引位置的字符,索引无效将不删除任何字符 **/
String.prototype.deleteCharAt = function (index) {
    if (index < 0 || index >= this.length) {
        return this.valueOf();
    }
    else if (index == 0) {
        return this.substring(1, this.length);
    }
    else if (index == this.length - 1) {
        return this.substring(0, this.length - 1);
    }
    else {
        return this.substring(0, index) + this.substring(index + 1);
    }
}
/** 删除指定索引区间的字符串 **/
String.prototype.deleteString = function (start, end) {
    if (start == end) {
        return this.deleteCharAt(start);
    }
    else {
        if (start > end) {
            var temp = start;
            start = end;
            end = temp;
        }
        if (start < 0) {
            start = 0;
        }
        if (end > this.length - 1) {
            end = this.length - 1;
        }
        return this.substring(0, start) + this.substring(end +1 , this.length);
    }
}
/** 检查字符串是否以subStr结尾 **/
String.prototype.endWith = function (subStr) {
    if (subStr.length > this.length) {
        return false;
    }
    else {
        return (this.lastIndexOf(subStr) == (this.length - subStr.length)) ? true : false;
    }
}
/** 比较两个字符串是否相等,也可以直接用 == 进行比较 **/
String.prototype.equal = function (str) {
    if (this.length != str.length) {
        return false;
    }
    else {
        for (var i = 0; i < this.length; i++) {
            if (this.charAt(i) != str.charAt(i)) {
                return false;
            }
        }
        return true;
    }
}
/** 比较两个字符串是否相等,不区分大小写 **/
String.prototype.equalIgnoreCase = function (str) {
    var temp1 = this.toLowerCase();
    var temp2 = str.toLowerCase();
    return temp1.equal(temp2);
}
/** 将指定的字符串插入到指定的位置后面,索引无效将直接追加到字符串的末尾 **/
String.prototype.insert = function (ofset, subStr) {
    if (ofset < 0 || ofset >= this.length - 1) {
        return this.append(subStr);
    }
    return this.substring(0, ofset + 1) + subStr + this.substring(ofset + 1);
}
/** 判断字符串是否数字串 **/
String.prototype.isAllNumber = function () {
    for (var i = 0; i < this.length; i++) {
        if (this.charAt(i) < '0' || this.charAt(i) > '9') {
            return false;
        }
    }
    return true;
}
/** 将字符串反序排列 **/
String.prototype.reserve = function () {
    var temp = "";
    for (var i = this.length - 1; i >= 0; i--) {
        temp = temp.concat(this.charAt(i));
    }
    return temp;
}
/** 将指定的位置的字符设置为另外指定的字符或字符串.索引无效将直接返回不做任何处理 **/
String.prototype.setCharAt = function (index, subStr) {
    if (index < 0 || index > this.length - 1) {
        return this.valueOf();
    }
    return this.substring(0, index) + subStr + this.substring(index+1);
}
/** 检查字符串是否以subStr开头 **/
String.prototype.startWith = function (subStr) {
    if (subStr.length > this.length) {
        return false;
    }
    return (this.indexOf(subStr) == 0) ? true : false;
}
/** 计算长度,每个汉字占两个长度,英文字符每个占一个长度 **/
String.prototype.charLength = function () {
    var temp = 0;
    for (var i = 0; i < this.length; i++) {
        if (this.charCodeAt(i) > 255) {
            temp += 2;
        }
        else {
            temp += 1;
        }
    }
    return temp;
}
String.prototype.charLengthReg = function () {
    return this.replace(/[^\x00-\xff]/g, "**").length;
}
/** 去掉首尾空格 **/
String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
/** 测试是否是数字 **/
String.prototype.isNumeric = function () {
    var tmpFloat = parseFloat(this);
    if (isNaN(tmpFloat))
        return false;
    var tmpLen = this.length - tmpFloat.toString().length;
    return tmpFloat + "0".Repeat(tmpLen) == this;
}
/** 测试是否是整数 **/ 
String.prototype.isInt = function () {
    if (this == "NaN")
        return false;
    return this == parseInt(this).toString();
}
/** 获取N个相同的字符串 **/
String.prototype.Repeat = function (num) {
    var tmpArr = [];
    for (var i = 0; i < num; i++) tmpArr.push(this);
    return tmpArr.join("");
}
/** 合并多个空白为一个空白 **/ 
String.prototype.resetBlank = function () {
    return this.replace(/s+/g, " ");
}
/** 除去左边空白 **/ 
String.prototype.LTrim = function () {
    return this.replace(/^s+/g, "");
}
/** 除去右边空白 **/ 
String.prototype.RTrim = function () {
    return this.replace(/s+$/g, "");
}
/** 除去两边空白 **/ 
String.prototype.trim = function () {
    return this.replace(/(^s+)|(s+$)/g, "");
}
/** 保留数字 **/ 
String.prototype.getNum = function () {
    return this.replace(/[^d]/g, "");
}
/** 保留字母 **/ 
String.prototype.getEn = function () {
    return this.replace(/[^A-Za-z]/g, "");
}
/** 保留中文 **/ 
String.prototype.getCn = function () {
    return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g, "");
}
/** 得到字节长度 **/ 
String.prototype.getRealLength = function () {
    return this.replace(/[^x00-xff]/g, "--").length;
}
/** 从左截取指定长度的字串 **/ 
String.prototype.left = function (n) {
    return this.slice(0, n);
}
/** 从右截取指定长度的字串 **/ 
String.prototype.right = function (n) {
    return this.slice(this.length - n);
}

放手对象与原生对象

内置(Build-in)对象与原生(Naitve)对象的区别在于:前者总是在汽油发动机伊始化阶段就被创制好的对象,是后者的3个子集;而后者包蕴了有的在运作进程中动态创立的靶子。

图片 1

//扩展:对JavaScript原生对象的扩展
//原理:原型对象
Object.prototype.keys = function(){
    var keys = [];
    for(var item in this)
        if(this.hasOwnProperty(item))//用于判断一个属性是否是对象实例定义的,还是从原型导入的
            keys.push(item);
    return keys;
}

var obj = {"name":"johnny","age":21};
obj.keys();//["name"."age"]

 

原生对象(New后的靶子)

ECMA-262 把原生对象(native object)定义为“独立于宿主环境的
ECMAScript 达成提供的靶子”。包蕴如下:

Object、Function、Array、String、Boolean、Number、Date、RegExp、Error、伊娃lError、RangeError、ReferenceError、SyntaxError、TypeError、UPAJEROIError、ActiveXObject(服务器方面)、Enumerator(集合遍历类)、RegExp(正则表明式)

1
Object、Function、Array、String、Boolean、Number、Date、RegExp、Error、EvalError、RangeError、ReferenceError、SyntaxError、TypeError、URIError、ActiveXObject(服务器方面)、Enumerator(集合遍历类)、RegExp(正则表达式)

透过能够看出,简单来讲,原生对象便是 ECMA-262 定义的类(引用类型)。

来源:

  

放置对象(不必要New)

概念:由ECMAScript达成提供的指标,独立于宿主环境,在三个本子程序实践的开头处。

:各类内置对象(built-in object)皆以原生对象(Native Object),一个内置的构造函数是几个放权的对象,也是二个构造函数。

来源:

举个栗子:

Native objects: Object (constructor), Date, Math, parseInt, eval。
string 方法比如 indexOf 和 replace, array 方法, … Host objects
(假定是浏览器环境): window, document, location, history, XMLHttpRequest,
setTimeout, getElementsByTagName, querySelectorAll, …

1
2
3
Native objects: Object (constructor), Date, Math, parseInt, eval。 string 方法比如 indexOf 和 replace, array 方法, …
 
Host objects (假定是浏览器环境): window, document, location, history, XMLHttpRequest, setTimeout, getElementsByTagName, querySelectorAll, …

ECMA-262][二 只定义了五个新的放到对象,即 GlobalMath
(它们也是原生对象,依照定义,各个内置对象都以原生对象)。

以下是ECMA-262概念的放置对象(built-in):

global、Object、Function、Array、String、Boolean、Number、Math、Date、RegExp、JSON、Error对象(Error,
EvalError, RangeError, ReferenceError, SyntaxError, TypeError
和URIError)

1
global、Object、Function、Array、String、Boolean、Number、Math、Date、RegExp、JSON、Error对象(Error,   EvalError, RangeError, ReferenceError,   SyntaxError, TypeError 和URIError)

图片 2

大家也足以修改内置对象的原型

JavaScript

if (!Array.prototype.forEach) { Array.prototype.forEach = function(fn){
for ( var i = 0; i < this.length; i++ ) { fn( this[i], i, this ); }
}; } [“a”, “b”, “c”].forEach(function(value, index, array){ assert(
value, “Is in position ” + index + ” out of ” + (array.length – 1) );
});

1
2
3
4
5
6
7
8
9
10
11
if (!Array.prototype.forEach) {
  Array.prototype.forEach = function(fn){
    for ( var i = 0; i < this.length; i++ ) {
      fn( this[i], i, this );
    }
  };
}
["a", "b", "c"].forEach(function(value, index, array){
  assert( value, "Is in position " + index + " out of " + (array.length – 1) );
});

如上代码将出口:

PASS Is in position 0 out of 2 PASS Is in position 1 out of 2 PASS Is in
position 2 out of 2

1
2
3
PASS Is in position 0 out of 2
PASS Is in position 1 out of 2
PASS Is in position 2 out of 2

专注:扩张原型是很危急的:

JavaScript

Object.prototype.keys = function(){ var keys = []; for ( var i in this
) keys.push( i ); return keys; }; var obj = { a: 1, b: 2, c: 3 };
assert( obj.keys().length == 3, “We should only have 3 properties.” );
delete Object.prototype.keys;

1
2
3
4
5
6
7
8
9
10
11
12
Object.prototype.keys = function(){
  var keys = [];
  for ( var i in this )
    keys.push( i );
  return keys;
};
var obj = { a: 1, b: 2, c: 3 };
assert( obj.keys().length == 3, "We should only have 3 properties." );
delete Object.prototype.keys;

输出: FAIL We should only have 3 properties.

假使不是有异样须要而去扩大原生对象和原型(prototype)的做法是不好的

JavaScript

//不要这么做 Array.prototype.map = function() { // code };

1
2
3
4
//不要这样做
Array.prototype.map = function() {
    // code
};

唯有那样做是值得的,例如,向一些旧的浏览器中增加一些ECMAScript5中的方法。
在这种场馆下,大家一般那样做:

XHTML

<script type=”text/javascript”> if (!Array.prototype.map) {
Array.prototype.map = function() { //code }; } </script>

1
2
3
4
5
6
7
<script type="text/javascript">
    if (!Array.prototype.map) {
        Array.prototype.map = function() {
            //code
        };
    }
</script>

Number类型:

用原型扩展对象

对js原生对象的恢宏无非正是往prototype里登记,例如,大家可今后String对象里增添ltrim,rtrim等措施。js种种对象都连任自Object,并且,对象和任何质量都由此prototype对象来再三再四。通过prototype目的,我们得以扩充任何对象,蕴含内建的目的,如StringDate

Number.prototype.add = function(param){
    return this + param;
}

var n = 5;
n.add(3);//8

String对象的庞大

JavaScript

<script type=”text/javascript”> if(typeof
String.prototype.ltrim==’undefined’){ String.prototype.ltrim =
function(){ var s = this; s = s.replace(/^\s*/g, ”); return s; } }
if(typeof String.prototype.rtrim==’undefined’){ String.prototype.rtrim =
function(){ var s = this; s = s.replace(/\s*$/g, ”); return s; } }
if(typeof String.prototype.trim==’undefined’){ String.prototype.trim =
function(){ return this.ltrim().rtrim(); } } if(typeof
String.prototype.htmlEncode==’undefined’){ String.prototype.htmlEncode =
function(encodeNewLine){//encodeNewLine:是否encode换行符 var s = this; s
= s.replace(/&/g, ‘&’); s = s.replace(/</g, ‘<‘); s =
s.replace(/>/g, ‘>’); s = s.replace(/’/g, ‘"’);
if(encodeNewLine){ s = s.replace(/\r\n/g, ‘<br />’); s =
s.replace(/\r/g, ‘<br />’); s = s.replace(/\n/g, ‘<br
/>’); } return s; } } </script>

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
<script type="text/javascript">
    if(typeof String.prototype.ltrim==’undefined’){
        String.prototype.ltrim = function(){
            var s = this;
            s = s.replace(/^\s*/g, ”);
            return s;
        }
    }
 
    if(typeof String.prototype.rtrim==’undefined’){
        String.prototype.rtrim = function(){
            var s = this;
            s = s.replace(/\s*$/g, ”);
            return s;
        }
    }
 
    if(typeof String.prototype.trim==’undefined’){
        String.prototype.trim = function(){
            return this.ltrim().rtrim();
        }
    }
 
    if(typeof String.prototype.htmlEncode==’undefined’){
        String.prototype.htmlEncode = function(encodeNewLine){//encodeNewLine:是否encode换行符
            var s = this;
            s = s.replace(/&/g, ‘&amp;’);
            s = s.replace(/</g, ‘&lt;’);
            s = s.replace(/>/g, ‘&gt;’);
            s = s.replace(/’/g, ‘&quot;’);
            if(encodeNewLine){
                s = s.replace(/\r\n/g, ‘<br />’);
                s = s.replace(/\r/g, ‘<br />’);
                s = s.replace(/\n/g, ‘<br />’);
            }
            return s;
        }
    }
</script>

 

Date对象的扩展

getDaysInMonth:获取某月有微微天

JavaScript

Date.getDaysInMonth = function (year, month) { var days = 0; switch
(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days
= 31 break; case 4: case 6: case 9: case 11: days = 30; break; case 2:
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) days = 29;
else days = 28; break; } return days; } if (typeof Date.prototype.format
== ‘undefined’) { Date.prototype.format = function (mask) { var d =
this; var zeroize = function (value, length) { if (!length) length = 2;
value = String(value); for (var i = 0, zeros = ”; i < (length –
value.length); i++) { zeros += ‘0’; } return zeros + value; }; return
mask.replace(/”[^”]*”|'[^’]*’|\b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])\1?|[lLZ])\b/g,
function ($0) { switch ($0) { case ‘d’: return d.getDate(); case ‘dd’:
return zeroize(d.getDate()); case ‘ddd’: return [‘Sun’, ‘Mon’, ‘Tue’,
‘Wed’, ‘Thr’, ‘Fri’, ‘Sat’][d.getDay()]; case ‘dddd’: return
[‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’,
‘Saturday’][d.getDay()]; case ‘M’: return d.getMonth() + 1; case
‘MM’: return zeroize(d.getMonth() + 1); case ‘MMM’: return [‘Jan’,
‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’, ‘Jul’, ‘Aug’, ‘Sep’, ‘Oct’, ‘Nov’,
‘Dec’][d.getMonth()]; case ‘MMMM’: return [‘January’, ‘February’,
‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’,
‘October’, ‘November’, ‘December’][d.getMonth()]; case ‘yy’: return
String(d.getFullYear()).substr(2); case ‘yyyy’: return d.getFullYear();
case ‘h’: return d.getHours() % 12 || 12; case ‘hh’: return
zeroize(d.getHours() % 12 || 12); case ‘H’: return d.getHours(); case
‘HH’: return zeroize(d.getHours()); case ‘m’: return d.getMinutes();
case ‘mm’: return zeroize(d.getMinutes()); case ‘s’: return
d.getSeconds(); case ‘ss’: return zeroize(d.getSeconds()); case ‘l’:
return zeroize(d.getMilliseconds(), 3); case ‘L’: var m =
d.getMilliseconds(); if (m > 99) m = Math.round(m / 10); return
zeroize(m); case ‘tt’: return d.getHours() < 12 ? ‘am’ : ‘pm’; case
‘TT’: return d.getHours() < 12 ? ‘AM’ : ‘PM’; case ‘Z’: return
d.toUTCString().match(/[A-Z]+$/); // Return quoted strings with the
surrounding quotes removed default: return $0.substr(1, $0.length – 2);
} }); }; }

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
Date.getDaysInMonth = function (year, month) {
            var days = 0;
            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    days = 31
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    days = 30;
                    break;
                case 2:
                    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                        days = 29;
                    else
                        days = 28;
                    break;
            }
            return days;
        }
 
        if (typeof Date.prototype.format == ‘undefined’) {
            Date.prototype.format = function (mask) {
 
                var d = this;
 
                var zeroize = function (value, length) {
 
                    if (!length) length = 2;
 
                    value = String(value);
 
                    for (var i = 0, zeros = ”; i < (length – value.length); i++) {
 
                        zeros += ‘0’;
 
                    }
 
                    return zeros + value;
 
                };
 
                return mask.replace(/"[^"]*"|'[^’]*’|\b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])\1?|[lLZ])\b/g, function ($0) {
 
                    switch ($0) {
 
                        case ‘d’: return d.getDate();
 
                        case ‘dd’: return zeroize(d.getDate());
 
                        case ‘ddd’: return [‘Sun’, ‘Mon’, ‘Tue’, ‘Wed’, ‘Thr’, ‘Fri’, ‘Sat’][d.getDay()];
 
                        case ‘dddd’: return [‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’][d.getDay()];
 
                        case ‘M’: return d.getMonth() + 1;
 
                        case ‘MM’: return zeroize(d.getMonth() + 1);
 
                        case ‘MMM’: return [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’, ‘Jul’, ‘Aug’, ‘Sep’, ‘Oct’, ‘Nov’, ‘Dec’][d.getMonth()];
 
                        case ‘MMMM’: return [‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’][d.getMonth()];
 
                        case ‘yy’: return String(d.getFullYear()).substr(2);
 
                        case ‘yyyy’: return d.getFullYear();
 
                        case ‘h’: return d.getHours() % 12 || 12;
 
                        case ‘hh’: return zeroize(d.getHours() % 12 || 12);
 
                        case ‘H’: return d.getHours();
 
                        case ‘HH’: return zeroize(d.getHours());
 
                        case ‘m’: return d.getMinutes();
 
                        case ‘mm’: return zeroize(d.getMinutes());
 
                        case ‘s’: return d.getSeconds();
 
                        case ‘ss’: return zeroize(d.getSeconds());
 
                        case ‘l’: return zeroize(d.getMilliseconds(), 3);
 
                        case ‘L’: var m = d.getMilliseconds();
 
                            if (m > 99) m = Math.round(m / 10);
 
                            return zeroize(m);
 
                        case ‘tt’: return d.getHours() < 12 ? ‘am’ : ‘pm’;
 
                        case ‘TT’: return d.getHours() < 12 ? ‘AM’ : ‘PM’;
 
                        case ‘Z’: return d.toUTCString().match(/[A-Z]+$/);
 
                            // Return quoted strings with the surrounding quotes removed    
 
                        default: return $0.substr(1, $0.length – 2);
 
                    }
 
                });
 
            };
        }

选取原生js实现复制对象及扩展

jQueryextend()主意能很便利的贯彻扩张对象方法,那里要兑现的是:使用原生js落实复制对象,扩张对象,类似jQuery中的extend()方法

JavaScript

var obj壹 = { name : ‘trigkit4’, age : 2二 }; var obj贰 = { name : ‘frank’,
age : 二一, speak : function(){ alert(“hi, I’m + name “); } }; var obj三 ={
age : 20 }; function cloneObj(oldObj) { //复制对象方法 if
(typeof(oldObj) != ‘object’) return oldObj; if (oldObj == null) return
oldObj; var newObj = Object(); for (var i in oldObj) newObj[i] =
cloneObj(oldObj[i]); return newObj; } function extendObj() {
//增添对象 var args = arguments;//将传递过来的参数数组赋值给args变量 if
(args.length < 2) return; var temp = cloneObj(args[新萄京349000com ,0]);
//调用复制对象方法 for (var n = 一; n < args.length; n++) { for (var i
in args[n]) { temp[i] = args[n][i]; } } return temp; } var obj
=extendObj(obj1,obj2,obj3); console.log(obj);//{ name: ‘frank’, age: 20,
speak: [Function] } console.log(obj1);//{ name: ‘trigkit4’, age: 22 }
console.log(obj2);//{ name: ‘frank’, age: 21, speak: [Function] }
console.log(obj3);//{ age: 20 }

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
var obj1 = {
    name : ‘trigkit4’,
    age : 22
};
var obj2 = {
    name : ‘frank’,
    age : 21,
    speak : function(){
        alert("hi, I’m + name ");
    }
};
 
var obj3 ={
    age : 20
};
 
function cloneObj(oldObj) { //复制对象方法
    if (typeof(oldObj) != ‘object’) return oldObj;
    if (oldObj == null) return oldObj;
    var newObj = Object();
    for (var i in oldObj)
        newObj[i] = cloneObj(oldObj[i]);
    return newObj;
}
 
function extendObj() { //扩展对象
    var args = arguments;//将传递过来的参数数组赋值给args变量
    if (args.length < 2) return;
    var temp = cloneObj(args[0]); //调用复制对象方法
    for (var n = 1; n < args.length; n++) {
        for (var i in args[n]) {
            temp[i] = args[n][i];
        }
    }
    return temp;
}
var obj =extendObj(obj1,obj2,obj3);
console.log(obj);//{ name: ‘frank’, age: 20, speak: [Function] }
console.log(obj1);//{ name: ‘trigkit4’, age: 22 }
console.log(obj2);//{ name: ‘frank’, age: 21, speak: [Function] }
console.log(obj3);//{ age: 20 }

es5-safe 模块

es5-safe 模块里,仅扩大了能够较好贯彻的能够高枕而卧接纳的1部分方法,包蕴:

JavaScript

Function.prototype.bind Object.create Object.keys Array.isArray
Array.prototype.forEach Array.prototype.map Array.prototype.filter
Array.prototype.every Array.prototype.some Array.prototype.reduce
Array.prototype.reduceRight Array.prototype.indexOf
Array.prototype.lastIndexOf String.prototype.trim Date.now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Function.prototype.bind
Object.create
Object.keys
Array.isArray
Array.prototype.forEach
Array.prototype.map
Array.prototype.filter
Array.prototype.every
Array.prototype.some
Array.prototype.reduce
Array.prototype.reduceRight
Array.prototype.indexOf
Array.prototype.lastIndexOf
String.prototype.trim
Date.now

详情:

指标的始建

JavaScript
援救多体系型的指标:内部对象、生成的靶子、宿主给出的靶子(如 Internet
浏览器中的 windowdocument)以及ActiveX 对象(外部组件)。

Microsoft Jscript 提供了 11
个内部(或“内置”)对象。它们是Array、Boolean、Date、Function、Global、Math、Number、Object、RegExp、Error
以及 String 对象。每3个对象有相关的点子和总体性,

JavaScript中指标的开创有以下二种方法:

(1)使用内置对象 (二)使用JSON符号 (三)自定义对象组织

1
2
3
(1)使用内置对象
(2)使用JSON符号
(3)自定义对象构造

相关文章